【问题标题】:Clear ComboBox if RadioButton is deactivated如果 RadioButton 已停用,则清除 ComboBox
【发布时间】:2017-11-08 16:39:01
【问题描述】:

我有五个RadioButton 和五个ComboBox 控件。
每个RadioButton 都连接到一个ComboBox。

当我激活一个RadioButton,对应的ComboBox,它就会被启用。 现在当我选择另一个RadioButton 时,之前选择的ComboBox 中的信息应该会清除但不会清除!

我尝试过使用ComboBox.Clear() 和ComboBox.Reset(),但它不起作用。

这是我的ComboBox 和RadioButton 之一的代码

if (radioButtondinner.Checked == true)
        {
            comboBoxdinner.DataSource = DList.Dwork();
            comboBoxdinner.DisplayMember = "dinner";
        }

【问题讨论】:

  • 代码总是有帮助的。
  • 能否请您发布您的代码
  • 如果Checked == false会发生什么?
  • @mo.wl 请在您尝试清除它们时显示代码,可能是有错字或其中有什么东西阻止了它的工作。但是,如果我们没有看到您尝试过的确切代码,那就有点猜测工作了。你检查这个的完整方法也会很好。您是否对事件单选按钮更改或其他内容进行检查?

标签: c# winforms combobox radio-button


【解决方案1】:

正如我在评论中所说:您可以使用一个Combobox,并且仅在您检查其他应该可以正常工作的RadioButton 时更改数据源 但如果你想拥有更多Combobox,那么只需输入else 语句

comboBox.DataSource = null;

【讨论】:

    【解决方案2】:

    // 创建一个检查更改事件并使用它。

    private void radioButtondinner_CheckedChanged(object sender, EventArgs e)
            {
                if (!radioButtondinner.Checked)
                {
                    // if you want to clear only the text or selected item text
                    comboBoxdinner.Text = String.Empty;
                    // if you want to clear the entire data source
                    comboBoxdinner.DataSource = null;
                }
    
            }
    

    【讨论】:

      猜你喜欢
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-25
      • 1970-01-01
      • 2016-11-07
      • 1970-01-01
      • 2014-06-25
      相关资源
      最近更新 更多