tags:

views:

90

answers:

3

I create array:

TextBox[] textarray = new TextBox[100];

Then in cycle set this params, all items array situated in uniformGrid1

textarray[i] = new TextBox();
        textarray[i].Height = 30;
        textarray[i].Width = 50;
        uniformGrid1.Children.Add(textarray[i]);

How create events Click or DoubleClick that all items array?
Sorry my English.

+2  A: 

Just add in your click or double click event handler. For example, to trap double click events:

textarray[i] = new TextBox();
textarray[i].Height = 30;
textarray[i].Width = 50;
textarray[i].MouseDoubleClick += this.OnMouseDoubleClick;

uniformGrid1.Children.Add(textarray[i]);

For the above to work, you class will need a method like:

void OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    // Do something
}
Reed Copsey
thanks Reed Copsey
ScarX
+1  A: 
 public static void blah()
        {
            TextBox[] textarray = new TextBox[100];
            List<TextBox> textBoxList = new List<TextBox>();
            for (int i = 0; i < textarray.Length; i++)
            {
                textarray[i] = new TextBox();
                textarray[i].Height = 30;
                textarray[i].Width = 50;

                // events registration
                textarray[i].Click += 
                      new EventHandler(TextBoxFromArray_Click);
                textarray[i].DoubleClick += 
                      new EventHandler(TextBoxFromArray_DoubleClick);
            }
        }

        static void TextBoxFromArray_Click(object sender, EventArgs e)
        {
            // implement Event OnClick Here
        }

        static void TextBoxFromArray_DoubleClick(object sender, EventArgs e)
        {
            // implement Event OnDoubleClick Here
        }

EDIT:

A better / recommended way of event registration as per @Aaronaugh: advice:

textarray[i].Click += TextBoxFromArray_Click;
textarray[i].DoubleClick += TextBoxFromArray_DoubleClick;
Asad Butt
You don't need the `new EventHandler` constructor, you can add `TextBoxFromArray_Click` directly, and that's actually the recommended way of doing this now. Creating a new delegate for each item makes it more difficult to unsubscribe if that's ever a requirement.
Aaronaught
@Aaronaught, interesting. Thanks for the heads up.
Anthony Pegram
thanks Asad Butt
ScarX
@Aaronaugh: Thanks mate, Have Edited as per your recommendations
Asad Butt
@Asad: Looks like your code formatting got messed up on that last edit.
Aaronaught
A: 

Create a click event handler and then use it to subscribe to the click events of your textboxes, like so:

textarray[i].Click += new EventHandler(textbox_Click);

...

void textbox_Click(object sender, EventArgs e)
{
    // do something 
}

If the actions you want to take are the same for each textbox, then one click handler will suffice.

Anthony Pegram
thanks Anthony Pegram
ScarX