Lew*_*III 3 c# text textbox winforms
所以我需要一种方法,当一个人点击8x8文本框网格中的文本框时,他们点击的文本框中的文本将更改为某些内容.我的网格设置在一个名为textboxes[,]so 的变量中,如果你键入,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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后,只要单击其中一个框,我就可以处理.如果你有更好的方法,我很乐意听到它.我只是不知道如何访问被点击的框,主要是文本.希望我已经解释得这么好了.感谢您的帮助!
-Lewis
你的方法很好.您只需定义一些其他信息即可在事件中处理它,如下所示:
我们可以定义一个类来存储文本框位置:
public class GridIndex
{
//stores the position of a textbox
public int ipos { get; set; }
public int jpos { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
你的代码片段被修改了:
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 };
}
Run Code Online (Sandbox Code Playgroud)
然后你的处理程序:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:我对代码做了一些更改,请查看.
| 归档时间: |
|
| 查看次数: |
1486 次 |
| 最近记录: |