ARTICLE

Working with ListBox control in VB.NET

Posted by Dea Saddler Articles | ASP.NET using VB.NET April 04, 2011
In this article you will learn about ListBox control and also see how you can use its default event to display index number.
Download Files:
 
Reader Level:


A visual basic ListBox control displays a list of items from which you can make a selection. You can select one or more than one of the items from the ListBox. The ListBox control is based on the ListControl class which is based on the control class.


Properties of the ListBox:

  • MultiColumn:-Default value is false. You have to set it to true if you want the ListBox to display multiple columns 
  • Sorted:- Default value is false. You have to set it to true if you want the items displayed in the ListBox to be Sorted by alphabetical order. 
  • ScrollAlwaysVisible:- Default value is false. Setting it to true will display both vertical and horizontal scrollbar always. 
  • HorizontalScrollbar:-  Displays a horizontal scrollbar to the ListBox. It will work when the ListBox has multile columns.


ListBox Event:

The default event of ListBox is SelectedIndexChanged.

How you can work with ListBox
  • Simply just open a new project. 
  • Drag a ListBox, TextBox and a Button on Form and add some items to the ListBox by its item property. The form will look like below.

    LIST1.GIF
     
  • Items in a ListBox are referred by Index. When Items are added to the ListBox they assigned an index. Write the below code to display the index of an item.

       Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            TextBox1.Text = ListBox1.SelectedIndex
            'using the selected index property of the list box to select the index
        End
    Sub
     
  • Write the below code on Button Click to display the number of items avilable in the ListBox.

       
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e _
        As System.EventArgs) Handles Button1.Click
            TextBox1.Text = ListBox1.Items.Count
            'counting the number of items in the ListBox with the Items.Count
        End
    Sub

OutPut:

LIST2.GIF

LIST3.GIF

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Team Foundation Server Hosting
Become a Sponsor