0

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.

Hard Disk
  • 49
  • 2
  • 8
  • Hint: [`is` or `typeof`](https://stackoverflow.com/questions/184681/is-vs-typeof). – HABO Jun 29 '17 at 03:19
  • You don't gain much by trying to do this with one block of code. But... Get rid of the array, make it `For (int i = 0; i < numberOfEmployees; i++)` then do your If blocks. – LarsTech Jun 29 '17 at 03:23

2 Answers2

2

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.

Tien Nguyen Ngoc
  • 1,555
  • 1
  • 8
  • 17
  • its not working. if (EmployeesControl[i].GetType() == typeof(RadioButton)) is null – Hard Disk Jun 29 '17 at 03:47
  • yes its working. but my problem is, the checkbox and radiobutton has similar properties. so its means i will copy and paste same code. – Hard Disk Jun 29 '17 at 04:32
  • ButtonBase is base class of both RadioButton and CheckBox . You can use change RadioButton and CheckBox to ButtonBase. After you can set properties. – Tien Nguyen Ngoc Jun 29 '17 at 04:40
0

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
}