Hard Disk
Hard Disk

Reputation: 49

If Control is Radiobutton or Textbox

I have a form which changes Title depends on users selection. Then i want to create a properties for the control, either radiobutton or checkbox. They have same properties but i cant implement well.

 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;
        }
    }

How can i make a variable if a certain control is a radiobutton or checkbox.

Upvotes: 0

Views: 172

Answers (2)

user8129215
user8129215

Reputation:

You can use ButtonBase class to your perpouse.

Declare array of controls by ButtonBase, so common properties of CheckBox and RadioButton you can use without casting.

Properties of CheckBox or RadioButton you can access by

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
}

Upvotes: 0

Tien Nguyen Ngoc
Tien Nguyen Ngoc

Reputation: 1555

You can use GetType to check, after you will change EmployeesControl[i] to CheckBox or 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;                      
    }
}

I hope it will help you.

Upvotes: 2

Related Questions