简体   繁体   中英

Combo box functionality

I have 4 combobox in my visual basic; ComboBox1 has 3 items: Vehicle, Motorbikes, None ComboBox2 has 4 items: sportbike, casual bikes and sportcar,casual cars ComboBox3 ComboBox4 I KINDLY need a code that will let me do the following:

  1. Make combobox 2,3,4 invisible until I make a selection on combobox 1 ie I will choose vehicle and later progress to select sportscar meanwhile combobox 3 and 4 are invisible. In short, the next combo box only appears after making a selection on the previous one.
  2. On combobox 1, if "none" is selected the other 2,3,4 remains invincible.

In your designer file, set the Visible property of 2, 3, and 4 to false.

Then in your code file use the following code:

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim showComboBox = Not String.IsNullOrWhitespace(ComboBox1.Text) AndAlso ComboBox1.Text <> "None"

    ComboBox2.Visible = showComboBox
    ComboBox3.Visible = showComboBox
    ComboBox4.Visible = showComboBox
End Sub    

What this does is set a Boolean variable equal to if the Text of ComboBox1 is not an empty string and not "None". It then sets the Visible property of the other ComboBox controls to the result.

On the forms Load event you need to set combo boxes 2,3 and 4 to visible=False. Then on the ComboBox1_SelectedIndexChanged event you can change combobox2 to visible=True and then do the same for each progression. The code is shown below. You will also need to decide how you want to reset previous box. In other words do you want to comboboxes 2, 3, and 4 to once again become invisible if you change from Motorbikes to none in combobox1?

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ComboBox2.Visible = False
    ComboBox3.Visible = False
    ComboBox4.Visible = False
End Sub

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedItem="None" then
    ComboBox2.Visible=False
    ComboBox3.Visible=False
    ComboBox4.Visible=False
Else
    ComboBox2.Visible = True
End If
End Sub
Private Sub ComboBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    ComboBox3.Visible = True
End Sub
Private Sub ComboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    ComboBox4.Visible = True
End Sub
End Class

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