ComboBox help
-
Hi, I need help displaying a list of items in a combo box. My problem is this, I want to display a name "Drive Time" and when it is selected i want to use the value of the variable "002302" it is like: Drive Time = 002302 I want to display "Drive Time" and I want to use the value "002302" when it is selected in the combobox. I hope this made sense. I appreciate the help. Thanks
-
Hi, I need help displaying a list of items in a combo box. My problem is this, I want to display a name "Drive Time" and when it is selected i want to use the value of the variable "002302" it is like: Drive Time = 002302 I want to display "Drive Time" and I want to use the value "002302" when it is selected in the combobox. I hope this made sense. I appreciate the help. Thanks
It would be best if you created a structure to hold both items as a single datatype, and then you create a List(Of item) and bind the ComboBox to the List(Of T). Then in your ComboBox.SelectedIndexChanged you can reset the textbox Like this
Public Class Form1
Private Structure Items Private strName As String Private intID As Integer Public Property Name() As String Get Return strName End Get Set(ByVal value As String) strName = value End Set End Property Public Property ID() As Integer Get Return intID End Get Set(ByVal value As Integer) intID = value End Set End Property End Structure Private lstItems As List(Of Items) Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lstItems = New List(Of Items) For i As Integer = 0 To 5 Dim newItem As New Items newItem.Name = "Item " & i.ToString newItem.ID = i lstItems.Add(newItem) Next ComboBox1.DataSource = lstItems ComboBox1.DisplayMember = "Name" ComboBox1.ValueMember = "ID" End Sub Private Sub ComboBox1\_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged TextBox1.Text = CType(ComboBox1.SelectedItem, Items).Name End Sub
End Class
Hope this helps Happy Coding
-
Hi, I need help displaying a list of items in a combo box. My problem is this, I want to display a name "Drive Time" and when it is selected i want to use the value of the variable "002302" it is like: Drive Time = 002302 I want to display "Drive Time" and I want to use the value "002302" when it is selected in the combobox. I hope this made sense. I appreciate the help. Thanks
Yes, makes sense. Why don't you use an ArrayList in conjuction with your ComboBox and make the indexes correspond to one another? E.g.
ComboBox1.Items.Add("Drive Time")
ComboBox1.Items.Add("......")
...
Array1.Add("002302")
Array1.Add("......")
...Public Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
value = Array1.Item(ComboBox1.SelectedIndex).ToString
End Sub
-
Yes, makes sense. Why don't you use an ArrayList in conjuction with your ComboBox and make the indexes correspond to one another? E.g.
ComboBox1.Items.Add("Drive Time")
ComboBox1.Items.Add("......")
...
Array1.Add("002302")
Array1.Add("......")
...Public Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
value = Array1.Item(ComboBox1.SelectedIndex).ToString
End Sub
-
It would be best if you created a structure to hold both items as a single datatype, and then you create a List(Of item) and bind the ComboBox to the List(Of T). Then in your ComboBox.SelectedIndexChanged you can reset the textbox Like this
Public Class Form1
Private Structure Items Private strName As String Private intID As Integer Public Property Name() As String Get Return strName End Get Set(ByVal value As String) strName = value End Set End Property Public Property ID() As Integer Get Return intID End Get Set(ByVal value As Integer) intID = value End Set End Property End Structure Private lstItems As List(Of Items) Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lstItems = New List(Of Items) For i As Integer = 0 To 5 Dim newItem As New Items newItem.Name = "Item " & i.ToString newItem.ID = i lstItems.Add(newItem) Next ComboBox1.DataSource = lstItems ComboBox1.DisplayMember = "Name" ComboBox1.ValueMember = "ID" End Sub Private Sub ComboBox1\_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged TextBox1.Text = CType(ComboBox1.SelectedItem, Items).Name End Sub
End Class
Hope this helps Happy Coding
-
Yes, makes sense. Why don't you use an ArrayList in conjuction with your ComboBox and make the indexes correspond to one another? E.g.
ComboBox1.Items.Add("Drive Time")
ComboBox1.Items.Add("......")
...
Array1.Add("002302")
Array1.Add("......")
...Public Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
value = Array1.Item(ComboBox1.SelectedIndex).ToString
End Sub
That is horrible. It was OK in the eighties perhaps, now things get to be handled in an object-oriented way, so turn those things into objects, show one of their properties and use another property. BTW: that is exactly what ComboBox.DisplayMember and ValueMember properties are for. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
That is horrible. It was OK in the eighties perhaps, now things get to be handled in an object-oriented way, so turn those things into objects, show one of their properties and use another property. BTW: that is exactly what ComboBox.DisplayMember and ValueMember properties are for. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
Hi, I need help displaying a list of items in a combo box. My problem is this, I want to display a name "Drive Time" and when it is selected i want to use the value of the variable "002302" it is like: Drive Time = 002302 I want to display "Drive Time" and I want to use the value "002302" when it is selected in the combobox. I hope this made sense. I appreciate the help. Thanks
I hate to admit it but I just recently learned this. I believe what you are trying to do is display one value and use another that is associated with it for something else. The easiest way I have seen this done is to use the DisplayMember and the ValueMember properties of the combo box like I said I just figured this out and it has made my life so much easier than the old way we handled it. So in your case you would use.
cboMyCombo.DisplayMember = "Drive Time"
cboMyCombo.ValueMember = "002302"Msgbox(cboMyCombo.ValueMember.ToString())
I did not compile that code it may or may not work but you get the general idea.
Humble Programmer
-
I hate to admit it but I just recently learned this. I believe what you are trying to do is display one value and use another that is associated with it for something else. The easiest way I have seen this done is to use the DisplayMember and the ValueMember properties of the combo box like I said I just figured this out and it has made my life so much easier than the old way we handled it. So in your case you would use.
cboMyCombo.DisplayMember = "Drive Time"
cboMyCombo.ValueMember = "002302"Msgbox(cboMyCombo.ValueMember.ToString())
I did not compile that code it may or may not work but you get the general idea.
Humble Programmer
-
Hi, I need help displaying a list of items in a combo box. My problem is this, I want to display a name "Drive Time" and when it is selected i want to use the value of the variable "002302" it is like: Drive Time = 002302 I want to display "Drive Time" and I want to use the value "002302" when it is selected in the combobox. I hope this made sense. I appreciate the help. Thanks
I think the best solution for this is using an enum
private enum combo_values
Drive_Time = 002302
.....
.....
end enumthis is the code for diclareing an enum. Unfortunataly ths would mean that you would have to use an underscore instead of a space. But I guess the user would not mind. So, once the enum is diclared, you can enter the following code when an item is selected:
myValue=ctype(combo1.text,enum)
if you want make 'combo_values' a variable of your type, you can do it by typing something like,'
private enum combo_values as integer
'. and do inform me if it does not work.