tags:

views:

71

answers:

3

If i create a control on the fly, as below

private void button10_Click(object sender, EventArgs e)
{
    CheckedListBox CheckedListBox1 = new CheckedListBox();
    CheckedListBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
    CheckedListBox1.ItemHeight = 16;
    CheckedListBox1.Location = new System.Drawing.Point(12, 313);
    CheckedListBox1.Name = "CheckedListBox1";
    CheckedListBox1.Size = new System.Drawing.Size(168, 244);
    CheckedListBox1.TabIndex = 0;
    Controls.Add(CheckedListBox1);

    Button button12 = new Button();
    button12.Location = new Point(900, 500);
    button12.Size = new Size(75, 23);
    button12.Click += new System.EventHandler(button12_Click);
    button12.Name = "button12";
    button12.Text = "Toggle All";
    Controls.Add(button12);
}

what is the best way to reference that control from a function outside of the local scope? would it be best creating a static class to somehow hold a reference to the control that can be accessed outside the local scope or is there a findcontrol function for winforms ( i think findcontrol is just for web).

i want

private void button12_Click(object sender, EventArgs e)
{
    for (int i = 0; i <= (CheckedListBox1.Items.Count - 1); i++)
    {
        if (CheckedListBox1.GetItemCheckState(i) == CheckState.Checked)
        {
            CheckedListBox1.SetItemCheckState(i, CheckState.Indeterminate);
        }
        else if (CheckedListBox1.GetItemCheckState(i) == CheckState.Indeterminate)
        {
            CheckedListBox1.SetItemCheckState(i, CheckState.Checked);
        }
    }
}

to be able to work but im going wrong because of scope? pls help a newbie

thanks

A: 

When there is only one CheckedListBox make it a class variable. But when you have always only one CheckedListBox - why do you create it dynamically?

tanascius
+1  A: 

I'm assuming the two functions *button12_Click* and *button10_Click* are members of a From class. In this case, your should make your CheckListBox1 and button12 members of the From class. That way, the button12_Click will be able to reference the controls you will have created.

 public partial class Form1 : Form
 {
      CheckedListBox CheckedListBox1 = null;
      Button button12 = null;


      private void button10_Click(object sender, EventArgs e) 
      {
        CheckedListBox1 = new CheckedListBox();
        CheckedListBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(255)), ((System.Byte)(224)), ((System.Byte)(192)));
        CheckedListBox1.ItemHeight = 16;
        CheckedListBox1.Location = new System.Drawing.Point(12, 313);
        CheckedListBox1.Name = "CheckedListBox1";
        CheckedListBox1.Size = new System.Drawing.Size(168, 244);
        CheckedListBox1.TabIndex = 0;
        Controls.Add(CheckedListBox1);

        button12 = new Button();
        button12.Location = new Point(900, 500);
        button12.Size = new Size(75, 23);
        button12.Click += new System.EventHandler(button12_Click);
        button12.Name = "button12";
        button12.Text = "Toggle All";
        Controls.Add(button12);
    }

    private void button12_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= (CheckedListBox1.Items.Count - 1); i++)
        {
            if (CheckedListBox1.GetItemCheckState(i) == CheckState.Checked)
            {
                CheckedListBox1.SetItemCheckState(i, CheckState.Indeterminate);
            }
            else if (CheckedListBox1.GetItemCheckState(i) == CheckState.Indeterminate)
            {
                CheckedListBox1.SetItemCheckState(i, CheckState.Checked);
            }
        }
    }

 }
Miky Dinescu
thanks mate seems so simple, sometimes you just need someone elses eyes -- cheers
Gazza
you're welcome..
Miky Dinescu
A: 

If you're adding the controls to the page's Controls collection, just go look there. If you know the index of the control you can reference it that way. if you're adding the control to some container's Control's collection (say, a panel), look for it there

Jason