Skip to content Skip to sidebar Skip to footer

How To Show Hidden Text Boxes When Items Are Selected In A Combo Box

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 box

Solution 1:

enter image description here

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

Solution 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
});

Solution 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

Solution 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.


Post a Comment for "How To Show Hidden Text Boxes When Items Are Selected In A Combo Box"