Omg, sometimes VB.NET is really wierd! I have searched the internet for ages, I was looking for a way to add items to a combobox with a value and a different displayvalue. So without further ado: Create a class for the items you want to add, for example:
Public Class cValue
Private sDisplay As String
Private sValue As String
Public Property Display() As String
Get
Return Me.sDisplay
End Get
Set(ByVal value As String)
Me.sDisplay = value
End Set
End Property
Public Property Value() As String
Get
Return Me.sValue
End Get
Set(ByVal value As String)
Me.sValue = value
End Set
End Property
Public Sub
New(ByVal sDisplay As String, ByVal sValue As String)
Me.Display = sDisplay
Me.Value = sValue
End Sub
Public Overrides Function ToString() As String
Return Display
End Function
End Class
Populate the comboboxes with the following code:
Dim alItems As ArrayList = New ArrayList()
olItems.add(new cValue("Display1", "Value1")
olItems.add(new cValue("Display2", "Value2")
olItems.add(new cValue("Display3", "Value3")
cboComboBox.DataSource = aLanden
cboComboBox.DisplayMember = "Display"
cboComboBox.ValueMember = "Value"
Make sure that you combobox is not sorted! Check it in the property-window of Visual Studio, the property is called "Sorted".
Reacties
Dustin Davis schreef:
19/01/07
I've done this myself, but I have any issue. The combo box is bound to a datasource, it is not populating the values automatically. And, whenever I select a value and the combobox loses focus, the text disappears from the control. Every encountered this?
tys schreef:
19/01/07
Never encountered that before, the code just works fine here. However it is just a "translation" from my C#-code
Joe schreef:
02/01/08
I've run into this a few times and wasn't very happy with the solution that I came up with. It is very similar to the code listed here. The major difference is that I don't use an array list as the datasource for the combobox. Another minor difference is that I use a custom "Name Value" class that I tend to use over and over.
Last note, there is a custom Method to add items to the combobox, a custom Property to get or set the selected item in the combobox:
' Class to hold the Display and Value
Public Class clsNameValue
Private _name As String = ""
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Private _value As String = ""
Public Property Value() As String
Get
Return _value
End Get
Set(ByVal value As String)
_value = value
End Set
End Property
Private _tag As String = ""
Public Property Tag() As String
Get
Return _tag
End Get
Set(ByVal value As String)
_tag = value
End Set
End Property
Public Sub New(ByRef Name As String, ByRef Value As String)
_name = Name
_value = Value
End Sub
Public Overrides Function ToString() As String
Return _value
End Function
End Class
Public Class dlgSelectEvent
Public Sub AddItem(ByRef oEvent As clsNameValue)
Me.cbEvent.Items.Add(oEvent)
Me.cbEvent.Sorted = True
End Sub
Public Property EventType() As String
Get
Return CType(Me.cbEvent.SelectedItem, clsNameValue).Name
End Get
Set(ByVal value As String)
For Each oNV As clsNameValue In Me.cbEvent.Items
If oNV.Name = value Then
Me.cbEvent.SelectedItem = oNV
Exit For
End If
Next
ValidateName()
End Set
End Property
End Class
Richard schreef:
02/06/08
Excellent, works well, thx for that. If your combo needs to be bound to a datasource add the line;
cboComboBox.DataBindings.Add("SelectedValue",BindingSource, "FieldName")
nisreen schreef:
07/08/08
hey plz i tried to get values from the combobox using vb.net and it failed can anyone help me???
Sentax schreef:
04/11/08
Well, I really like the approach and it works great for me. But there is a big problem with your code sample, there are syntax errors all over it, even some strange double-quote that Visual Studio doesn't like. You should really double-check your code for errors as newbies would have a hard time just copy/pasting this.
Thanks though...
tijs schreef:
06/11/08
@sentax: My code-plugin converts quotes into curly-quotes. So you should replace them by regular ones.
Praveen schreef:
07/02/09
I Tried to get selectitem value from combobox,anyone can help me how to do this.
Norman schreef:
22/02/10
MY GOD MAN YOUR A BLOODY GENIOUS!!!
MANY THANKS.... ive been banging my head of the wall the last two days.... because of a couple of filtered comboboxes and trying to get the filtered data into a second combo box. but was getting an non-zero index error!
Many thanks once again
Norm
Ekeolere Olaide schreef:
03/03/10
GOD Bless the creator of this cValue Class, your reward is in heaven. I have had this problem since little forever.
It will be very ungrateful of me not to drop a comment
jussy schreef:
07/04/10
Hello all,
Does anybody know how to carry out multiplication using ComBoBox. Suppose i have a combobox for the number of days in a month and a textbox for TRAVELLING. Upon selecting the working day in the combobox, this is multiplied by 20 and returns the value in the textbox travelling. How do i do this in VB.NET. Reply on:
APuma schreef:
21/04/10
You say to make sure the combobox is not sorted, I would assume that is because the add method of the combobox will request the toString method of each item already in its collection every time a new item is added, slows way down when you want to add 1,000's of items. has anyone found a workaround for this issue.
I have tried setting the sorted property to false before adding the items to the control as well as suspendLayout() and Visible = False. Any Ideas on how to get them to load faster (not call toString() every time an item is added)?
Sidhhu schreef:
26/04/10
Hey it work fines....
But what if i want display Textbox at UpdateMode so ihave to display a text according to selected value...so it not work plz help me out of this
Vijay schreef:
04/10/10
Hi,
Not sure if this will be helpful, I simply insert a dummy row in the dataset...
Dim sql As String = ""
Dim ds As DataSet = Nothing
'Init Company drop-down
sql = "(SELECT '' AS myCode, '' AS myDesc) " _
& "UNION " _
& "SELECT myCode, myDesc from myTable"
ds = DatabaseHelper.ExecuteDataSet(sql)
If ds.Tables(0).Rows.Count > 0 Then
With Me.txtJOCompany
.DataSource = Nothing
.Items.Clear()
.DataSource = ds.Tables(0)
.DisplayMember = "myDesc"
.ValueMember = "myCode"
.SelectedIndex = 0
End With
End If
Dave schreef:
28/01/11
Great!
How do you now get the value of the selected item in the combobox?
Thanks
SP schreef:
26/05/11
@Dave: To get the value, typcast the combobox.selectedItem to cvalue. And then use its value property.
CType(cbClient.SelectedItem, cValue).Value