How to clear panel's graphics in c#? - c#

I have used grahics in a panel. At start of the program I draw some points in the panel and after that I want to draw lines connecting those points. Problem is when I press tab button the graphics created disappear (but this happens once in the program). Next problem is I want to clear the panel I used following code to clear panel:
Panel1.Invalidate();
But this only clears the lines but not those points that were initially created. Does anyone has a simple solution because I don't want to recreate the panel.
Technical Detail: to draw initial points in panel, paint event of panel1 is used:
Graphics gfx = e.CreateGraphics()
For lines, there is a seprate function that is called on button click and in that I used:
Graphics gfx = Panel1.CreateGraphics();
Another button that is used to clear panel has following code:
Panel1.invalidate();
but it only clears the line graphics, not those initial points.

I was making a mistake by creating initial points in the paint event of panel1. So everytime i call
Panel1.Invalidate();
it recalls that paint event and those points are redrawn and not cleared.

Do you save the points in some sort of collection and draw them in the Paint event?
Then you should empty the collection and then call the Invalidate

The panel is redrawn inside the paint event. It means that you have to draw everything inside this method, instead of accessing the graphics context directly.
This explains why everything you draw when you use Graphics gfx = Panel1.CreateGraphics(); is lost every time the control is redrawn, since during the paint event, you are only drawing the initial points, nothing more.

Related

Graphics FillEllipse in Picturebox save state

I create graphics thru Graphics.FillEllipse(...) which is drawn on Picturebox. After all drawing is done I would like to save current state of Picturebox with points drawn. Can someone point me to right direction, or what is the best approach to achieve desired functionality?
Thanks in advance ;)
Windows Forms painting on controls (including of course PictureBox) is immediate mode drawing. Whatever you are drawing must be repainted in some event handler, or it is lost on the next repaint. Typically the Control.Paint event. Perhaps you should move the drawing logic into some method that takes a Graphics instance as a parameter. When you want to draw to a bitmap, for example, you can create a graphics object from the bitmap using Graphics.FromImage(...) and pass it to the painting method. Whatever you "paint" in the bitmap stays in the bitmap: no need to repaint. Then you can save or do whatever you want with the bitmap.
See immediate versus retained mode:
http://msdn.microsoft.com/en-us/library/windows/desktop/ff684178%28v=vs.85%29.aspx

How would I assign a control an order number?

I have created a method which draws all the controls to the panel, but its draws them based on the order which I have listed them, which means PictureBox1 will always be behind all the other pictureboxes. Example:
e.Graphics.DrawImage(PictureBox1.BackgroundImage,
new Rectangle(PictureBox1.Location, PictureBox1.Size));
e.Graphics.DrawImage(PictureBox2.BackgroundImage,
new Rectangle(PictureBox2.Location, PictureBox2.Size));
it draws PictureBox1 first, then PictureBox2 second, then PictureBox3 third etc...
This means that PictureBox2 get drawn Over PictureBox1, and PictureBox3 gets Drawn over PictureBox2. Here's a pic to display overlapping images:
Now when I push a button when focus is on picturebox2 I would like the DrawImage order to be changed so that PictureBox2 is drawn last.
I'm sure one of you has a great solution to this, I would like to listen to any suggestions you may have.
In your question you haven't made it clear what determines the order for all the other picture boxes in the panel. However, you can achieve the same effect using SetChildIndex. To Swap the position of PictureBox1 with PictureBox2 for instance, you can do:
var p1ndex = panel.Controls.IndexOf(pBox1);
var p2ndex = panel.Controls.IndexOf(pBox2);
panel.Controls.SetChildIndex(pBox1, p1ndex);
panel.Controls.SetChildIndex(pBox2, p2ndex);
Here, pBox1 could be the last PictureBox you hovered on and pBox2 the current one you're hovering on...
Assuming this is WinForms, you can call
picturebox2.BringToFront()
before redrawing it.
If you need finer control than just "make me on top of all other controls" you can use GetChildIndex and SetChildIndex.

Doublebuffer winforms panel issues

I'm trying to double buffer a winforms panel element. I use the panel for drawing shapes. Right now, I have something along the lines of:
class BufferPanel : Panel {
public BufferPanel() {
this.DoubleBuffer = true;
}
}
Mouse movement triggers the panel Refresh(). Some shapes are drawn in when Paint is triggered This is where I have a problem. These shapes are only drawn for a split second after mouse movement triggers a Refresh, then completely vanish. It's like they are only drawn on one buffer or something along those lines. This appears to only happen with Paint. For example, I can copy/paste the shapes into the mouse move method, and everything will work fine. Any thoughts on why?
Posted from comment:
Sounds like you aren't using the e.Graphics object from the Paint event or OnPaint override. Avoid using CreateGraphics.

Issue on PowerPack Controls and Graphics upon clearing and redrawing

To briefly explain:
I have a win form written in C# that contains a panel.
The panel contains a shape container, through that I have programmatically added some oval and line shape controls from VisualBasic.PowerPacks to the panel.
The reason of using such shapes was I needed to perform some operations on their MouseHover and MouseClick events.
I have also drawn some graphics like strings and ellipse on the panel using Graphics in Paint(object sender, PaintEventArgs e) method of the panel.
The application has zoom in and zoom out buttons and whenever user clicks on them the size of shapes and graphics are supposed to be changed based on the scale.
To redraw graphics after scaling, I needed to clear the old ones before drawing new graphics in the new scale, otherwise they remained on the panel. So, I used Clear(Color color) method of the graphic to do so.
Now, the problem I have is upon using Clear(Color color), everything including shape controls gets disappeared. By my perception, I do not expect shapes to be disappeared because they should be treated as controls, unless I am missing something here. How can I avoid this issue? Any advice would be appreciated.
Solved the problem by invalidating "shape.Invalidate()" shape controls.

Border for Dynamically created buttons in Windows Application C#.net

Hai all,
I am dynamically creating button at runtime in a c#.net Windows Application, I want to draw a border for the dynamically created button how can i do it, i tried this
private void DrawBorder(Button bt)
{
Graphics g = bt.CreateGraphics();
ControlPaint.DrawBorder(g, bt.DisplayRectangle, Color.FromArgb(229, 227, 227), ButtonBorderStyle.Solid);
g.Dispose();
}
but it is not showing any error but i can't see any border
Please Help
If you want a button that draws differently, you should probably create a new class inheriting from Button, and in it you would override OnPaint and/or OnPaintBackground and implement your drawing logics, such as drawing a border.
The reason your current solution is not working probably depends on when you are calling your DrawBorder method. If your drawing code is not executed as part of handling an OnPaint event, then the graphics you draw will be drawn-over in the next paint.
But anyway, instead of drawing the border yourself, can't you set the Border proerpty on the Button object?

Categories

Resources