Reputation: 53
I have created a button with below code.There is a label created on the bottom left corner of the button.The code works fine but the button has no response when the user clicks on the label, please help.
Custom Button:
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//
}
Upvotes: 1
Views: 5238
Reputation: 2871
You could invoke the onClick event of the button whenever the label is clicked. Therefore I'd modify your custom button like that:
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;
}
}
Now everytime the label is clicked, the click event of the button will get fired as well. However, you won't get that nice animation when you hover over your label, therefore you'd have to create a custom label which makes this effect.
Upvotes: 1