none
Combobox and text box in vb2010 RRS feed

  • Question

  • Dear Friend

    i use 1-comboBox, 1-TextBox , 2-Button

    my code

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim comboSource As New Dictionary(Of Integer, String)()
            comboSource.Add(25, "Anbu")
            comboSource.Add(20, "Rose")
            comboSource.Add(45, "Siva")
            comboSource.Add(75, "Srini")
            comboSource.Add(60, "Nazar")
            comboSource.Add(40, "Sakthi")
            comboSource.Add(65, "Vince")

            ComboBox1.DataSource = New BindingSource(comboSource, Nothing)
            ComboBox1.DisplayMember = "Value"
            ComboBox1.ValueMember = "Key"
        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim key As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of Integer, String)).Key
            Dim value As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of Integer, String)).Value
            MessageBox.Show(key & "   " & value)
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            
        End Sub
    End Class

     My Request

    if i click button1 then message  show combobox  display member and value member.

    i want , i will give the value member in Textbox, comboBox will display the related display member.  what i do?

    help me pls.

    Thanks

    Anburose

    Friday, August 17, 2018 11:52 AM

All replies

  • Here is an example that may suit your needs

    cboDepartments.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    cboDepartments.AutoCompleteSource = AutoCompleteSource.ListItems
    cboDepartments.DropDownStyle = ComboBoxStyle.DropDown
    Dim ops = New Operations
    Dim bs As New BindingSource
    bs.DataSource = ops.Deparments
    cboDepartments.DataSource = bs
    cboDepartments.DisplayMember = "key"
    cboDepartments.ValueMember = "value"
    TextBox1.DataBindings.Add("Text", bs, "Key")
    TextBox2.DataBindings.Add("Text", bs, "Value")
    Except for the TextBox controls the full example is here which gets data from a database table but will work w/o a database table also. There may be some code that needs tweaking as I did this in Visual Studio 2017, have not had VS2010 for many years.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, August 17, 2018 12:37 PM
    Moderator
  • Give an example of a text, typed in Textbox, and the expected results.

    Maybe you can repeat the code from Form1_Load with corresponding adjustments and additions.


    Friday, August 17, 2018 12:40 PM
  • Dear Friend

    What i ask

    i type 45 in textbox then

    combobox should display "siva"

    if i type 65 then

    combobox should display "vince"

    Thanks

    Anburose

    Saturday, August 18, 2018 5:39 AM
  • Dear Friend

    What i ask

    i type 45 in textbox then

    combobox should display "siva"

    if i type 65 then

    combobox should display "vince"

    Thanks

    Anburose

    That is not normal behavior to type one thing and get something else.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Saturday, August 18, 2018 10:35 AM
    Moderator
  • Dear Friend

    What i ask

    i type 45 in textbox then

    combobox should display "siva"

    if i type 65 then

    combobox should display "vince"

    Thanks

    Anburose

    Hi,

    Do you want this effect?

    code:

    Public Class Form1
        Dim comboSource As New Dictionary(Of Integer, String)()
        Dim comIndex As New Dictionary(Of String, Integer)()
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            comboSource.Add(25, "Anbu")
            comboSource.Add(20, "Rose")
            comboSource.Add(45, "Siva")
            comboSource.Add(75, "Srini")
            comboSource.Add(60, "Nazar")
            comboSource.Add(40, "Sakthi")
            comboSource.Add(65, "Vince")
            comIndex.Add("Anbu", 0)
            comIndex.Add("Rose", 1)
            comIndex.Add("Siva", 2)
            comIndex.Add("Srini", 3)
            comIndex.Add("Nazar", 4)
            comIndex.Add("Sakthi", 5)
            comIndex.Add("Vince", 6)
            ComboBox1.DataSource = New BindingSource(comboSource, Nothing)
            ComboBox1.DisplayMember = "Value"
            ComboBox1.ValueMember = "Key"
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim key As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of Integer, String)).Key
            Dim value As String = DirectCast(ComboBox1.SelectedItem, KeyValuePair(Of Integer, String)).Value
            MessageBox.Show(key & "   " & value)
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            ComboBox1.SelectedIndex = comIndex(comboSource(TextBox1.Text))
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 21, 2018 7:29 AM