gpt4 book ai didi

c# - 单击多个文本框的事件

转载 作者:行者123 更新时间:2023-11-30 13:28:44 28 4
gpt4 key购买 nike

所以我需要一种方法,当有人在 8x8 文本框网格中单击文本框时,他们单击的文本框中的文本会更改为某些内容。我的网格设置在一个名为 textboxes[,] 的变量中,因此如果您键入 textboxes[0,0],您将获得网格中的第一个框。截至目前,以我非常有限的知识,我有这个。

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

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

}
}

然后只要单击其中一个框,我就可以处理。如果你有更好的方法,我很想听听。我只是不知道如何访问被点击的框,主要是文本。希望我已经很好地解释了这一点。感谢所有的帮助!

-刘易斯

最佳答案

你的方法很好。你只需要在事件中定义一些额外的信息来处理它,如下:

我们可以定义一个类来存储文本框的位置:

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;
}
}

编辑:我对代码做了一些修改,请查看。

关于c# - 单击多个文本框的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4213328/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com