问题
I have a combo box with 5 different options, "one player", "two players", "three players" etc. My requirement is when user select something from combo box their equivalent text boxes should become visible i.e. if "one player" get selected, one text box should appear so user can enter the value. Same for two and three. Please suggest.
回答1:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.Text.Trim.Contains("Player 1") = True Then
TextBox1.Visible = True
TextBox2.Visible = False
TextBox3.Visible = False
ElseIf ComboBox1.Text.Trim.Contains("Player 2") = True Then
TextBox1.Visible = True
TextBox2.Visible = True
TextBox3.Visible = False
ElseIf ComboBox1.Text.Trim.Contains("Player 3") = True Then
TextBox1.Visible = True
TextBox2.Visible = True
TextBox3.Visible = True
End If
End Sub
回答2:
Simply combine the .change() function available in jquery (https://api.jquery.com/change/) with css visibility (visibility:hidden/visible; check http://www.w3schools.com/cssref/pr_class_visibility.asp )
$( "#myComboBox" ).change(function() {
//do what you have to do here
});
回答3:
Try something like this:
Sub cbC(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Select Case ComboBox1.SelectedIndex
Case 0
TextBox1.Visible = False
TextBox2.Visible = False
Case 1
TextBox1.Visible = True
TextBox2.Visible = False
Case 2
TextBox1.Visible = True
TextBox2.Visible = True
End Select
End Sub
Or this, according to what fits better to your needs:
Sub cbC(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
TextBox1.Visible = (ComboBox1.SelectedIndex = 0)
TextBox1.Visible = (ComboBox1.SelectedIndex = 1)
End Sub
回答4:
The TextBox1.Visible = (ComboBox1.SelectedIndex = 0)
works perfectly.
You must also make sure to set Visible under Properties to False for your textboxes and labels. Otherwise you textboxes will initially be visible when you run application.
来源:https://stackoverflow.com/questions/23750879/how-to-show-hidden-text-boxes-when-items-are-selected-in-a-combo-box