473,766 Members | 2,130 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Listbox Custom Sorting

1 New Member
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 1713
debasisdas
8,127 Recognized Expert Expert
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
560
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 this is by sorting an array of some sort and then setting the DataSource. I don't like this because it fires the DataSourceChanged event. What I really want to do is get access to the underlying list items, sort them, and then have the...
3
2419
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 writing a usercontrol which displays the typical two listboxes and the ability to move items from one to the other. The listboxes are populated with my custom objects (SwapItem), which simply
4
5910
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 ArrayList only has one element in it everything works ok. But as soon as I have more than one element, I get the following exception when the control loads up: "Cannot modify the Items collection when the DataSource property is set.". Anybody...
3
2977
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 other text here> 22%<E other text here>
3
5745
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. Sorting must visualise (preview) process in list. Any method of sorting. I do not want to use stantard methods including to QT. I have ready: (main window, list, slider (count elements to generic), button: generic numeric
7
10068
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 there is already one. C.P.
2
2892
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 order? -- Joe Fallon
4
3099
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 grid. And with rebinding, sorting image (little triangle on the column header) goes away. I need to show sorting image as well custom sorting. Please help. Thanks
1
4030
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 are not bound, I select from the bottom set and add to the top set which works fine, but now i decide to remove an item from the top set. when i tried to use a remove item code it worked fine, it did delete the item form the list but it added...
0
9571
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10009
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8835
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5279
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.