Things are not working as expected
-
hey guys, can any1 please tell me wtf is wrong with the following code :((:
private void panelRightView_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
System.Drawing.Graphics oGfx = this.panelRightView.CreateGraphics();
System.Drawing.Rectangle rect = this.picTitleBar.Bounds;
oGfx.DrawLine(_penLine, rect.Right, rect.Bottom, this.panelRightView.ClientSize.Width - rect.Right, rect.Bottom);
}The scenario is like this: I've two panels on my Windows form
panelLeftView
(LEFT docking) andpanelRightView
(FILL docking). In my right panel'sPAINT
event handler, I need to draw a f@#king line. The line draws ok, works exactly as I want, it expands when I increase the width of the form, but the problem is that this code doesn't work if I decrease the width of the form :mad:. The line previously drawn is not cleared when i reduce the form width. Can any .Net guru (especially of C#) tell me why is this happening? Am I doing something wrong :~:confused: ? Or is that problem occuring only to remind me that I'm working on Microsoft's technology where nothing seems to work as expected X| :mad::mad:??? Please Help. Thanks Gurmeet S. Kochar
If you believe in God, it's because of the Devil
My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control
-
hey guys, can any1 please tell me wtf is wrong with the following code :((:
private void panelRightView_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
System.Drawing.Graphics oGfx = this.panelRightView.CreateGraphics();
System.Drawing.Rectangle rect = this.picTitleBar.Bounds;
oGfx.DrawLine(_penLine, rect.Right, rect.Bottom, this.panelRightView.ClientSize.Width - rect.Right, rect.Bottom);
}The scenario is like this: I've two panels on my Windows form
panelLeftView
(LEFT docking) andpanelRightView
(FILL docking). In my right panel'sPAINT
event handler, I need to draw a f@#king line. The line draws ok, works exactly as I want, it expands when I increase the width of the form, but the problem is that this code doesn't work if I decrease the width of the form :mad:. The line previously drawn is not cleared when i reduce the form width. Can any .Net guru (especially of C#) tell me why is this happening? Am I doing something wrong :~:confused: ? Or is that problem occuring only to remind me that I'm working on Microsoft's technology where nothing seems to work as expected X| :mad::mad:??? Please Help. Thanks Gurmeet S. Kochar
If you believe in God, it's because of the Devil
My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control
When resizing the form , you NEED to redraw the control, otherwise it will only display what is in the video memory. My terminology is probably not 100% accurate, but all you really need to know is that you need to refresh your form, and it will work. just do:
this.refresh();
-
hey guys, can any1 please tell me wtf is wrong with the following code :((:
private void panelRightView_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
System.Drawing.Graphics oGfx = this.panelRightView.CreateGraphics();
System.Drawing.Rectangle rect = this.picTitleBar.Bounds;
oGfx.DrawLine(_penLine, rect.Right, rect.Bottom, this.panelRightView.ClientSize.Width - rect.Right, rect.Bottom);
}The scenario is like this: I've two panels on my Windows form
panelLeftView
(LEFT docking) andpanelRightView
(FILL docking). In my right panel'sPAINT
event handler, I need to draw a f@#king line. The line draws ok, works exactly as I want, it expands when I increase the width of the form, but the problem is that this code doesn't work if I decrease the width of the form :mad:. The line previously drawn is not cleared when i reduce the form width. Can any .Net guru (especially of C#) tell me why is this happening? Am I doing something wrong :~:confused: ? Or is that problem occuring only to remind me that I'm working on Microsoft's technology where nothing seems to work as expected X| :mad::mad:??? Please Help. Thanks Gurmeet S. Kochar
If you believe in God, it's because of the Devil
My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control
BTW, I'm issuing
this.panelRightView.CreateGraphics()
instead ofe.Graphics
because in the latter case, the line only draw once and then doesn't expand at all when form's width is increased. I dunno the reason but .... Gurmeet S. Kochar
If you believe in God, it's because of the Devil
My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control
-
When resizing the form , you NEED to redraw the control, otherwise it will only display what is in the video memory. My terminology is probably not 100% accurate, but all you really need to know is that you need to refresh your form, and it will work. just do:
this.refresh();
First of all, I want to thank you for a super quick reply. And second, I again want to thank you for giving me the perfect answer. Yes, it worked. Thank you very much Gurmeet S. Kochar
If you believe in God, it's because of the Devil
My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control
-
BTW, I'm issuing
this.panelRightView.CreateGraphics()
instead ofe.Graphics
because in the latter case, the line only draw once and then doesn't expand at all when form's width is increased. I dunno the reason but .... Gurmeet S. Kochar
If you believe in God, it's because of the Devil
My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control
You need to force the control to refresh when resized and DO NOT create a new
Graphics
object in thePaint
event handler. Besides, even if you do, you must callGraphics.Dispose
on it or you're going to find your memory consumption increasing and performance decreasing. To force your control to repaint itself when it resizes, you can do this a number of ways. The easiest is just to callSetStyle(ControlStyles.ResizeRedraw, true);
in your constructor. You could also set the protectedResizeRedraw
property totrue
in your constructor. These result in the same effect.Microsoft MVP, Visual C# My Articles
-
You need to force the control to refresh when resized and DO NOT create a new
Graphics
object in thePaint
event handler. Besides, even if you do, you must callGraphics.Dispose
on it or you're going to find your memory consumption increasing and performance decreasing. To force your control to repaint itself when it resizes, you can do this a number of ways. The easiest is just to callSetStyle(ControlStyles.ResizeRedraw, true);
in your constructor. You could also set the protectedResizeRedraw
property totrue
in your constructor. These result in the same effect.Microsoft MVP, Visual C# My Articles
Heath Stewart wrote: Besides, even if you do, you must call Graphics.Dispose on it or you're going to find your memory consumption increasing and performance decreasing Hey, what about that Garbage Collection thing? :confused: Heath Stewart wrote: To force your control to repaint itself when it resizes, you can do this a number of ways Well, I tried using
SetStyle
but it says "Cannot access protected member 'System.Windows.Forms.Control.SetStyle(System.Windows.Forms.ControlStyles, bool)' via a qualifier of type 'Panel'; the qualifier must be of type 'FormPackages' (or derived from it)". Gurmeet S. Kochar
If you believe in God, it's because of the Devil
My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control
-
Heath Stewart wrote: Besides, even if you do, you must call Graphics.Dispose on it or you're going to find your memory consumption increasing and performance decreasing Hey, what about that Garbage Collection thing? :confused: Heath Stewart wrote: To force your control to repaint itself when it resizes, you can do this a number of ways Well, I tried using
SetStyle
but it says "Cannot access protected member 'System.Windows.Forms.Control.SetStyle(System.Windows.Forms.ControlStyles, bool)' via a qualifier of type 'Panel'; the qualifier must be of type 'FormPackages' (or derived from it)". Gurmeet S. Kochar
If you believe in God, it's because of the Devil
My CodeProject Articles: HTML Reader C++ Class Library, Numeric Edit Control
Gurmeet S. Kochar wrote: Hey, what about that Garbage Collection thing? The
Graphics
class - like many other class in the .NET FCL - use native resources (unmanaged) so the GC cannot collect them. Read Programming for Garbage Collection[^] in the .NET Framework SDK to learn more. Gurmeet S. Kochar wrote: Well, I tried using SetStyle but it says... As I mentioned, you should call this in your constructor - the constructor for the control you're writing, which means you must extend the base class you want and add it to your constructor:public class MyPanel : Panel
{
public MyPanel()
{
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer
| ControlStyles.UserPaint, true);
// ...
}
// ..
}Microsoft MVP, Visual C# My Articles