Val Nolav
Val Nolav

Reputation: 902

Checkbox event handling

I have a MyControl class. Inside MyClass object there are textboxes and a checkbox.

I am trying to add a checkbox event handler, but it does not work. What can be the problem?

private List<MyControls> _myControls = new List<MyControls>();
MyControls mc = new MyControls();

public void CreateFormElements(int i, StringReader sr)
{
    ProductForm form2 = new ProductForm();
    form2.Visible = true;
    form2.Activate();
    String line = "";

    for (int n = 0; n < i; n++)
    {
        line = sr.ReadLine();
        mc = new MyControls();

        if (line.Length > 3)
        {
            String[] _line = line.Split(new char[] { '\t' });
            mc.SetY(30 + n * 20);
            mc.initElements(_line, n);
            _myControls.Add(mc);
            **mc.cb.CheckedChanged += cb_CheckedChanged;**
        }
    }
}


private void cb_CheckedChanged(Object sender, EventArgs e)
{
    string NameSet  = (sender as CheckBox).Name.Split(new char[]{'_'})[1];
    MessageBox.Show(NameSet);
}

Here is the code of MyControls Class:

class MyControls
{
    int x=5;
    int y=30;
    public CheckBox cb = new CheckBox();
    public TextBox tb1 = new TextBox();

    public TextBox tbSpecs = new TextBox();
    public TextBox tb3 = new TextBox();
    public TextBox tb4 = new TextBox();

    public void initElements(String[] name, int i)
    {
        cb.Width = 10;
        cb.Height = 10;
        cb.Name = "cb_" + i.ToString();
        cb.Location = new Point(x, y+5);
        cb.Checked = false;
        Form.ActiveForm.Controls.Add(cb);

        x += 15;

        tb1.Width = 50;
        tb1.Height = 20;
        tb1.Location = new Point(x, y);
        tb1.Name = "tb1_" + i.ToString();
        tb1.Text = name[0];
        Form.ActiveForm.Controls.Add(tb1);
        x += 60;

        tbSpecs.Width = 150;
        tbSpecs.Height = 20;
        tbSpecs.Name = "tb2_" + i.ToString();
        tbSpecs.Text = name[1];
        tbSpecs.Location = new Point(x, y);
        Form.ActiveForm.Controls.Add(tbSpecs);
        x += 160;

        tb3.Width = 40;
        tb3.Height = 20;
        tb3.Name = "tb3_" + i.ToString();
        tb3.Text = name[2];
        tb3.Location = new Point(x, y);
        Form.ActiveForm.Controls.Add(tb3);
        x += 50;

        tb4.Width = 450;
        tb4.Height = 20;
        tb4.Name = "tb4_" + i.ToString();
        tb4.Text = name[3];
        tb4.Location = new Point(x, y);
        Form.ActiveForm.Controls.Add(tb4);

        x = 0;
    }

    public int SetX(int X)
    {
        x = X;
        return x;
    }

    public int SetY(int Y)
    {
        y = Y;
        return y;
    }
}

Upvotes: 2

Views: 8251

Answers (2)

Joey
Joey

Reputation: 1790

A few points to check in CreateFormElements(int i, StringReader sr){...}:

Is i greater than zero? If not the loop will never run and the event handler will never get attached.

Is line.Length ever greater than zero? If not you'll never get inside the if-block and the handler won't get attached.

Upvotes: 0

Waqas Mehmood
Waqas Mehmood

Reputation: 135

mc.cb.CheckedChanged += new System.EventHandler(this.cb_CheckedChanged)

Upvotes: 3

Related Questions