Friday, February 14, 2014

How to add value to a list item in ListBox,ComboBox controls in VB.NET without ValueMember Property



As a beginner programmer you might have faced a lot of problems with the List Box and Combo Box controls in Visual Basic.NET while you access data from database with code.   If you want to use the property namely “ValueMember” then you must have to data bind the List Box with a data source. You can do that easily because there are a lot of tutorials regarding that. I’m not really going to explain how to connect to data base or how to data bind controls or similar stuffs. I’m just showing you how to set multiple values for a single Item in a List Box controls. I’ll show you it with an example. Before we start, you should have a clear understanding of classes. VB.NET is all about Classes and Objects. 



When you code the following please note that Item As Object.
 


Which means Every Item in the list box is an Object. That is every Objects can be the Instances of our own class. So how should that class look like? Let me explain it with a simple example.

Step 1:

Create a new project and add a List box to the current Form from the tool box. Look at the following screenshot for clarification.


Step 2:
 Go to Solution Explorer and Select your project and add a new Class. Name that with “ListItem”. Check out the screenshot.




Step 3:
 Now we created the class. The primary things you want make sure that the class you should be public and it must override the ToString() function. Override means, every class you create in vb.net is extend or inherited from a base class by default. So there some default methods came from the parent class like GetHashCode(),ToString() etc. We can change the functionality of these methods by “Overriding” these methods. That is, you have to write the method you like with a keyword Overrides. Now you might be asking that why do we want to Override the ToString() function? It’s simple, when you associate an object to a string by default, that object returns a value from the ToString() method. So when you add the object of this class to the List Box the display member (Item Text) will be the value of the ToString() method of our class. Please look at the code below.


 Step 4:
  Now it’s time to create the multi valued list item with our class. Assume that we are going to design like; one item represents one text and two values. So we need three variables representing the above specified things. The variable name and its data types are given below. You can have any number of variables with any data type you like.  

1)      Text : String 
2)      Value : Integer 
3)      Status : Boolean

All these should be declared as properties check out the code below,

Public Class ListItem
    Private ItemText As String
    Private ItemValue As Integer
    Private ItemStatus As Boolean

    Public Property Text
        Get
            Return ItemText
        End Get

        Set(ByVal value)
            ItemText = value
        End Set

    End Property

    Public Property Value
        Get
            Return ItemValue
        End Get
        Set(ByVal value)
            ItemValue = value
        End Set
    End Property

    Public Property Status
        Get
            Return ItemStatus
        End Get
        Set(ByVal value)
            ItemStatus = value
        End Set
    End Property

    Public Overrides Function ToString() As String
        Return Me.ItemText
    End Function
End Class

Step 5:

 Wow we completed our class. Now it’s time to use it. Open the form and add the following code.
We are going to add three items with the properties that we have defined earlier. Here we created an array of objects of our class.

Public Class List_Item

    Private Sub List_Item_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim litem(3) As ListItem

        litem(0) = New ListItem
        litem(0).Text = "Text1"
        litem(0).Value = 10
        litem(0).Status = True

        litem(1) = New ListItem
        litem(1).Text = "Text2"
        litem(1).Value = 11
        litem(1).Status = True

        litem(2) = New ListItem
        litem(2).Text = "Text3"
        litem(2).Value = 12
        litem(2).Status = False

        Me.ListBox1.Items.Add(litem(0))
        Me.ListBox1.Items.Add(litem(1))
        Me.ListBox1.Items.Add(litem(2))
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim Litem As ListItem = Me.ListBox1.SelectedItem

        MsgBox(Litem.Text)
        MsgBox(Litem.Value)
        MsgBox(Litem.Status)
    End Sub
End Class

When you click each item you can see its values in MsgBox.

Step 6:



Run!



Hope you enjoyed this tutorial. Thanks


Also read,

No comments:

Post a Comment