How to create processing events for a TextBox array

I create an array:

TextBox[] textarray = new TextBox[100];

Then, in a loop, set these parameters, all array elements located in uniform Grid1

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

How to create events Click or double-click to the entire array of elements?
Sorry, my English.

+3
source share
3 answers
 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:

Best / recommended way to log events by @Aaronaugh : tip:

textarray[i].Click += TextBoxFromArray_Click;
textarray[i].DoubleClick += TextBoxFromArray_DoubleClick;
+2
source

Just add a click or double click event handler. For example, to block 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]);

In order for the above work to work, you need a class, for example:

void OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    // Do something
}
+2
source

, , :

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 text field, then one click handler is enough.

0
source

Source: https://habr.com/ru/post/1737938/


All Articles