检查选中哪个单选按钮的方法始终返回null [英] Method to check which Radio button is checked always returns null

查看:88
本文介绍了检查选中哪个单选按钮的方法始终返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在Form类中有一个方法来检查GroupBox中的哪个RadioButtons被选中,从而返回一个字符串。



然后我创建一个实例在这样的另一个类中形成



So I have a method in my Form class to check which RadioButtons in a GroupBox are selected, and thus return a string.

I then create an instance of Form in another class like this

Form1 form = new Form1();







然后我使用该对象从表单类调用我的方法






I then use that object to call my method from the form class

form.whatChecked();





这个表单类中的方法





This is my method in the form class

public String whatChecked()
        {
            if (a1.Checked)
            {
                return "a1";
            }
            else if (a2.Checked)
            {
                return "a2";
            }
            else if (a2.Checked)
            {
                return "a3";
            }
            else if (a2.Checked)
            {
                return "a4";
            }
            else if (a2.Checked)
            {
                return "a5";
            }
            else if (a2.Checked)
            {
                return "a6";
            }
            else
            {
                return null;
            }
        }





我在另一堂课上检查了一下,我发现它总是返回else值(在本例中为Null)。我已经测试过确保让else返回一个String,并确实返回,即使检查了a1或a2。



I've made a check in the other class, and I see that it always returns the "else" value (In this case, Null). I've tested to make sure by having the else return a String, and it does indeed return, even though a1 or a2 are checked.

推荐答案

你似乎检查在大多数情况下,相同的单选按钮。代码应该是这样的:

You seem to check the same radio button in most of the cases. Should the code look like:
public String whatChecked()
        {
            if (a1.Checked)
            {
                return "a1";
            }
            else if (a2.Checked)
            {
                return "a2";
            }
            else if (a3.Checked)
            {
                return "a3";
            }
            else if (a4.Checked)
            {
                return "a4";
            }
            else if (a5.Checked)
            {
                return "a5";
            }
            else if (a6.Checked)
            {
                return "a6";
            }
            else
            {
                return null;
            }
        }





增加:





ADDED:

private void radioButtons_CheckedChanged(Object sender, EventArgs e)
{
   if (a1.Checked))
   {
      // here execute the code what needs to be done when a1 is selected
      // in the same event handler also add code for other radio buttons since 
      // this event handler will be used for all radio buttons
   }
}





在Form_Load事件或构造函数中,连接所有复选框以使用相同的事件处理程序





And in the Form_Load event or constructor, wire all the check boxes to use the same event handler

this.a1.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.a2.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.a3.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
this.a4.CheckedChanged += new System.EventHandler(this.radioButtons_CheckedChanged);
...


这段代码我从Mika发布的解决方案中删除了一点点(我希望你能原谅我Mika )。



This code I have taken from the posted Solution from Mika to change it a little bit (I hope you will forgive me Mika).

private void radioButtons_CheckedChanged(Object sender, EventArgs e)
{
   RadioButton mySender = sender ;
   if (mySender.Checked) && (mySender.Name="A1")
   {
      // here execute the code what needs to be done when a1 is selected
      // in the same event handler also add code for other radio buttons since 
      // this event handler will be used for all radio buttons
   }
}





这也可能如下所示:



this could also look like this :

private void radioButtons_CheckedChanged(Object sender, EventArgs e)
{
	if (! object.ReferenceEquals(Sender.gettype, typeof(radiobutton))) return;  // if incomming object is not radiobutton


radiobutton mySender = Sender;
if (mySender.Checked) 
   {

   switch (mySender.name) {
	case "a1":
                // do the Action here
		break;
	case "a2":
                // do the Action here
		break;
      }
   }
}


这篇关于检查选中哪个单选按钮的方法始终返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆