C# PANEL DRAWING ON BUTTON PRESS

Chintoo Semi

Total Post:135

Points:947
Posted by  Chintoo Semi
C# 
C#
 2621  View(s)
Ratings:
Rate this:

I want to update the panel graphics (panel will contain simple shapes like circles and rectangles) every time a button is pressed. The code would look something like this:

private void PanelGraphics()
{
    ClearThePanel();  //empties the panel
    FillThePanel();   //draws new shapes in the panel
}
The function would be then called every time a button is pressed, and I understand button_events well enough so that's not a problem. However, I have no idea how to pull off the PanelGraphics() function, and a few links about panel_paint events didn't help me much.
  1. ezra heywood

    Post:145

    Points:1019
    Re: C# panel drawing on button press

    Hi Chintoo,


    You can draw to the panel by creating a graphics controller to it.

    Graphics g = panel.CreateGraphics();

    You can then use the Graphic class's plethora of methods to draw whatever you want to the panel

    g.DrawCurve(parameters);
    g.DrawEllipse(parameters);
    g.DrawLine(parameters);
    g.DrawRectangle(parameters);

    To clear the panel the easiest way is to draw a box the colour of the background to the panel

    g.DrawRectangle(new Pen(panel.BackColor), new Rectangle(new Point(), panel.Size));

      Modified On Apr-03-2018 05:48:01 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!