Click here to Skip to main content
15,914,419 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Can anybody tell why the button click event doesn't occur when the shape of the button is changed form its regular shape. Like i have changed its shape from rectangle to Octagonal shape using paint event properties. I have added a button in the form and initialized the size, after that using paint event i have created octagonal shape of it. Here is the code that i have implemented:

C#
public Form1()
{
    InitializeComponent();  
    button1.Size = new Size(150, 130);
}

protected override void OnPaint(PaintEventArgs e)
{
     System.Drawing.Drawing2D.GraphicsPath g_path = new System.Drawing.Drawing2D.GraphicsPath();
     Point[] myarr = { new Point(10, 35), new Point(10, 95), new Point(30, 115), new Point(110, 115), new Point(130, 95), new Point(130, 35), new Point(110, 15), new Point(30, 15) };
     button1.BackColor = System.Drawing.Color.Green;
     button1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
     g_path.AddPolygon(myarr);
     button1.Region = new Region(g_path);
}

private void button1_Click(object sender, EventArgs e)
{
    MessageBox.Show("HI");
}
Posted
Updated 9-May-13 0:18am
v4

It doesn't stop working: it's just that the remaining region is pretty small unless you have enlarged the button from it's standard size, so you have to click in the right place.

But why are you doing that in the form OnPaint override? You do realize that that is done every time the form is painted, and you only need to do it once? Put it in the Form Load event handler instead...

Personally, I wouldn't do it this way at all - I would create a new class derived from Button (called HexButton perhaps?) to keep all that within the control itself. (And not have your big margins, either)
 
Share this answer
 
Comments
Jagadisha_Ingenious 9-May-13 6:43am    
Thanks for your valuable suggestion @Original griff i added the code in form_load event & its working fine..
OriginalGriff 9-May-13 6:53am    
You're welcome - but you should really look at doing it via a separate control: it is much better practice and reduces the complexity of your form code.
Try this:

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
System.Drawing.Drawing2D.GraphicsPath g_path = new System.Drawing.Drawing2D.GraphicsPath();
Point[] myarr = { new Point(10, 35), new Point(10, 95), new Point(30, 115), new Point(110, 115), new Point(130, 95), new Point(130, 35), new Point(110, 15), new Point(30, 15) };
button1.BackColor = System.Drawing.Color.Green;
button1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
g_path.AddPolygon(myarr);
button1.Region = new Region(g_path);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900