Hi,
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:
[CODE=vbnet]Public Class Form1
Private Sub ListBox1_Double Click(ByVal sender As Object, ByVal e As System.EventArg s) Handles ListBox1.Double Click
ListBox2.Items. Add(ListBox1.Se lectedItem)
ListBox1.Items. Remove(ListBox1 .SelectedItem)
End Sub
Private Sub ListBox2_Double Click(ByVal sender As Object, ByVal e As System.EventArg s) Handles ListBox2.Double Click
ListBox1.Items. Add(ListBox2.Se lectedItem)
ListBox2.Items. Remove(ListBox2 .SelectedItem)
End Sub
Private Sub btnSave_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) 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).ToStrin g & vbCrLf
Next
If Not IO.File.Exists( "C:\" & Today.Month & "-" & Today.Day & "-" & Today.Year & ".txt") Then
IO.File.WriteAl lText("C:\" & Today.Month & "-" & Today.Day & "-" & Today.Year & ".txt", items)
Else
MsgBox("File already exists!")
End If
End Sub
Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnExit.Click
Me.Close()
End Sub
End Class[/CODE]
Now, I've come up with an alternative solution by placing each type of item on a tab with two listboxes each and can then output them into a file based on which listbox group they are in, but my curiousity has been peaked and I was wondering if there is a way to do custom sorting within a listbox.
Here's the other code for the workaround:
[CODE=vbnet]Public Class Form2
Private Sub lbFood_DoubleCl ick(ByVal sender As Object, ByVal e As System.EventArg s) Handles lbFood.DoubleCl ick
lbFoodList.Item s.Add(lbFood.Se lectedItem)
lbFood.Items.Re move(lbFood.Sel ectedItem)
End Sub
Private Sub lbFoodList_Doub leClick(ByVal sender As Object, ByVal e As System.EventArg s) Handles lbFoodList.Doub leClick
lbFood.Items.Ad d(lbFoodList.Se lectedItem)
lbFoodList.Item s.Remove(lbFood List.SelectedIt em)
End Sub
Private Sub lbToilitries_Do ubleClick(ByVal sender As Object, ByVal e As System.EventArg s) Handles lbToiletries.Do ubleClick
lbToiletriesLis t.Items.Add(lbT oiletries.Selec tedItem)
lbToiletries.It ems.Remove(lbTo iletries.Select edItem)
End Sub
Private Sub lbToilitriesLis t_DoubleClick(B yVal sender As Object, ByVal e As System.EventArg s) Handles lbToiletriesLis t.DoubleClick
lbToiletries.It ems.Add(lbToile triesList.Selec tedItem)
lbToiletriesLis t.Items.Remove( lbToiletriesLis t.SelectedItem)
End Sub
Private Sub btnSave_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnSave.Click
Dim toiletryItems As Integer = lbToiletriesLis t.Items.Count
Dim foodItems As Integer = lbFoodList.Item s.Count
Dim cleaningItems As Integer = lbCleaningList. Items.Count
Dim miscItems As Integer = lbMiscList.Item s.Count
Dim items As String = ""
items &= "Toiletries (Pharmacy area): " & vbCrLf
For i As Integer = 0 To toiletryItems - 1
items &= lbToiletriesLis t.Items.Item(i) .ToString & vbCrLf
Next
items &= vbCrLf & "Cleaning Items:" & vbCrLf
For i As Integer = 0 To cleaningItems - 1
items &= lbCleaningList. Items(i).ToStri ng & vbCrLf
Next
items &= vbCrLf & "Food: " & vbCrLf
For i As Integer = 0 To foodItems - 1
items &= lbFoodList.Item s.Item(i).ToStr ing & vbCrLf
Next
items &= vbCrLf & "Misc Items:" & vbCrLf
For i As Integer = 0 To miscItems - 1
items &= lbMiscList.Item s.Item(i).ToStr ing & vbCrLf
Next
If Not IO.Directory.Ex ists("C:\Docume nts and Settings\All Users\Desktop\S hopping List") Then
IO.Directory.Cr eateDirectory(" C:\Documents and Settings\All Users\Desktop\S hopping List")
End If
IO.File.WriteAl lText("C:\Docum ents and Settings\All Users\Desktop\S hopping List\" & txtSave.Text & ".txt", items)
End Sub
Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnExit.Click
Me.Close()
End Sub
Private Sub lbCleaning_Doub leClick(ByVal sender As Object, ByVal e As System.EventArg s) Handles lbCleaning.Doub leClick
lbCleaningList. Items.Add(lbCle aning.SelectedI tem)
lbCleaning.Item s.Remove(lbClea ning.SelectedIt em)
End Sub
Private Sub lbCleaningList_ DoubleClick(ByV al sender As Object, ByVal e As System.EventArg s) Handles lbCleaningList. DoubleClick
lbCleaning.Item s.Add(lbCleanin gList.SelectedI tem)
lbCleaningList. Items.Remove(lb CleaningList.Se lectedItem)
End Sub
Private Sub lbMisc_DoubleCl ick(ByVal sender As Object, ByVal e As System.EventArg s) Handles lbMisc.DoubleCl ick
lbMiscList.Item s.Add(lbMisc.Se lectedItem)
lbMisc.Items.Re move(lbMisc.Sel ectedItem)
End Sub
Private Sub lbMiscList_Doub leClick(ByVal sender As Object, ByVal e As System.EventArg s) Handles lbMiscList.Doub leClick
lbMisc.Items.Ad d(lbMiscList.Se lectedItem)
lbMiscList.Item s.Remove(lbMisc List.SelectedIt em)
End Sub
Private Sub Form2_Load(ByVa l sender As Object, ByVal e As System.EventArg s) Handles Me.Load
lbFood.Sorted = True
lbCleaning.Sort ed = True
lbToiletries.So rted = True
lbMisc.Sorted = True
End Sub
End Class[/CODE]
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:
[CODE=vbnet]Public Class Form1
Private Sub ListBox1_Double Click(ByVal sender As Object, ByVal e As System.EventArg s) Handles ListBox1.Double Click
ListBox2.Items. Add(ListBox1.Se lectedItem)
ListBox1.Items. Remove(ListBox1 .SelectedItem)
End Sub
Private Sub ListBox2_Double Click(ByVal sender As Object, ByVal e As System.EventArg s) Handles ListBox2.Double Click
ListBox1.Items. Add(ListBox2.Se lectedItem)
ListBox2.Items. Remove(ListBox2 .SelectedItem)
End Sub
Private Sub btnSave_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) 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).ToStrin g & vbCrLf
Next
If Not IO.File.Exists( "C:\" & Today.Month & "-" & Today.Day & "-" & Today.Year & ".txt") Then
IO.File.WriteAl lText("C:\" & Today.Month & "-" & Today.Day & "-" & Today.Year & ".txt", items)
Else
MsgBox("File already exists!")
End If
End Sub
Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnExit.Click
Me.Close()
End Sub
End Class[/CODE]
Now, I've come up with an alternative solution by placing each type of item on a tab with two listboxes each and can then output them into a file based on which listbox group they are in, but my curiousity has been peaked and I was wondering if there is a way to do custom sorting within a listbox.
Here's the other code for the workaround:
[CODE=vbnet]Public Class Form2
Private Sub lbFood_DoubleCl ick(ByVal sender As Object, ByVal e As System.EventArg s) Handles lbFood.DoubleCl ick
lbFoodList.Item s.Add(lbFood.Se lectedItem)
lbFood.Items.Re move(lbFood.Sel ectedItem)
End Sub
Private Sub lbFoodList_Doub leClick(ByVal sender As Object, ByVal e As System.EventArg s) Handles lbFoodList.Doub leClick
lbFood.Items.Ad d(lbFoodList.Se lectedItem)
lbFoodList.Item s.Remove(lbFood List.SelectedIt em)
End Sub
Private Sub lbToilitries_Do ubleClick(ByVal sender As Object, ByVal e As System.EventArg s) Handles lbToiletries.Do ubleClick
lbToiletriesLis t.Items.Add(lbT oiletries.Selec tedItem)
lbToiletries.It ems.Remove(lbTo iletries.Select edItem)
End Sub
Private Sub lbToilitriesLis t_DoubleClick(B yVal sender As Object, ByVal e As System.EventArg s) Handles lbToiletriesLis t.DoubleClick
lbToiletries.It ems.Add(lbToile triesList.Selec tedItem)
lbToiletriesLis t.Items.Remove( lbToiletriesLis t.SelectedItem)
End Sub
Private Sub btnSave_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnSave.Click
Dim toiletryItems As Integer = lbToiletriesLis t.Items.Count
Dim foodItems As Integer = lbFoodList.Item s.Count
Dim cleaningItems As Integer = lbCleaningList. Items.Count
Dim miscItems As Integer = lbMiscList.Item s.Count
Dim items As String = ""
items &= "Toiletries (Pharmacy area): " & vbCrLf
For i As Integer = 0 To toiletryItems - 1
items &= lbToiletriesLis t.Items.Item(i) .ToString & vbCrLf
Next
items &= vbCrLf & "Cleaning Items:" & vbCrLf
For i As Integer = 0 To cleaningItems - 1
items &= lbCleaningList. Items(i).ToStri ng & vbCrLf
Next
items &= vbCrLf & "Food: " & vbCrLf
For i As Integer = 0 To foodItems - 1
items &= lbFoodList.Item s.Item(i).ToStr ing & vbCrLf
Next
items &= vbCrLf & "Misc Items:" & vbCrLf
For i As Integer = 0 To miscItems - 1
items &= lbMiscList.Item s.Item(i).ToStr ing & vbCrLf
Next
If Not IO.Directory.Ex ists("C:\Docume nts and Settings\All Users\Desktop\S hopping List") Then
IO.Directory.Cr eateDirectory(" C:\Documents and Settings\All Users\Desktop\S hopping List")
End If
IO.File.WriteAl lText("C:\Docum ents and Settings\All Users\Desktop\S hopping List\" & txtSave.Text & ".txt", items)
End Sub
Private Sub btnExit_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnExit.Click
Me.Close()
End Sub
Private Sub lbCleaning_Doub leClick(ByVal sender As Object, ByVal e As System.EventArg s) Handles lbCleaning.Doub leClick
lbCleaningList. Items.Add(lbCle aning.SelectedI tem)
lbCleaning.Item s.Remove(lbClea ning.SelectedIt em)
End Sub
Private Sub lbCleaningList_ DoubleClick(ByV al sender As Object, ByVal e As System.EventArg s) Handles lbCleaningList. DoubleClick
lbCleaning.Item s.Add(lbCleanin gList.SelectedI tem)
lbCleaningList. Items.Remove(lb CleaningList.Se lectedItem)
End Sub
Private Sub lbMisc_DoubleCl ick(ByVal sender As Object, ByVal e As System.EventArg s) Handles lbMisc.DoubleCl ick
lbMiscList.Item s.Add(lbMisc.Se lectedItem)
lbMisc.Items.Re move(lbMisc.Sel ectedItem)
End Sub
Private Sub lbMiscList_Doub leClick(ByVal sender As Object, ByVal e As System.EventArg s) Handles lbMiscList.Doub leClick
lbMisc.Items.Ad d(lbMiscList.Se lectedItem)
lbMiscList.Item s.Remove(lbMisc List.SelectedIt em)
End Sub
Private Sub Form2_Load(ByVa l sender As Object, ByVal e As System.EventArg s) Handles Me.Load
lbFood.Sorted = True
lbCleaning.Sort ed = True
lbToiletries.So rted = True
lbMisc.Sorted = True
End Sub
End Class[/CODE]
Comment