473,395 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Listbox Custom Sorting

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:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.  
  3.  
  4.     Private Sub ListBox1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.DoubleClick
  5.  
  6.         ListBox2.Items.Add(ListBox1.SelectedItem)
  7.         ListBox1.Items.Remove(ListBox1.SelectedItem)
  8.  
  9.     End Sub
  10.  
  11.  
  12.     Private Sub ListBox2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox2.DoubleClick
  13.  
  14.         ListBox1.Items.Add(ListBox2.SelectedItem)
  15.         ListBox2.Items.Remove(ListBox2.SelectedItem)
  16.  
  17.     End Sub
  18.  
  19.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
  20.  
  21.         Dim shopItems As Integer = ListBox2.Items.Count
  22.         Dim items As String = ""
  23.  
  24.         For i As Integer = 0 To shopItems - 1
  25.  
  26.  
  27.             items &= ListBox2.Items.Item(i).ToString & vbCrLf
  28.  
  29.         Next
  30.  
  31.         If Not IO.File.Exists("C:\" & Today.Month & "-" & Today.Day & "-" & Today.Year & ".txt") Then
  32.  
  33.             IO.File.WriteAllText("C:\" & Today.Month & "-" & Today.Day & "-" & Today.Year & ".txt", items)
  34.  
  35.         Else
  36.  
  37.             MsgBox("File already exists!")
  38.  
  39.         End If
  40.  
  41.     End Sub
  42.  
  43.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
  44.  
  45.         Me.Close()
  46.  
  47.     End Sub
  48.  
  49. End Class
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:

Expand|Select|Wrap|Line Numbers
  1. Public Class Form2
  2.  
  3.     Private Sub lbFood_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbFood.DoubleClick
  4.  
  5.         lbFoodList.Items.Add(lbFood.SelectedItem)
  6.         lbFood.Items.Remove(lbFood.SelectedItem)
  7.  
  8.     End Sub
  9.  
  10.     Private Sub lbFoodList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbFoodList.DoubleClick
  11.  
  12.         lbFood.Items.Add(lbFoodList.SelectedItem)
  13.         lbFoodList.Items.Remove(lbFoodList.SelectedItem)
  14.  
  15.     End Sub
  16.  
  17.     Private Sub lbToilitries_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbToiletries.DoubleClick
  18.  
  19.         lbToiletriesList.Items.Add(lbToiletries.SelectedItem)
  20.         lbToiletries.Items.Remove(lbToiletries.SelectedItem)
  21.  
  22.     End Sub
  23.  
  24.     Private Sub lbToilitriesList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbToiletriesList.DoubleClick
  25.  
  26.         lbToiletries.Items.Add(lbToiletriesList.SelectedItem)
  27.         lbToiletriesList.Items.Remove(lbToiletriesList.SelectedItem)
  28.  
  29.     End Sub
  30.  
  31.     Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
  32.  
  33.         Dim toiletryItems As Integer = lbToiletriesList.Items.Count
  34.         Dim foodItems As Integer = lbFoodList.Items.Count
  35.         Dim cleaningItems As Integer = lbCleaningList.Items.Count
  36.         Dim miscItems As Integer = lbMiscList.Items.Count
  37.         Dim items As String = ""
  38.  
  39.         items &= "Toiletries (Pharmacy area): " & vbCrLf
  40.  
  41.         For i As Integer = 0 To toiletryItems - 1
  42.  
  43.             items &= lbToiletriesList.Items.Item(i).ToString & vbCrLf
  44.  
  45.         Next
  46.  
  47.         items &= vbCrLf & "Cleaning Items:" & vbCrLf
  48.  
  49.         For i As Integer = 0 To cleaningItems - 1
  50.  
  51.             items &= lbCleaningList.Items(i).ToString & vbCrLf
  52.  
  53.         Next
  54.  
  55.         items &= vbCrLf & "Food: " & vbCrLf
  56.  
  57.         For i As Integer = 0 To foodItems - 1
  58.  
  59.             items &= lbFoodList.Items.Item(i).ToString & vbCrLf
  60.  
  61.         Next
  62.  
  63.         items &= vbCrLf & "Misc Items:" & vbCrLf
  64.  
  65.         For i As Integer = 0 To miscItems - 1
  66.  
  67.             items &= lbMiscList.Items.Item(i).ToString & vbCrLf
  68.  
  69.         Next
  70.  
  71.         If Not IO.Directory.Exists("C:\Documents and Settings\All Users\Desktop\Shopping List") Then
  72.  
  73.             IO.Directory.CreateDirectory("C:\Documents and Settings\All Users\Desktop\Shopping List")
  74.  
  75.         End If
  76.  
  77.         IO.File.WriteAllText("C:\Documents and Settings\All Users\Desktop\Shopping List\" & txtSave.Text & ".txt", items)
  78.  
  79.     End Sub
  80.  
  81.     Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
  82.  
  83.         Me.Close()
  84.  
  85.     End Sub
  86.  
  87.     Private Sub lbCleaning_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbCleaning.DoubleClick
  88.  
  89.         lbCleaningList.Items.Add(lbCleaning.SelectedItem)
  90.         lbCleaning.Items.Remove(lbCleaning.SelectedItem)
  91.  
  92.     End Sub
  93.  
  94.     Private Sub lbCleaningList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbCleaningList.DoubleClick
  95.  
  96.         lbCleaning.Items.Add(lbCleaningList.SelectedItem)
  97.         lbCleaningList.Items.Remove(lbCleaningList.SelectedItem)
  98.  
  99.     End Sub
  100.  
  101.     Private Sub lbMisc_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbMisc.DoubleClick
  102.  
  103.         lbMiscList.Items.Add(lbMisc.SelectedItem)
  104.         lbMisc.Items.Remove(lbMisc.SelectedItem)
  105.  
  106.     End Sub
  107.  
  108.     Private Sub lbMiscList_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbMiscList.DoubleClick
  109.  
  110.         lbMisc.Items.Add(lbMiscList.SelectedItem)
  111.         lbMiscList.Items.Remove(lbMiscList.SelectedItem)
  112.  
  113.     End Sub
  114.  
  115.     Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  116.  
  117.         lbFood.Sorted = True
  118.         lbCleaning.Sorted = True
  119.         lbToiletries.Sorted = True
  120.         lbMisc.Sorted = True
  121.  
  122.     End Sub
  123. End Class
Feb 28 '08 #1
1 1684
debasisdas
8,127 Expert 4TB
Question moved to .NET forum.
Feb 28 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Marri Suliez | last post by:
Has anyone found some documentation on how to properly subclass a ListBox control and provide custom sorting (when the list items come from a DataSource)? The only way I can figure out how to do...
3
by: Alex Stevens | last post by:
I'd already posted this in microsoft.public.dotnet.framework.windowsforms and microsoft.public.dotnet.framework.windowsforms.controls to no avail so apologies for the cross-posting. Hi, I'm...
4
by: Jason | last post by:
Here is an odd issue. I am trying to shed some light on why this is causing a problem. I have an ArrayList. I am binding it to a ListBox control with has its Sort property set to True. If the...
3
by: Ali Chambers | last post by:
Hi, I have a bit of a problem with a sort procedure I need to do. I have a list of items in a listbox, eg:- 2.3%<A other text here> -4%<B other text here> 10%<C other text here> -9.3%<D...
3
by: Leszek Klich | last post by:
Hello All ! I have a task: QT library: List ListBox. I generating n random numbers from range 0 to 100. I have to sort it by hand... It has to look nicely. It's my workhome from my school....
7
by: 00_ChInkPoIntD12 | last post by:
Can anyone confirm there isn't a Sort() method for WebControl Listbox in Asp.net? It is rather simple to write a method to do the sorting, but just wondering I shouldn't invent the wheel if...
2
by: Joe Fallon | last post by:
I have 2 listboxes on a Web Form. As I move an item from 1 to the other it shows up at the end of the list. How can I sort the list that just got the new item added to it so it is in alphabetical...
4
by: Ambica Jain | last post by:
Hi, I want custom sorting on some of the columns in the datagrid. And i am able to do the same by overriding MouseDown event. However, i need to rebind my datatable to reflect the changes in...
1
by: Sunray | last post by:
I have a form called the sales form and i have 2 sets of listboxes So what happens is. i add items form the bottom set of list boxes which are bound to a data base to the top set of list boxes which...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.