我有一個方法名稱addDays,需要在月份的第一天以及該月有多少天。此方法將動態標籤添加到表格7x6中,該表格將表示每週每月的每一天。我還爲每個標籤添加了事件,以便用戶點擊它們。我遇到的問題是我需要能夠閱讀該人點擊的標籤。比方說,我運行應用程序填充我的日曆,我點擊本月的第一個,然後我想要能夠捕獲該標籤的文本將是1.
這是我的addDays方法的代碼。需要幫助從動態創建的標籤讀取標籤文本
private void addDays(int day,int totaldays)
{
int reset = 0;
Label newlabel;
string label;
int labelnum;
Console.WriteLine("testoutputbeforebig forloop");
//DayTableHold.Controls.Add(newlabel, 0, 6);
int numday = 0;
for (int coll = 0; coll <= 7; coll++)
{
for (int row = 0; row <=6; row++)
{
if (numday < totaldays)
{
newlabel = new Label();
newlabel.AutoSize = true;
if (row==day &&coll==0)
{
labelnum = numday + 1;
label = labelnum.ToString();
newlabel.Text = label;
newlabel.Margin = new System.Windows.Forms.Padding(17, 0, 10, 0);
newlabel.Click += new System.EventHandler(days_Click);
DayTableHold.Controls.Add(newlabel, row, coll);
numday++;
reset = 1;
if (row == 6)
{
coll = 1;
}
else coll = 0;
Console.WriteLine("testoutput1 " + numday + " " + label);
}
else if (reset == 1)
{
labelnum = numday + 1;
label = labelnum.ToString();
newlabel.Text = label;
newlabel.Margin = new System.Windows.Forms.Padding(17, 0, 10, 0);
newlabel.Click += new System.EventHandler(days_Click);
DayTableHold.Controls.Add(newlabel, row, coll);
numday++;
Console.WriteLine("test output2 " + numday + " " + label);
}
}
}
}
}
這是我的標籤事件處理程序單擊
private void days_Click(object sender, EventArgs e)
{
//here is were i want to capture the labels of the clicks.
count++;
Console.WriteLine("day was click"+ count);
} enter code here
你需要顯示在其中定義計數代碼++還創建例如公共委託無效事件處理程序的事件委託(對象發件人,EventArgs e);在那裏做這樣的事情this.label.Click + = new System.EventHandler(this.days_Click);你將不得不改變這個爲你的例子正確工作..也加上 - =爲相同的事件處理程序 – MethodMan 2011-12-21 14:33:06