How to use a Listbox in VB.NET
Select a Listbox from Visual Basic Express or Visual Studio’s Toolbox. Draw the frame of that ListBox and you should be ready to go. Your ListBox should be named ListBox1 if is your first one in your form.
Then, in the code, we adjust the properties. I choose to do at the start of the form. You could do the same. I decided to do thing differently using the With function (it only change the presentation).
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With ListBox1
.Enabled = True 'if the listox is enable or disabled
.Sorted = True ' if you want ti list sorted
.BorderStyle = BorderStyle.Fixed3D ' the border style
.Visible = True
.ScrollAlwaysVisible = True 'presence of scroll all time
.MultiColumn = False 'add a new column if number of items reach max height
End With
End Sub
|
When the ListBox is initialized, we can add words in it. In the same load function, I decided to add a lot of words. Nothing complicate and obviously some are identical.
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With ListBox1
.Enabled = True 'if the listox is enable or disabled
.Sorted = True ' if you want ti list sorted
.BorderStyle = BorderStyle.Fixed3D ' the border style
.Visible = True
.ScrollAlwaysVisible = True 'presence of scroll all time
.MultiColumn = False 'add a new column if number of items reach max height
End With
ListBox1.Items.Add("allo1")
ListBox1.Items.Add("allo2")
ListBox1.Items.Add("allo3")
ListBox1.Items.Add("allo4")
ListBox1.Items.Add("allo5")
ListBox1.Items.Add("allo1")
ListBox1.Items.Add("allo2")
ListBox1.Items.Add("allo3")
ListBox1.Items.Add("allo4")
ListBox1.Items.Add("allo5")
ListBox1.Items.Add("allo1")
ListBox1.Items.Add("allo2")
ListBox1.Items.Add("allo3")
ListBox1.Items.Add("allo4")
ListBox1.Items.Add("allo5")
End Sub
|
ListBox1 is sorted alphabetically. That means all the “allo1” are all grouped together. “allo2” come after and so on. It will work each time you add a String in it.
If you execute the code right now, you will notice that no item is selected. This is perfect in most cases.
If you want to set an initial selection in ListBox1, you need to use SelectedIndex or SelectedItem.
Be careful. Each time you add an item or remove one from the ListBox, the indexes changes. Same thing if you sort an unsorted ListBox. So you can’t rely on the SelectedIndex property to get you result.
ListBox1.SelectedIndex = 2 '0 is the first one, 2 is the third.
|
You could use SelectedItem to get the String stored in the ListBox. Again, the String may not be unique, so you have to be careful. If the user chooses something in the ListBox, you have to make sure your program will deal it correctly. Please notice the programme will always stop his selection on the first encountered.
ListBox1.SelectedItem = "allo3" 'will always select the first he encounter
|
You could make the ListBox1 more interesting using the SelectionMode property.
ListBox1.SelectionMode = SelectionMode.MultiSimple 'no need to use shift or ctrl, only space or left-click
ListBox1.SelectionMode = SelectionMode.MultiExtended 'no need to use shift or ctrl with left-click
|
When SelectionMode is equal to SelectionMode.MultiSimple, then the user could simply use the mouse button to selecto or deselect the items in the ListBox1.
When SelectionMode is equal to SelectionMode. MultiExtended, the user need to use CTRL or SHIFT with the mouse button to select or deselects items.
We could also introduce a little event in the ListBox1. Lets say if the user choose an item in the ListBox, a little message box pops up and display the information. Just for fun or to validate our code.
Is pretty simple with Visual Basic Express 2010 or with Visual Studio 2010. Open your form with the design view form1.vb [Design]. Double click on your ListBox1. You should see something like this.
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
|
Now put a MsgBox in the function and try to display something. Like SelectedIndex.
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
MsgBox(ListBox1.SelectedIndex)
End Sub
|
So when you change the selection in ListBox1, it will show the index (a number).
Your code should look something like this by now:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With ListBox1
.Enabled = True 'if the listox is enable or disabled
.Sorted = True ' if you want ti list sorted
.BorderStyle = BorderStyle.Fixed3D ' the border style
.Visible = True
.ScrollAlwaysVisible = True 'presence of scroll all time
.MultiColumn = False 'add a new column if number of items reach max height
End With
ListBox1.Items.Add("allo1")
ListBox1.Items.Add("allo2")
ListBox1.Items.Add("allo3")
ListBox1.Items.Add("allo4")
ListBox1.Items.Add("allo5")
ListBox1.Items.Add("allo1")
ListBox1.Items.Add("allo2")
ListBox1.Items.Add("allo3")
ListBox1.Items.Add("allo4")
ListBox1.Items.Add("allo5")
ListBox1.Items.Add("allo1")
ListBox1.Items.Add("allo2")
ListBox1.Items.Add("allo3")
ListBox1.Items.Add("allo4")
ListBox1.Items.Add("allo5")
ListBox1.SelectedIndex = 2 '0 is the first one, 2 is the third.
ListBox1.SelectedItem = "allo3" 'will always select the first he encounter
ListBox1.SelectionMode = SelectionMode.MultiSimple 'no need to use shift or ctrl, only space or left-click
ListBox1.SelectionMode = SelectionMode.MultiExtended 'no need to use shift or ctrl with left-click
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
MsgBox(ListBox1.SelectedIndex)
End Sub
End Class
|
There is now a little flood in this code. When your start the code, the Msgbox is called before the form is completely loaded and chance are your get an empty Msgbox. This is not very sexy. We have to make this program a little more intelligent by putting a condition in the event function. Starting from now, I’ll quickly talk about it and forgive me if I am doing this too quickly. I’ll post another article later this week.
How to get rid of the Msgbox on load?
You have to bring 4 things in your code to make your code more intelligent:
-A condition in the function Listbox1 event
-A function that triggers the condition in the ListBox event.
-Declare a common variable available for both functions.
-Initialize that variable
Sorry if is not clear, is not always easy to explain something. Using this example might help.
The condition:
You need a if function or anything that could prevent your code to run the Msgbox. I love the if because is safe and simple to use.
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If form_onShow = True Then
'MsgBox(ListBox1.SelectedItem.ToString)
MsgBox(ListBox1.SelectedIndex)
End If
MsgBox(ListBox1.SelectedItem.ToString)
End Sub
|
If the variable Form_onShow is true, that meens the form is ready. Otherwise, nothing will happen.
The function that triggers the condition:
The form trigger a function when the form is completely loaded. That function is OnShown. That function is invisible in the form class, so you have to pick it and use it.
Here is the code:
Protected Overrides Sub OnShown(e As System.EventArgs)
MyBase.OnShown(e)
form_onShow = True
End Sub
|
Is best to simply copy and paste the code. No need to ask too much question here. The only thing we need to understand is that this function is triggered after the form is completely loaded. So is a good time to set the value form_onShow at true.
Declare a common variable available for both functions:
Every variable must be declared. Otherwise, the computer won’t be hable to know that is form_onShow. I suggest you to put the variable in the class Form1 but outside the functions.
Public Class Form1
Private form_onShow As Boolean 'variable member of Form1
Protected Overrides Sub OnShown(e As System.EventArgs)
MyBase.OnShown(e)
form_onShow = True
End Sub
…
…
…
|
Initialize that variable:
Finally, you need to set an initial value to your and the best place to do it, is in the new function. The new function is like the OnShown function, is hidden. So you have to bring it. Check the sample and copy paste it in Visual Basic Express 2012 or Visual Studio 2012 (any edition will work)
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
form_onShow = False
End Sub
|
Finally, your code should look like this (without the error handling):
Public Class Form1
Private form_onShow As Boolean 'variable member of Form1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
form_onShow = False
End Sub
Protected Overrides Sub OnShown(e As System.EventArgs)
MyBase.OnShown(e)
form_onShow = True
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
With ListBox1
.Enabled = True 'if the listox is enable or disabled
.Sorted = True ' if you want ti list sorted
.BorderStyle = BorderStyle.Fixed3D ' the border style
.Visible = True
.ScrollAlwaysVisible = True 'presence of scroll all time
.MultiColumn = False 'add a new column if number of items reach max height
End With
ListBox1.Items.Add("allo1")
ListBox1.Items.Add("allo2")
ListBox1.Items.Add("allo3")
ListBox1.Items.Add("allo4")
ListBox1.Items.Add("allo5")
ListBox1.Items.Add("allo1")
ListBox1.Items.Add("allo2")
ListBox1.Items.Add("allo3")
ListBox1.Items.Add("allo4")
ListBox1.Items.Add("allo5")
ListBox1.Items.Add("allo1")
ListBox1.Items.Add("allo2")
ListBox1.Items.Add("allo3")
ListBox1.Items.Add("allo4")
ListBox1.Items.Add("allo5")
ListBox1.SelectedIndex = 2 '0 is the first one, 2 is the third.
ListBox1.SelectedItem = "allo3" 'will always select the first he encounter
ListBox1.SelectionMode = SelectionMode.MultiSimple 'no need to use shift or ctrl, only space or left-click
ListBox1.SelectionMode = SelectionMode.MultiExtended 'no need to use shift or ctrl with left-click
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
If form_onShow = True Then
'MsgBox(ListBox1.SelectedItem.ToString)
MsgBox(ListBox1.SelectedIndex)
End If
MsgBox(ListBox1.SelectedItem.ToString)
End Sub
End Class
|
Hiç yorum yok:
Yorum Gönder