简体   繁体   English

将CheckBox或RadioButton传递给方法

[英]Passing a CheckBox or RadioButton to a method

Currently I am passing my CB and RD to the following methods. 目前,我正在将CB和RD传递给以下方法。

public void objEnabled(System.Windows.Forms.RadioButton x, bool enabled)
    {
        if (x.InvokeRequired)
        {
            BeginInvoke(new MyDelegate(delegate()
            {
                x.Checked = enabled;
            }));
        }
        else
        {
            x.Checked = enabled;
        }
    }

    public void objEnabled(System.Windows.Forms.CheckBox x, bool enabled)
    {
        if (x.InvokeRequired)
        {
            BeginInvoke(new MyDelegate(delegate()
            {
                x.Checked = enabled;
            }));
        }
        else
        {
            x.Checked = enabled;
        }
    }

But I would like to do something like the following. 但是我想做类似下面的事情。

public void objChecked(Control x, bool enabled)
    {
        if (x.InvokeRequired)
        {
            BeginInvoke(new MyDelegate(delegate()
            {
                x.Checked = enabled;
            }));
        }
        else
        {
            x.Checked = enabled;
        }
    }

I am able to use something similar for changing .Text, .Visible, and .Enable. 我可以使用类似的方法来更改.Text,.Visible和.Enable。 but .Checked is not part of the Controls class. 但是.Checked不属于Controls类。

The Checked property is not part of interface, so you can't have "common" functionality. Checked属性不是界面的一部分,因此您不能具有“通用”功能。 They are defined both seperately in RadioButton and CheckBox. 它们在RadioButton和CheckBox中分别定义。

If you would like to treat them as one, you can use dynamic keyword to let the runtime figure it out. 如果您想将它们视为一个,则可以使用dynamic关键字让运行时解决它。

public void objChecked(Control x, bool enabled)
    {
        if(x as RadioButton == null && x as CheckBox == null)
          throw new Exception("Not supported");


        dynamic runtimeObject = (dynamic)x;
        if (x.InvokeRequired)
        {
            BeginInvoke(new MyDelegate(delegate()
            {
                runtimeObject.Checked = enabled;
            }));
        }
        else
        {
            runtimeObject.Checked = enabled;
        }
    }

You'll need to check the type and cast it accordingly. 您需要检查类型并进行相应的转换。

if (x is System.Windows.Forms.RadioButton)
    (x as System.Windows.Forms.RadioButton).Checked = enabled;
else if (x is System.Windows.Forms.CheckBox)
    (x as System.Windows.Forms.CheckBox).Checked = enabled;

Cast your control x to the type you want: for example, (System.Windows.Forms.RadioButton)x will convert x from control to System.Windows.Forms.RadioButton . control x强制转换为所需的类型:例如, (System.Windows.Forms.RadioButton)x会将x从control转换为System.Windows.Forms.RadioButton Then you should be able to access its checked property. 然后,您应该能够访问其选中的属性。

I don't think System.Windows.Forms has a base class which contains the Checked property, so you'll need to cast your control to a specific implementation which has Checked . 我认为System.Windows.Forms没有包含Checked属性的基类,因此您需要将控件转换为具有Checked的特定实现。

Cast x as a radiobutton 将x投射为单选按钮

    public void objChecked(Control c, bool enabled)
    {
        RadioButton x = c as RadioButton;
        if(x == null)
            throw new ArgumentException();
        if (x.InvokeRequired)
        {
            BeginInvoke(new MyDelegate(delegate()
            {
                x .Checked = enabled;
            }));
        }
        else
        {
            x.Checked = enabled;
        }
    }

Just as a curiosity, not recommended - other possibility without casting: 出于好奇,不建议使用-其他无需铸造的可能性:

    public void objChecked(Control c, bool enabled)
    {
        Expression property
            = Expression.Property(Expression.Constant(c), "Checked");
        var lambda = Expression.Lambda<Action>(
                         Expression.Assign(
                             property,
                             Expression.Constant(enabled)))
                     .Compile();

         if (c.InvokeRequired)
         {
             BeginInvoke(new MyDelegate(delegate()
                 {
                     lambda.Invoke();
                 }));
         }
         else
         {
             lambda.Invoke();
         }
    }

Using Reflection. 使用反射。 Will work for any control with a boolean property named Checked . 适用于具有名为Checked的布尔属性的任何控件。

delegate void MyDelegate(object control, object enabled);
public void objChecked(Control x, bool enabled)
{
    System.Reflection.PropertyInfo pi = x.GetType().GetProperty("Checked");

    if (pi != null && pi.PropertyType == System.Type.GetType("System.Boolean"))
    {
        if (x.InvokeRequired)
            BeginInvoke(new MyDelegate(pi.SetValue), new object[] { x, enabled });
        else
            pi.SetValue(x, enabled);
    }
}

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

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