How to Sort Items in a ListBox in Visual Basic.NET
Sorting is a neat feature that can be done to lists for easier accessibility. Sorting lists in alphabetical order has kind of become a norm in our society, people are used to it, so naturally as programmers we would like to display our data in alphabetical order when possible. This tutorial will show you how to easily sort a list box in alphabetical order making it easier for users to view the data.
If you have not done so already, we need to set up a web site and web form to work on. Click on File > New > Web Site. Using ‘Visual Basic’ as the template, select ASP.NET Empty Web Site, name the web site anything you’d like and click OK. Now look in the solution explorer window and you will see the web site we just created, right click it and select Add New Item. Once again, use ‘Visual Basic’ as the template and select Web Form. Leave the file name as Default.aspx and click add. Now that we have a web site and web form to work on, we can begin coding.
Open Default.apsx in source view so we can see the html side of things of our web form. Expand the ‘Toolbox’ panel and place a ListBox item inside the ‘div’ tags. Now place a Button item after the closing list box tag and change it’s text to Sort.
Now lets add items to the listbox, we will use classic video games as our topic of choice for the list. To do this we must use the ListItem template inside the listbox tags with the name of an item inside of the list item templates. Look in the code snippet below for a better understanding and apply the same items in your list box.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<form id="form1" runat="server">
<asp:listbox ID="lstGames" runat="server" >
<asp:ListItem>Super Mario Bros.</asp:ListItem>
<asp:ListItem>Sonic the Hedgehog</asp:ListItem>
<asp:ListItem>The Legend of Zelda</asp:ListItem>
<asp:Button ID="Button1" runat="server" Text="Sort" />
If you are not already on Default.aspx, do so and switch to design view to create a event handler in Default.aspx.vb. You will see a private sub called Button1_Click and inside of this sub is where we will insert code that will sort the list box. For this specific example we will create a private sub and then call it inside the ‘Button1_Click’ sub. So whenever we want to sort a list we can just call the sub rather than writing tedious code. Apply the data in the code snippet below to the Default.aspx.vb page:
Partial Class _Default
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Private Sub sortListBoxItems(ByVal list As Object)
' Create Variables
Dim li As ListItem
Dim sl As SortedList = New SortedList
' Loop through each Item in the List and move them over to the SortedList
For Each li In list.Items
' Move sorted items back to List again
list.DataSource = sl
list.DataValueField = "Value"
list.DataTextField = "Key"
In the sortListBoxItems private sub, we declare a sorted list and add the contents in the list to ti. We then declare list.DataSource to equal the newly created sorted list. Now we can sort the list box by defining a ‘DataValueField’, ‘DataTextField’, and data binding. Data binding pretty much secures controls to data from databases and is commonly used when working with databases.
Run the page by clicking on the green arrow near the top or pressing F5. If it asks you to ‘Modify the Web.config file to enable debugging’ click OK. You will see the list box and the sort button. Notice how the list box is not sorted in alphabetical order, so click on the Sort button and like magic, the list box is now sorted alphabetically.
So now you know how to sort a list box. Organizing data by certain attributes is very common, whether it be alphabetical, numerical, or maybe by how many items are contained within it. No matter what, good organization of data is key in any application or web site so utilize it whenever possible.