简体   繁体   English

如何将复选框/单选按钮的名称带入函数

[英]How to bring the name of the checkbox/radiobutton into the function

I using WPF and C# to build my application. 我使用WPF和C#构建应用程序。 Inside my application, i have quite lots of check boxes. 在我的应用程序内部,我有很多复选框。 Since i have lots of check boxes, there will be a LOTS of if checkbox.isChecked = true then do something....So, i need to create a function for the event whenever the check boxes is checked in order to make sure my coding is not flooded with the checkbox.isChecked. 由于我有很多复选框,因此会有很多如果checkbox.isChecked = true然后做某事....所以,每当选中复选框时,我都需要为该事件创建一个函数,以确保我编码不会被checkbox.isChecked淹没。

public Window1()
    {
        InitializeComponent();
        CheckTrueFlase(checkboxA);
        CheckTrueFlase(checkboxB);
        CheckTrueFlase(checkboxC);
        CheckTrueFlase(checkboxD);
    }

The function will be something like: 该函数将类似于:

private string CheckTrueFalse(/*What parameter i should write in this area?*/) 
{
   string x;

   if(checkbox.isChecked == true)
     x = "correct";
   else if (checkbox.isChecked == false)
     x = "wrong";

   return x;
 }

You can register the event of all checkboxs(form.controls) to the same method and sending the sender (converted to checkbox) to your method. 您可以将所有复选框(form.controls)的事件注册到相同的方法,并将发送方(转换为复选框)发送到您的方法。 I dont know wpf but I guess it is similar to winforms, this is an answer for winforms. 我不知道wpf,但我想它类似于winforms,这是winforms的答案。

This would do the trick. 这可以解决问题。

private string CheckTrueFalse(System.Windows.Controls.CheckBox checkbox) 
{
    string x;

    if (checkbox.isChecked)
        x = "correct";
    else
        x = "wrong";

    return x;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM