0
我正在使用c#處理Windows窗體應用程序項目,並試圖使用相同的事件操作來創建TextBox數組。我的意思是需要N個文本框(N與用戶分配不同),而所有「TextBox_TextChanged」事件都是相同的。如果你能幫助我,我會很感激。具有相同事件的文本框數組
我正在使用c#處理Windows窗體應用程序項目,並試圖使用相同的事件操作來創建TextBox數組。我的意思是需要N個文本框(N與用戶分配不同),而所有「TextBox_TextChanged」事件都是相同的。如果你能幫助我,我會很感激。具有相同事件的文本框數組
請試試這個。
private void frmMain_Load(object sender, EventArgs e)
{
int userPermittedCount = 4 // You can add user defined permission no : of text box count here;
int pointX = 30;
int pointY = 40;
for (int i = 0; i < userPermittedCount; i++)
{
TextBox txtBox = new TextBox();
txtBox.Location = new Point(pointX, pointY);
this.Controls.Add(txtBox);
this.Show();
pointY += 20;
txtBox.TextChanged += txtAdd_TextChanged;
}
}
private void txtAdd_TextChanged(object sender, EventArgs e)
{
}
查找下面
TextBox t1 = new TextBox();
TextBox t2 = new TextBox();
TextBox t3 = new TextBox();
TextBox t4 = new TextBox();
TextBox t5 = new TextBox();
TextBox t6 = new TextBox();
private void Form1_Load(object sender, EventArgs e)
{
TextBox[] tBoxes = { t1, t2, t3, t4, t5, t6 };
foreach (TextBox item in tBoxes)
{
item.TextChanged += text_Changed;
}
}
private void text_Changed(object sender, EventArgs e)
{
}
'ArrayOfTextBoxes.ToList()的ForEach(TXT => txt.TextChanged + = TextBox_TextChanged);' – Fabio