Why the line disappear?
-
Hi, I use the following code for drawing line on a panel, but after few seconds the line disappear. Can someone help with this?
Graphics formGraphics = panel1.CreateGraphics();
//p1 and p2 are well defined points.
formGraphics.DrawLine((Pens.Red, p1, p2);Thanks in advance. Lune
-
Hi, I use the following code for drawing line on a panel, but after few seconds the line disappear. Can someone help with this?
Graphics formGraphics = panel1.CreateGraphics();
//p1 and p2 are well defined points.
formGraphics.DrawLine((Pens.Red, p1, p2);Thanks in advance. Lune
Hi, that indicates you didn't paint it in the right way. there are several steps to correctly draw something so it becomes visible on the screen: 1. decide upon what object you want to draw; it normally is a Control (e.g. a Panel) or a Form itself. I prefer to add a Panel to a Form, then draw on the Panel. 2. create some variables (Rectangle, struct, class, whatever) that hold the parameters of your drawing. For a rectangle that could be top and left coordinate, and width+height, or just a Rectangle. etc. 3. create a Paint handler (either add your own paint handler to the Paint event, or override the OnPaint method) for that Panel, and do all your drawing in there, using the Graphics class and your variables. 4. when you want to change things, modify the variables and call Panel.Invalidate() or one of its overloads (for selective invalidation). 5. If you want to animate things, perform the move (step 4) inside the Tick handler of a Windows.Forms.Timer BTW: if you need to create some objects (Fonts, Pens, Brushes, ...) either keep them alive in class members (hence create them only once); or create them inside the Paint handler and don't forget to call Dispose() on them. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
-
Hi, that indicates you didn't paint it in the right way. there are several steps to correctly draw something so it becomes visible on the screen: 1. decide upon what object you want to draw; it normally is a Control (e.g. a Panel) or a Form itself. I prefer to add a Panel to a Form, then draw on the Panel. 2. create some variables (Rectangle, struct, class, whatever) that hold the parameters of your drawing. For a rectangle that could be top and left coordinate, and width+height, or just a Rectangle. etc. 3. create a Paint handler (either add your own paint handler to the Paint event, or override the OnPaint method) for that Panel, and do all your drawing in there, using the Graphics class and your variables. 4. when you want to change things, modify the variables and call Panel.Invalidate() or one of its overloads (for selective invalidation). 5. If you want to animate things, perform the move (step 4) inside the Tick handler of a Windows.Forms.Timer BTW: if you need to create some objects (Fonts, Pens, Brushes, ...) either keep them alive in class members (hence create them only once); or create them inside the Paint handler and don't forget to call Dispose() on them. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
And you have the nerve to suggest that I am verbose! :laugh:
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Hi, I use the following code for drawing line on a panel, but after few seconds the line disappear. Can someone help with this?
Graphics formGraphics = panel1.CreateGraphics();
//p1 and p2 are well defined points.
formGraphics.DrawLine((Pens.Red, p1, p2);Thanks in advance. Lune
-
Hi, that indicates you didn't paint it in the right way. there are several steps to correctly draw something so it becomes visible on the screen: 1. decide upon what object you want to draw; it normally is a Control (e.g. a Panel) or a Form itself. I prefer to add a Panel to a Form, then draw on the Panel. 2. create some variables (Rectangle, struct, class, whatever) that hold the parameters of your drawing. For a rectangle that could be top and left coordinate, and width+height, or just a Rectangle. etc. 3. create a Paint handler (either add your own paint handler to the Paint event, or override the OnPaint method) for that Panel, and do all your drawing in there, using the Graphics class and your variables. 4. when you want to change things, modify the variables and call Panel.Invalidate() or one of its overloads (for selective invalidation). 5. If you want to animate things, perform the move (step 4) inside the Tick handler of a Windows.Forms.Timer BTW: if you need to create some objects (Fonts, Pens, Brushes, ...) either keep them alive in class members (hence create them only once); or create them inside the Paint handler and don't forget to call Dispose() on them. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
Thanks, But do you mean that if I want a persistant line I should keep the point variables all along the program? and also you suggest that the line should be drawn in the OnPaint event but is it the only way? I need to create the line as a response to an other events, so what is the right way? Thanks,
-
And you have the nerve to suggest that I am verbose! :laugh:
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
Fortunately I do have a small collection of standard replies available, this is the one I give for all paint problems unless more specific symptoms ask for a more personalized answer. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
-
Thanks, But do you mean that if I want a persistant line I should keep the point variables all along the program? and also you suggest that the line should be drawn in the OnPaint event but is it the only way? I need to create the line as a response to an other events, so what is the right way? Thanks,
lune12 wrote:
what is the right way?
I just indicated the right way. if you want to change the drawing, then change the data and call Control.Invalidate() to request an automatic repaint. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
-
lune12 wrote:
what is the right way?
I just indicated the right way. if you want to change the drawing, then change the data and call Control.Invalidate() to request an automatic repaint. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
-
Thanks, it's working but isn't it a waist of time to redraw all the line each time that the paint event is invoked?
you could reduce the amount of work executed by taking into account the clipping data that is present in the PaintEventArgs/Graphics. IMO that is worthwhile only in special cases, like complex paintings that need frequent repaints (e.g. as in games, which probably get programmed quite differently anyhow). :)
Luc Pattyn [Forum Guidelines] [My Articles]
Avoiding unwanted divs (as in "articles needing approval") with the help of this FireFox add-in
-
CreateGraphics is temporary. Use e.Graphics and the Form.Paint event instead. You also appear to have a syntax error (two brackets in front of Pens.Red)