New to the forums and have a question. I've only been developing for about five months now so I apologize if this seems oversimplistic....I am writing a program on an idea I had on a whim. It's based on the fact that a store (Wal-Mart) is laid out in a logical manner and you can shop efficiently if your shopping list is in order. What I have are two listboxes, the left one contains all the items I might buy and as you double click on each item it will place it into the right listbox. What I was needing is a way to custom sort the items as they are added into the right listbox based on a few criteria (location in store and type). Here's the code I originally came up with:
Expand|Select|Wrap|Line Numbers
- Public Class Form1
- Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
- ListBox2.Items.Add(ListBox1.SelectedItem)
- ListBox1.Items.Remove(ListBox1.SelectedItem)
- End Sub
- Private Sub ListBox2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.DoubleClick
- ListBox1.Items.Add(ListBox2.SelectedItem)
- ListBox2.Items.Remove(ListBox2.SelectedItem)
- End Sub
- Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
- Dim shopItems As Integer = ListBox2.Items.Count
- Dim items As String = ""
- For i As Integer = 0 To shopItems - 1
- items &= ListBox2.Items.Item(i).ToString & vbCrLf
- Next
- If Not IO.File.Exists("C:\" & Today.Month & "-" & Today.Day & "-" & Today.Year & ".txt") Then
- IO.File.WriteAllText("C:\" & Today.Month & "-" & Today.Day & "-" & Today.Year & ".txt", items)
- Else
- MsgBox("File already exists!")
- End If
- End Sub
- Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
- Me.Close()
- End Sub
- End Class
Here's the other code for the workaround:
Expand|Select|Wrap|Line Numbers
- Public Class Form2
- Private Sub lbFood_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbFood.DoubleClick
- lbFoodList.Items.Add(lbFood.SelectedItem)
- lbFood.Items.Remove(lbFood.SelectedItem)
- End Sub
- Private Sub lbFoodList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbFoodList.DoubleClick
- lbFood.Items.Add(lbFoodList.SelectedItem)
- lbFoodList.Items.Remove(lbFoodList.SelectedItem)
- End Sub
- Private Sub lbToilitries_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbToiletries.DoubleClick
- lbToiletriesList.Items.Add(lbToiletries.SelectedItem)
- lbToiletries.Items.Remove(lbToiletries.SelectedItem)
- End Sub
- Private Sub lbToilitriesList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbToiletriesList.DoubleClick
- lbToiletries.Items.Add(lbToiletriesList.SelectedItem)
- lbToiletriesList.Items.Remove(lbToiletriesList.SelectedItem)
- End Sub
- Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
- Dim toiletryItems As Integer = lbToiletriesList.Items.Count
- Dim foodItems As Integer = lbFoodList.Items.Count
- Dim cleaningItems As Integer = lbCleaningList.Items.Count
- Dim miscItems As Integer = lbMiscList.Items.Count
- Dim items As String = ""
- items &= "Toiletries (Pharmacy area): " & vbCrLf
- For i As Integer = 0 To toiletryItems - 1
- items &= lbToiletriesList.Items.Item(i).ToString & vbCrLf
- Next
- items &= vbCrLf & "Cleaning Items:" & vbCrLf
- For i As Integer = 0 To cleaningItems - 1
- items &= lbCleaningList.Items(i).ToString & vbCrLf
- Next
- items &= vbCrLf & "Food: " & vbCrLf
- For i As Integer = 0 To foodItems - 1
- items &= lbFoodList.Items.Item(i).ToString & vbCrLf
- Next
- items &= vbCrLf & "Misc Items:" & vbCrLf
- For i As Integer = 0 To miscItems - 1
- items &= lbMiscList.Items.Item(i).ToString & vbCrLf
- Next
- If Not IO.Directory.Exists("C:\Documents and Settings\All Users\Desktop\Shopping List") Then
- IO.Directory.CreateDirectory("C:\Documents and Settings\All Users\Desktop\Shopping List")
- End If
- IO.File.WriteAllText("C:\Documents and Settings\All Users\Desktop\Shopping List\" & txtSave.Text & ".txt", items)
- End Sub
- Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
- Me.Close()
- End Sub
- Private Sub lbCleaning_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbCleaning.DoubleClick
- lbCleaningList.Items.Add(lbCleaning.SelectedItem)
- lbCleaning.Items.Remove(lbCleaning.SelectedItem)
- End Sub
- Private Sub lbCleaningList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbCleaningList.DoubleClick
- lbCleaning.Items.Add(lbCleaningList.SelectedItem)
- lbCleaningList.Items.Remove(lbCleaningList.SelectedItem)
- End Sub
- Private Sub lbMisc_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbMisc.DoubleClick
- lbMiscList.Items.Add(lbMisc.SelectedItem)
- lbMisc.Items.Remove(lbMisc.SelectedItem)
- End Sub
- Private Sub lbMiscList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbMiscList.DoubleClick
- lbMisc.Items.Add(lbMiscList.SelectedItem)
- lbMiscList.Items.Remove(lbMiscList.SelectedItem)
- End Sub
- Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- lbFood.Sorted = True
- lbCleaning.Sorted = True
- lbToiletries.Sorted = True
- lbMisc.Sorted = True
- End Sub
- End Class