我有一個程序逐行讀取文件,並將該字符串放在tableLayoutPanel中,但是如何爲tableLayoutPanel中的每個標籤創建一個eventHandler?C#自動生成EventHandler
這裏是我使用的代碼:
Label label = new Label();
label.Name = "MyNewLabel";
label.ForeColor = Color.Red;
label.Text = line;
tableLayoutPanel1.RowCount++;
tableLayoutPanel1.RowStyles.Add(new RowStyle());
tableLayoutPanel1.Controls.Add(label, 0, tableLayoutPanel1.RowCount + 1);
每個標籤,需要打開一個網頁,並鏈接必須是它自己的文字。
我已經嘗試過這樣的:
與
private void HandleClick(object sender, EventArgs e)
{
messageBox.Show("Hello World!");
}
這是行不通的。
新的問題:
主要問題是由傑伊·沃克解決了,但現在我有另一個問題。並非所有的標籤都可以使用eventHandler。下面是主要代碼:
string line;
System.IO.StreamReader file = new System.IO.StreamReader("research.dat");
while ((line = file.ReadLine()) != null)
{
Label label = new Label();
label.Name = "MyNewLabel";
label.ForeColor = Color.Red;
label.Text = line;
label.Click += HandleClick;
tableLayoutPanel1.RowCount++;
tableLayoutPanel1.RowStyles.Add(new RowStyle());
tableLayoutPanel1.Controls.Add(label, 0, tableLayoutPanel1.RowCount + 1);
}
結合:
private void HandleClick(object sender, EventArgs e)
{
((Control)sender).BackColor = Color.White;
}
有些標籤背景變爲白色,而相同那些沒有。
您要添加的標籤'tableLayoutPanel1',但是,你是循環'PANEL1 '控件(並且分配給'label'而不是'x',但是在下面的答案中被評論過)......是嗎? – Jcl 2013-03-03 16:49:28
你能否詳細說明一下「一些標籤背景變成了白色,而**相同的**則沒有」。 ** **是什麼? – 2013-03-04 00:00:24