【问题标题】:If Control is Radiobutton or Textbox如果 Control 是 Radiobutton 或 Textbox
【发布时间】:2017-12-02 14:26:03
【问题描述】:

我有一个根据用户选择更改标题的表单。然后我想为控件创建一个属性,单选按钮或复选框。它们具有相同的属性,但我无法很好地实现。

 private void DeleteEdit()
      {
        Control[] EmployeesControl; //I think this is the problem, but i cant figure it out.

        if (Form_AddEmployee.Text.Contains("Edit"))
        {
            EmployeesControl = new RadioButton[numberOfEmployees];
        }
        else if (Form_AddEmployee.Text.Contains("Delete"))
        {
            EmployeesControl = new CheckBox[numberOfEmployees];
        }

        for (int i = 0; i < EmployeesControl.Count(); i++)
        {
            EmployeesControl[i] = new EmployeesControl();
            InitializeControls(EmployeesControl[i]);
            EmployeesControl[i].Visible = true;
            panelEmployee.Controls.Add(EmployeesControl[i]);
            EmployeesControl[i].Text = stringTemp;
            EmployeesControl[i].Location = new Point(100, 100 * (i+1));
            EmployeesControl[i].Font = MyFont;
            EmployeesControl[i].CheckedChanged += EmployeesDeleteEditEmployees_CheckedChanged;
        }
    }

如果某个控件是单选按钮或复选框,我如何创建变量。

【问题讨论】:

  • 提示:is or typeof.
  • 尝试用一个代码块来做这件事并没有什么收获。但是...摆脱数组,将其设为For (int i = 0; i &lt; numberOfEmployees; i++),然后执行您的 If 块。

标签: c# winforms


【解决方案1】:

您可以使用GetType 进行检查,之后将EmployeesControl[i] 更改为CheckBox 或RadioButton。

private void DeleteEdit()
{
    Control[] EmployeesControl; //I think this is the problem, but i cant figure it out.

    if (Form_AddEmployee.Text.Contains("Edit"))
        {
            EmployeesControl = new RadioButton[numberOfEmployees];
            for (int i = 0; i < EmployeesControl.Count(); i++)
            {
                EmployeesControl[i] = new RadioButton();
            }
        }
        else if (Form_AddEmployee.Text.Contains("Delete"))
        {
            EmployeesControl = new CheckBox[numberOfEmployees];
            for (int i = 0; i < EmployeesControl.Count(); i++)
            {
                EmployeesControl[i] = new CheckBox();
            }
        }
    ButtonBase b;
    CheckBox chk;
    RadioButton rdo;
    for (int i = 0; i < EmployeesControl.Count(); i++)
    {
        b = (ButtonBase)EmployeesControl[i];//You use b to set property
        if (EmployeesControl[i].GetType() == typeof(RadioButton))
        {
            rdo = (RadioButton)EmployeesControl[i];

            //Your code
            //................
            rdo.CheckedChanged += EmployeesDeleteEditEmployees_CheckedChanged;                    
        }
        else
        {
            chk = (RadioButton)EmployeesControl[i];

            //Your code
            //...............
            chk.CheckedChanged += EmployeesDeleteEditEmployees_CheckedChanged;
        }

        //EmployeesControl[i] = new EmployeesControl();
        //InitializeControls(EmployeesControl[i]);
        //EmployeesControl[i].Visible = true;
        //panelEmployee.Controls.Add(EmployeesControl[i]);
        //EmployeesControl[i].Text = stringTemp;
        //EmployeesControl[i].Location = new Point(100, 100 * (i + 1));
        //EmployeesControl[i].Font = MyFont;
        //EmployeesControl[i].CheckedChanged += EmployeesDeleteEditEmployees_CheckedChanged;                      
    }
}

希望对你有帮助。

【讨论】:

  • 它不工作。 if (EmployeesControl[i].GetType() == typeof(RadioButton)) 为空
  • 是的,它的工作原理。但我的问题是,复选框和单选按钮具有相似的属性。所以这意味着我将复制并粘贴相同的代码。
  • ButtonBase 是 RadioButton 和 CheckBox 的基类。您可以使用将 RadioButton 和 CheckBox 更改为 ButtonBase。之后就可以设置属性了。
【解决方案2】:

您可以根据自己的喜好使用 ButtonBase 类。

通过 ButtonBase 声明控件数组,因此 CheckBox 和 RadioButton 的常用属性可以在不强制转换的情况下使用。

您可以通过以下方式访问的 CheckBox 或 RadioButton 的属性

var a = EmployeesControl[i] as CheckBox;
if (a != null)
{
    //this is checkbox
    continue;
}
var b = EmployeesControl[i] as RadioButton;
if (b != null)
{
    //this is RadioButton
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多