【问题标题】:event of selecting a item in combobox在组合框中选择项目的事件
【发布时间】:2014-03-19 17:39:28
【问题描述】:

我在表单中有一个组合框和文本框,(在windows窗体平台中),默认情况下可见文本框为false,我想在选择组合框的特定项目时显示(可见=真)文本框。

combobox的哪个事件适合这项工作!

【问题讨论】:

  • ComboBox.SelectedIndexChanged?编辑:阅读 MSDN,似乎ComboBox.SelectionChangedCommited 可能会更好。但也许您应该指定您使用的平台(WinForms、WPF、Silverlight、ASP.NET)
  • Combobox.SelectionChanged 应该这样做。
  • 如果您希望它仅在 用户 选择某些内容时触发,您应该使用 SelectionChangeCommitted

标签: c# events combobox textbox visible


【解决方案1】:

如果您依赖组合框中项目中的固定索引,请使用 SelectedIndexChange 事件

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex == yourindex)
        textBox1.Visible = true; 
    else
        textBox1.Visible = false; 
}

如果您依赖组合框选定项的值,请使用 SelectedValueChanged 事件

private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedValue.ToString() == "yourvalue")
        textBox1.Visible = true;
    else
        textBox1.Visible = false; 
}

【讨论】:

    【解决方案2】:

    使用组合框 SelectedIndexChange 事件或 Selecton Change Committed 并在该事件中检查您的组合框的 selectedvalue,例如

              if(combobox1.SelectedValue == desiredvalue)
                   textBox1.Visible = true;
    

    【讨论】:

      【解决方案3】:

      本准则肯定会对您有所帮助。

      if (comboBox2.Text.ToString() == "Desired Value")
           comboBox1.Visible = true;
      else
           comboBox1.Visible = false;
      

      【讨论】:

        猜你喜欢
        • 2016-07-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-16
        • 2013-12-04
        • 1970-01-01
        • 2014-10-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多