Click Event for multiple text fields.

So, I need a way, when a person clicks on a text field in a grid of 8x8 text fields, the text in the text field on which they clicked changes to something. My grid is set up in a variable named textboxes[,], so if you type textboxes[0,0], you will get the first field in the grid. At the moment, with my very limited knowledge, I have it.

 for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {

                textboxes[i, j].Click += new EventHandler(textboxes_Click);

            }
        }

Then I can process every time one of the boxes is clicked. If you have a better way to do this, I would love to hear that. I just don't know how to access the box that was clicked, mostly text. I hope I explained it quite well. Thanks for the help!

-Lewis

+3
4

. :

:

public class GridIndex
{
    //stores the position of a textbox
    public int ipos { get; set; }
    public int jpos { get; set; }
}

:

for (int i = 0; i < 8; i++)
  for (int j = 0; j < 8; j++)
  {
    textboxes[i, j].Click += new System.EventHandler(this.textBox_Click);
    textboxes[i, j].Tag = new GridIndex() { ipos = i, jpos = j };
  }

:

    private void textBox_Click(object sender, EventArgs e)
    {
        TextBox textBox = sender as TextBox;

        if (textBox != null)
        {
            //Here your have the text of the clicked textbox
            string text = textBox.Text;
            //And here the X and Y position of the clicked textbox
            int ipos = (textBox.Tag as GridIndex).ipos;
            int jpos = (textBox.Tag as GridIndex).jpos;   
        }
    }

. , , .

+5

EventHandler sender as parameter. TextBox, .

+3

:

void Handler(object sender, EventArgs args)

TextBox, . * j, , TextBox, , .

+2

You can get the values ​​of the text field by writing the following code

TextBox txt = (TextBox) sender; string text = txt.Text.ToString (); MessageBox.show (text);

Hope this will be helpful for u

+2
source

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


All Articles