BOOL FLAG Problem in Paint Handler
-
I have code that will draw a line on a form. When I hard code it to draw the line it works fine, meaning every time the form launches a line is drawn. Now i want to set a bool flag and pass it in as a conditional statement to drawn the line when the Button1 is clicked. I know I'm missing something obvious. public class Form1 : System.Windows.Forms.Form { //Member fields. private bool drawLine1 = false;..............etc /////////////////////////////////////////////////////////////////////////// private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { if(drawLine1) //DRAW LINE 1 IS ALWAYS FALSE..... DrawComplexNumber(e.Graphics);.............etc ////////////////////////////////////////////////////////////////////////// private void Button_1OK_Click(object sender, System.EventArgs e) { drawLine1 = !drawLine1; Invalidate(); } How do I fix it? Please -- need a "nuts and bolts" answer, not an "abstract" explanation. Thanks a lot in advance....
-
I have code that will draw a line on a form. When I hard code it to draw the line it works fine, meaning every time the form launches a line is drawn. Now i want to set a bool flag and pass it in as a conditional statement to drawn the line when the Button1 is clicked. I know I'm missing something obvious. public class Form1 : System.Windows.Forms.Form { //Member fields. private bool drawLine1 = false;..............etc /////////////////////////////////////////////////////////////////////////// private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { if(drawLine1) //DRAW LINE 1 IS ALWAYS FALSE..... DrawComplexNumber(e.Graphics);.............etc ////////////////////////////////////////////////////////////////////////// private void Button_1OK_Click(object sender, System.EventArgs e) { drawLine1 = !drawLine1; Invalidate(); } How do I fix it? Please -- need a "nuts and bolts" answer, not an "abstract" explanation. Thanks a lot in advance....
The code seems to be ok but I noticed that your button click eventhandler has a strange name. You should probably check if the click event of the button is really bound to that method. Just check (eg by setting a brakepoint) if the
drawLine1 = !drawLine1
is being hit and if drawLine1 has the correct contents afterwards. Then check if you prpbably set drawLine1 back to false somewhere else in your code. Doing someConsole.WriteLine("DrawLine1: " + drawLine1.ToString())
here and there should also help you to trace down the problem. Hope you don't feel this answer is abstract but if you test this code in an empty form you will see that it works and that the error must come from somewhere else. -
The code seems to be ok but I noticed that your button click eventhandler has a strange name. You should probably check if the click event of the button is really bound to that method. Just check (eg by setting a brakepoint) if the
drawLine1 = !drawLine1
is being hit and if drawLine1 has the correct contents afterwards. Then check if you prpbably set drawLine1 back to false somewhere else in your code. Doing someConsole.WriteLine("DrawLine1: " + drawLine1.ToString())
here and there should also help you to trace down the problem. Hope you don't feel this answer is abstract but if you test this code in an empty form you will see that it works and that the error must come from somewhere else. -
I have code that will draw a line on a form. When I hard code it to draw the line it works fine, meaning every time the form launches a line is drawn. Now i want to set a bool flag and pass it in as a conditional statement to drawn the line when the Button1 is clicked. I know I'm missing something obvious. public class Form1 : System.Windows.Forms.Form { //Member fields. private bool drawLine1 = false;..............etc /////////////////////////////////////////////////////////////////////////// private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { if(drawLine1) //DRAW LINE 1 IS ALWAYS FALSE..... DrawComplexNumber(e.Graphics);.............etc ////////////////////////////////////////////////////////////////////////// private void Button_1OK_Click(object sender, System.EventArgs e) { drawLine1 = !drawLine1; Invalidate(); } How do I fix it? Please -- need a "nuts and bolts" answer, not an "abstract" explanation. Thanks a lot in advance....
Try out this code as an example -
public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.Button button1; private bool drawline = false; private int radius = 25; private int green = 10; private int red = 10; private int blue = 10; } private void Button_1OK__Click(object sender, System.EventArgs e) { drawline = !drawline; Random r = new Random(100); Int32 x = Int32.Parse(r.Next().ToString().Substring(1,2)); Int32 y = Int32.Parse(r.Next().ToString().Substring(1,3)); if (green < 230) green += 10; if (blue < 230) blue += 10; if (red < 230) red += 10; if (drawline) { System.Drawing.Graphics g = this.CreateGraphics(); Point P = new Point(x,100); Point p1 = new Point (100,y); Color j = Color.FromArgb(red,green,blue); Pen Pe = new Pen(j); g.DrawEllipse(Pe,x,x ,radius, radius); radius += 20; } else { System.Drawing.Graphics g = this.CreateGraphics(); Point P = new Point(x,100); Point p1 = new Point (100,y); Color j = Color.FromArgb(red,green,blue); Pen Pe = new Pen(j); g.DrawEllipse(Pe,y,y ,radius, radius); radius += 20; this.Refresh(); } }
Regards Mahesh