2017-08-08 134 views
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// 
} 
+1

你不能將按鈕事件處理程序分配給標籤嗎? – EpicKip

+1

只需添加一個eventHandler標籤上點擊您的CustomButton,這將引發按鈕點擊事件 – VDN

+0

'lb.Width = 70; lb.Click + = this.Click; this.Controls.Add(lb);' – TaW

回答

0

你可以調用只要單擊標籤按鈕的onclick事件。因此,我想修改自定義按鈕,這樣的:

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; 
     lb.Click += (sender, args) => InvokeOnClick (this, args); //Add this line 
     this.Controls.Add (lb); 
    } 

    public System.Windows.Forms.Label getLb() 
    { 
     return lb; 
    } 

    public System.Windows.Forms.Button get_btn() 
    { 
     return this; 
    } 
} 

現在每次標籤被點擊,點擊按鈕事件將被解僱也是如此。但是,當您將鼠標懸停在標籤上時,您不會獲得那麼好的動畫,因此您必須創建一個自定義標籤來實現此效果。

+0

它已經解決了..感謝:) –