Adding 3 valued object to a combo box in VB.NET
Hi,
We usually come across situations where we add two valued object to a combo box. One is used as an index (Value Member) and the other is used to display the text (Display Member).
But in rare cases we may come across 3-valued object. For ex: I want Entity Id, Entity Name, Entity type to be added to a combo box.
The following code snippet demonstrates how can we achieve this:
'First create a class containing 3 fields & properties for accessing the members.
-------------------------------------------------------------------------------------------
Public Class clsComboBoxItem2
Private m_ItemText As String 'This is for Entityname
Private m_ItemIndex As Int32 'This is for Entity Id
Private m_ItemText2 As String 'This is for Entity type
'Constructor
Public Sub New(ByVal strItemText As String, ByVal intItemIndex As Integer, ByVal strItemText2 As String)
m_ItemText = strItemText
m_ItemIndex = intItemIndex
m_ItemText2 = strItemText2End Sub
End Sub
Public ReadOnly Property ItemIndex() As String
Get
Return m_ItemIndex
End Get
End Property
Public ReadOnly Property ItemText() As String
Get
Return m_ItemText
End Get
End Property
Public ReadOnly Property ItemText2() As String
Get
Return m_ItemText2
End Get
End Property
End Class
-----------------------------------------------------------------------------
Code to add 3-valued elements to combo box:
For i = 0 To ds.Rows.Count - 1
cmbEntities.Items.Add(
New clsComboBoxItem2(ds.Rows(i).Item(0), ds.Rows(i).Item(1), ds.Rows(i).Item(2)))
Next
cmbEntities.DisplayMember = "ItemText"
cmbEntities.ValueMember = "ItemIndex"
-----------------------------------------------------------------------------
Code to retrieve selected item from combo box:
Dim itmCombo2 as clsComboBoxItem2 =
CType(cmbEntities.SelectedItem, clsComboBoxItem2)
Dim EntityId as Int32 = itmCombo2.ItemIndex
Dim EntityName as String = itmCombo2.ItemText
Dim EntityType as String = itmCombo2.ItemText2
----------------------------------------------------------------------------
Hope this is very clear.
Even though this code is in VB.NET, same logic is used for C#.NET also.
Cheers....
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment