且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何在开关状态中使用单选按钮?

更新时间:2021-12-25 22:03:48

您可能没有考虑到以下事实:单选按钮的作用就像一个选择(例如,不同于复选框),如果它们被放置在相同的父控件中,例如面板,窗体(窗口)或组框.当选择一个,别人总是不选.这使代码更加简单,因为您不需要嵌套检查,甚至不需要else if.你有主意吗?

这使得编写单个case语句非常简单,因为一次只存在一种情况.

—SA
You probably don''t take into account the simple fact that the radio buttons work like a single choice (unlike check boxes, for example), if they are placed in the same parent control, like a panel, form (window) or a group box. When one is selected, others are always unselected. This makes the code much simpler, as you don''t need your nested checks and even else if. Are you getting the idea?

And that makes writing a single case statement really simple, because there is only one case at a time.

—SA


您需要做的所有单选按钮都是在属性下选择事件选项卡,然后在已检查"事件下选择radioButton_Click.我希望这对以后的人会有所帮助.


All you need to do with the radio button is select the event tab under properties and select radioButton_Click under the "Checked" event. I hope this will be helpful to someone in the future.


private void radioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton radioBtn = (RadioButton)sender;
            if (radioBtn.IsChecked == true)
            {
                switch (radioBtn.Name)
                {
                    case "radioBtnName1":
                        //do something
                        break;

                    case "radioBtnName2":
                        //do something
                        break;

                    case "radioBtnName3":
                        //do something
                        break;

                    case "radioBtnName4":
                        //do something
                        break;
                }

            }


        }