2017-08-08 100 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

它已经解决了..感谢:) –