Home > other >  Get a value by index from combobox class
Get a value by index from combobox class

Time:10-25

i'm approaching to vbnet from vb6 and i'm triyng to get value from combobox using a class which contains the values i stored in. here is the class

        Private m_ItemText As String
        Private m_ItemIndex As Int32

        Public Sub New(ByVal strItemText As String, ByVal intItemIndex As Int32)
            m_ItemText = strItemText
            m_ItemIndex = intItemIndex
        End Sub

        Public ReadOnly Property ItemIndex() As Int32
            Get
                Return m_ItemIndex
            End Get
        End Property

    Public ReadOnly Property ItemText() As String
        Get
            Return m_ItemText
        End Get
    End Property

I use this method charge the combobox

        ComboBox2.Items.Add(New clsComboBoxItem("sometext", 1))
        ComboBox2.Items.Add(New clsComboBoxItem("sometext 2", 2))
        ComboBox2.Items.Add(New clsComboBoxItem("sometext", 3))

and this on combobox.selectedindexchanged

        If ComboBox2.SelectedItem.GetType.ToString = itmCombo.GetType.ToString Then
            itmCombo = CType(ComboBox2.SelectedItem, clsComboBoxItem)
            MessageBox.Show("Item Text=" & itmCombo.ItemText & " and ItemIndex=" & CStr(itmCombo.ItemIndex))

        End If

Can anyone tell help me to understand how get an element by his index stored in the class? Eg writing '2' into a text box, the combo box sould be show "sometext2". Suppose i want to expand the class adding some values, like m_ItemText2,m_ItemText3 etc, i would learn a method to get all of theese values. I hope I was clear Thanks in advance

CodePudding user response:

If you have a DataSource set to a DataTable for your ComboBox, just set the DisplayMember and ValueMember. My test ComboBox is set to DropDownList.

Private Sub FillComboBox()
    Dim dt As New DataTable
    Using con As New SqlConnection(ConStr),
        cmd As New SqlCommand("Select FlavorID,FlavorName From Flavors", con)
        con.Open()
        Using reader = cmd.ExecuteReader
            dt.Load(reader)
        End Using
    End Using

    ComboBox1.DisplayMember = "FlavorName"
    ComboBox1.ValueMember = "FlavorID"
    ComboBox1.DataSource = dt
End Sub

To display the the values To display the Text cast to DataRowView (that is the object that is in the Item), provide the field you want and call ToString.

Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
    MessageBox.Show(ComboBox1.SelectedValue.ToString)
    MessageBox.Show(DirectCast(ComboBox1.SelectedItem, DataRowView)("FlavorName").ToString)
End Sub

If you are adding items one by one, you can still set the DisplayMember and ValueMember.

'https://stackoverflow.com/questions/38206678/set-displaymember-and-valuemember-on-combobox-without-datasource
 Private Sub SomeFormsLoadEvent()
    ComboBox1.Items.Add(New KeyValuePair(Of String, Integer)("Ultra-fast", 600))
    ComboBox1.Items.Add(New KeyValuePair(Of String, Integer)("Fast", 300))
    ComboBox1.Items.Add(New KeyValuePair(Of String, Integer)("Medium", 150))
    ComboBox1.Items.Add(New KeyValuePair(Of String, Integer)("Slow", 75))

    ComboBox1.DisplayMember = "Key"
    ComboBox1.ValueMember = "Value"
    ComboBox1.DataSource = ComboBox1.Items
End Sub

I found it a bit more complicated to display the text. I had to cast the item to its underlying type (KeyValuePair) then ask for the Key value.

Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
    MessageBox.Show(ComboBox1.SelectedValue.ToString)
    MessageBox.Show(DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of String, Integer)).Key)
End Sub

CodePudding user response:

As I understand it you want to store values in a class and display and access them through the combobox. How about this approach:

The class for the values:

Public Class clsValues

Private lstItemTexts As New List(Of String)


Public ReadOnly Property AllValues As List(Of String)
    Get
        Return lstItemTexts
    End Get
End Property

'To initialize class with empty list, items can be added with AddItems
Public Sub New()

End Sub

'To initialize class with items, items can still be added with AddItems
Public Sub New(lstItemTexts As List(Of String))
    Me.lstItemTexts = lstItemTexts
End Sub

Public Sub AddItem(item As String)
    Me.lstItemTexts.Add(item)
End Sub

Public Function GetItemByIndex(index As Integer) As String
    Return lstItemTexts(index)
End Function

Public Function GetIndexByItem(item As String) As Integer
    Return lstItemTexts.IndexOf(item)
End Function

End Class

You can declare it and fill values like this:

 Private Values As New clsValues()
 Values.AddItem("Some Text 1")
 Values.AddItem("Some Text 2")

or

Private Values As New clsValues(New List(Of String)({"Some Text 1", "Some Text 2"}))

to get a value from combobox

Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox2.SelectedIndexChanged

    MessageBox.Show(Values.GetItemByIndex(ComboBox2.SelectedIndex))

End Sub

CodePudding user response:

What @Mary stated is true the ComboBox has values that are useful SEE CODE BELOW Change gvTxType to be a TextBox to see results when you click on the tvTxType

    Private Sub cbTxType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbTxType.SelectedIndexChanged
    If cbTxType.SelectedIndex > -1 Then
        'Dim sindex As Integer
        'sindex = cbTxType.SelectedIndex
        'Dim sitem As String
        sitem = CType(cbTxType.SelectedItem, String)
        'MsgBox("You Selected " & sitem)
        'Index is ZERO based
        gvTxType = sitem
    End If
End Sub
  • Related