Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,038 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,587 people online right now. Registration is fast and FREE... Join Now!




Listbox Order

 
Reply to this topicStart new topic

Listbox Order, Question about custom listbox order

nofear217
27 Feb, 2008 - 04:09 PM
Post #1

D.I.C Regular
Group Icon

Joined: 8 Nov, 2007
Posts: 253



Thanked: 6 times
Dream Kudos: 175
My Contributions
Ok, so....on a whim I've decided to write a trivial program to organize Wal-Mart shopping trips. A simple program yet effective I think. The problem I've hit is that I can't find anywhere a way in which I can do custom ordering on a ListBox. The only way to order that I know of is alphabetically. Basically, what I want to do is have a list of items you could select and then based on their location in the store, sort them in the list that I can then print out. This will effectively make store trips more efficient so that you can make a round trip through the store and not miss any items and have to go back and get more.

Here's what I have so far.....

CODE
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!")
            ' Plan to change this around later

        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


I figured the easiest way would be to sort the items in the list and then just shoot them out into a file. If it's not possible to do a custom re-ordering, I'll have to figure out some kind of logic to reorder as I'm streaming the items into the string to output to the file (if anyone has a suggestion on that route, I'd appreciate it as well). Thanks for your time. Peace, love, and chicken grease.
User is offlineProfile CardPM
+Quote Post

nofear217
RE: Listbox Order
2 Mar, 2008 - 08:52 PM
Post #2

D.I.C Regular
Group Icon

Joined: 8 Nov, 2007
Posts: 253



Thanked: 6 times
Dream Kudos: 175
My Contributions
P.S.-- No ideas as of yet? Well I've come up with a slight workaround, albeit a bit longer....I split the categories up into different tabs and then can sort them into the list based on in which tab they are. Looks like this:

CODE
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


Though if anyone has an idea on just using one tab/listbox and doing a custom sort, I'd appreciate it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 09:46PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month