简体   繁体   中英

change the value of istbox according to combobox in VB.net

I have a combobox and a listbox. When the user select an item from the combobox, then the listbox will show a certain data. Then when the user switch item from the combobox, then the listbox will show another set of data.

But when I change the value of combobox from "Monitor" to "Mouse", the data in the listbox is continued and not changed.

Here is my code. Thanks in advance.

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

        If ComboBox1.Text = "Monitor" Then
            ListBox1.Items.Add("Monitor 1")
            ListBox1.Items.Add("Monitor 2")
            ListBox1.Items.Add("Monitor 3")
        End If
        If ComboBox1.Text = "Mouse" Then
            ListBox1.Items.Add("Mouse 1")
            ListBox1.Items.Add("Mouse 2")
            ListBox1.Items.Add("Mouse 3")

        End If

Clear the ListBox before assigning the next values.

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

    ListBox1.Items.Clear()

    If ComboBox1.Text = "Monitor" Then ......

You need to use the VALUE. When you set the combo box you will have Text and a value.

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

    ListBox1.Items.Clear()
    If ComboBox1.value = "Monitor" Then
        ListBox1.Items.Add("Monitor 1")
        ListBox1.Items.Add("Monitor 2")
        ListBox1.Items.Add("Monitor 3")
    End If

    If ComboBox1.value = "Mouse" Then
        ListBox1.Items.Add("Mouse 1")
        ListBox1.Items.Add("Mouse 2")
        ListBox1.Items.Add("Mouse 3")

    End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM