简体   繁体   English

在组合框中选择项目时如何显示隐藏的文本框

[英]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 boxes should become visible ie if "one player" get selected, one text box should appear so user can enter the value. 我有一个带有5个不同选项的组合框,“一个玩家”,“两个玩家”,“三个玩家”等。我的要求是,当用户从组合框中选择某些内容时,其等效文本框应可见,即如果“一个玩家”得到选中后,将出现一个文本框,以便用户输入值。 Same for two and three. 两个和三个相同。 Please suggest. 请提出建议。

在此处输入图片说明

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

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 ) 只需将jquery( https://api.jquery.com/change/ )中可用的.change()函数与CSS可见性(visibility:hidden / visible;请检查http://www.w3schools.com/cssref/pr_class_visibility)结合使用。 asp

$( "#myComboBox" ).change(function() {
  //do what you have to do here
});

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

The TextBox1.Visible = (ComboBox1.SelectedIndex = 0) works perfectly. TextBox1.Visible = (ComboBox1.SelectedIndex = 0)完美地工作。

You must also make sure to set Visible under Properties to False for your textboxes and labels. 您还必须确保将文本框和标签的“属性”下的“可见”设置为False。 Otherwise you textboxes will initially be visible when you run application. 否则,当您运行应用程序时,您的文本框最初将是可见的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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