【问题标题】:VS C# simple radio button switchVS C# 简单的单选按钮开关
【发布时间】:2021-11-26 12:06:36
【问题描述】:

我已尝试在此处搜索解决方案,但似乎没有可用的解决方案。这两个单选按钮位于一个组框中。

代码:

private void radioButton_CheckedChanged(object sender, EventArgs e)
        {

            RadioButton senderControl = sender as RadioButton;
            if (!senderControl.Checked)
                return;

            switch ((sender as RadioButton).Text)
            {
                case "radioButton1":
                    textBox4.Clear();
                    comboBox6.Enabled = false;
                    textBox4.ReadOnly = true;
                    textBox4.Enabled = false;
                    textBox4.Text = "000";
                    break;

                case "radioButton2":
                    textBox4.Clear();
                    comboBox6.Enabled = true;
                    textBox4.ReadOnly = false;
                    textBox4.Enabled = true;
                    textBox4.Text = "";
                    break;
            }                 
        }

它不想工作,因为它什么都不做

【问题讨论】:

    标签: c# radio-button


    【解决方案1】:

    我假设您的事件“radioChecked”被分配给一个单选按钮。您需要将两个单选按钮附加到同一个事件。您可以在代码或表单中这样做:

    public Form1()
            {
                InitializeComponent();
                radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
                radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            }
    

    但之后您需要转到 Form.Designer.cs 文件并删除将事件分配给 radioButtons 的代码行:

    this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged); // <-- delete
    

    另外,你的第一个条件if (!senderControl.Checked) 是不必要的,因为如果你点击单选按钮,那么它无论如何都会被选中。

    所以,这是你的一段代码:

    public Form1()
            {
                InitializeComponent();
                radioButton1.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
                radioButton2.CheckedChanged += new EventHandler(radioButton_CheckedChanged);
            }
    
    private void radioButton_CheckedChanged(object sender, EventArgs e)
            {
                RadioButton senderControl = sender as RadioButton;
                switch (senderControl.Text)
                {
                    case "radioButton1":
                        textBox4.Clear();
                        comboBox6.Enabled = false;
                        textBox4.ReadOnly = true;
                        textBox4.Enabled = false;
                        textBox4.Text = "000";
                        break;
    
                    case "radioButton2":
                        textBox4.Clear();
                        comboBox6.Enabled = true;
                        textBox4.ReadOnly = false;
                        textBox4.Enabled = true;
                        textBox4.Text = "";
                        break;
                }
            }
    

    【讨论】:

    • 我按照你说的做了,但没有区别,我不知道为什么。但是,我注意到我的 Form.Designer.cs(在我的情况下为 ToolingDesign.Designer.cs)中没有您写的那行
    • radioButton_CheckedChanged 事件的第一行使用断点,看看你是否进入并告诉我你是否这样做。
    • 很有趣,它进入了RadioButton senderControl = sender as RadioButton;switch (senderControl.Text),但从未进入case 本身,因此它永远不会因某种原因触发此案。单击单选按钮会触发RadioButton senderControl 并进入带有switch 的循环
    • 您可以在断点处停止时检查“senderControl”内部的内容。在那里您可以看到您按下的控件的 Text 属性是什么。因此,您可以调查您未输入任何案例条件的原因。
    • 天哪,我很笨。无论如何,案例名称应该是按钮的实际“文本”而不​​是名称(radioButton1),所以在我的例子中是“是”和“不”,它现在完美无缺。不知道我是怎么错过的,哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-18
    • 2015-01-05
    相关资源
    最近更新 更多