如何在 C# 中将 switch case 语句与组框结合使用?
我是 C# 新手,我正在尝试检查组框中七个单选按钮中的哪一个被选中。与“案例”相关的行给我带来了麻烦。我可以使用多个 if 语句使其工作,但我想知道 switch 语句是否也可以工作。感谢您的帮助!这是代码片段:
{
if (OptMaennlich.Checked == true)
{
double Grundumsatz = (double)NumGewicht.Value * 24;
switch (GrpAktivitaet)
{
case OptSchlafen.Checked == true
double PAL = 1.0;
break;
I am new to C# and I am trying to check which one of seven radio button within a group box is checked. The line with the "case" is giving me trouble. I am able to make it work using multiple if statements, but I would like to know if a switch statement would work too. Thanks for your help! Here is the code snippet:
{
if (OptMaennlich.Checked == true)
{
double Grundumsatz = (double)NumGewicht.Value * 24;
switch (GrpAktivitaet)
{
case OptSchlafen.Checked == true
double PAL = 1.0;
break;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您仍然遇到困难并且正在使用 Windows 窗体,我建议您在属性检查器中或在组框中每个单选按钮的后面代码中订阅 CheckedChanged 事件。打开 RadioButton 的 Text 属性是在事件中实现此目的的一种方法。
在您的初始化代码中:
OptSchlafen.CheckedChanged += SomeFunctionToCheckChangedState;
并在您的代码中调用一个函数:
当您单击单选按钮时,将触发此事件并调用该函数。
If you are still stuck and you are using Windows Forms, I would suggest subscribing to the CheckedChanged event either in the properties inspector or in the code behind for each of the radio buttons you have in your groupbox. Switching on the Text Property of the RadioButton is one way of achieving this in the event.
In your initialisation code:
OptSchlafen.CheckedChanged += SomeFunctionToCheckChangedState;
and in your code a function:
When you click the radio button this event will be fired and the function called.