C#标签插入按钮

C#标签插入按钮,c#,winforms,C#,Winforms,我用下面的代码创建了一个按钮。在按钮的左下角创建了一个标签。代码工作正常,但当用户单击标签时,按钮没有响应,请帮助 自定义按钮: public class CustomButton : System.Windows.Forms.Button { private System.Windows.Forms.Label lb = new System.Windows.Forms.Label(); public CustomButton(){ this.Width = 1

我用下面的代码创建了一个按钮。在按钮的左下角创建了一个标签。代码工作正常,但当用户单击标签时,按钮没有响应,请帮助

自定义按钮:

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//
}

只要单击标签,就可以调用按钮的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;
    }
}

现在,每次单击标签时,按钮的单击事件也将被触发。但是,当您将鼠标悬停在标签上时,将无法获得很好的动画效果,因此您必须创建一个自定义标签来实现此效果。

您不能将按钮事件处理程序分配给标签吗?只需在自定义按钮的标签单击上添加一个eventHandler,它将引发按钮单击事件
lb.Width=70;lb.单击+=此。单击;此.Controls.Add(lb)