0
我已經創建了一個按鈕,下面的代碼。在按鈕的左下角創建了一個標籤。代碼工作正常,但當用戶單擊標籤時按鈕沒有響應,請幫助。C#標籤按鈕
自定義按鈕:
public class CustomButton : System.Windows.Forms.Button
{
private System.Windows.Forms.Label lb = new System.Windows.Forms.Label();
public CustomButton(){
this.Width = 120;
this.Height = 65;
this.Font = new System.Drawing.Font(this.Font.FontFamily, 24);
lb = new System.Windows.Forms.Label();
lb.Font = new System.Drawing.Font(lb.Font.FontFamily, 9);
lb.Location = new System.Drawing.Point(4,35);
lb.TextAlign = System.Drawing.ContentAlignment.BottomLeft;
lb.BackColor = System.Drawing.Color.Transparent;
lb.Width = 70;
this.Controls.Add(lb);
}
public System.Windows.Forms.Label getLb()
{
return lb;
}
public System.Windows.Forms.Button get_btn()
{
return this;
}
}
的WinForm:
public CustomButton[,] bt = new CustomButton[13,32];
public FormClient()
{
bt[0, 0] = new CustomButton();
bt[0, 0].Click += new EventHandler(button_Click);
}
public void button_Click(object sender, EventArgs e)
{
//my code//
}
你不能將按鈕事件處理程序分配給標籤嗎? – EpicKip
只需添加一個eventHandler標籤上點擊您的CustomButton,這將引發按鈕點擊事件 – VDN
'lb.Width = 70; lb.Click + = this.Click; this.Controls.Add(lb);' – TaW