none
Adding a Rectangle object to a Panel RRS feed

  • Question

  • I was able to add a button/label to a Panel like this;
    this.panel1.Controls.Add(this.button1);
    this.panel1.Controls.Add(this.label11);
    this works..OK!!

    But, what I need is to add a Rectangle object to a Panel.

    I draw a Rectangle object;

    Graphics graph = this.CreateGraphics();
    Pen penCurrent = new Pen(Color.Red);
    Rectangle Rect = new Rectangle(300, 50, 150, 75);
    graph.DrawRectangle(penCurrent, Rect);

    But when i do like this;
    this.panel1.Controls.Add(this.Rect);

    it gives an ERROR!

    -----
    Says;
    Controls.Add() , works only for System.Windows.Forms.Control and CanNOT work with System.Drawing.Rectangle
    -----

    So how can i achive this;
    add a Rectangle object to a Panel


    Friday, May 25, 2007 1:37 PM

Answers

  • Just draw the shape in the panel's Paint event.  Here's an example, drop a Panel onto the form and paste this code:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
          // Anchor panel so it gets resized when form size changes
          this.panel1.Anchor |= AnchorStyles.Bottom | AnchorStyles.Right;
          this.panel1.Resize += panel1_Resize;
          this.panel1.Paint += panel1_Paint;
        }
        private void panel1_Paint(object sender, PaintEventArgs e) {
          // Paint rectangle and ellipse
          Rectangle rc = panel1.ClientRectangle;
          rc.Inflate(-10, -10);
          e.Graphics.DrawRectangle(Pens.Black, rc);
          e.Graphics.DrawEllipse(Pens.Black, rc);
        }
        private void panel1_Resize(object sender, EventArgs e) {
          // Graphic needs to be repainted when panel size changes
          panel1.Invalidate();
        }
      }

    Sunday, May 27, 2007 1:57 PM
    Moderator

All replies

  • Hi,

      depends on what you want, you could just create a new System.Windows.Form.Panel instance and add that to your form, since you can make it rectangular in shape there is no need to specifically draw something.  Your problem is that only objects that inherit from Control can be added to the Controls collection, and Rectangle does not, it is not a visual item.

     

      if you want to draw a rectangle on the form you should override the OnPaint method and draw your rectangle there.

     

    Mark.

    Friday, May 25, 2007 1:57 PM
  • yes;

    but, what i need is to resize the rectangle at runtime.

    I was able to resize the buttons/labels at runtime. There, what  i did was; add the button/label to the Panel and resize the Panel. So im going to apply the same theory here as well.
    but not works.!
    if you can pls, specify the code sample to achive this.- adding a Rectangle object to a Panel
    or any better way to resize the rectangle at runtime?
    Friday, May 25, 2007 2:05 PM
  • Hi,

      well depends how you want to resize the rectangle,  you could add a panel like I mentioned above and anchor the panel on the form so that when it is resized the panel is also resized, or do you want the user to be able to drag the sides of the rectangle into a new shape?  If you want an example of how to programatically draw a rectangle as the user moves the mouse see this post: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1560137&SiteID=1 which should point you in the right direction.

     

    Mark.

    Friday, May 25, 2007 2:19 PM
  • "you could add a panel like I mentioned above and anchor the panel on the form so that when it is resized the panel is also resized,"

    yh;
    hi,
    but i need to resize these all shapes.. such as Rectangle,Square,Circle.... so it may be a problem if i use the above method..
    If i able to find this "adding a rectangle obj. to a panel"...i think the same steps can be applied to the other shapes as well..like for circle..
    BUT
    still i was NOT able to find how to do this -
    adding a rectangle obj. to a panel...if you can pls explain with a code sample
    Thanks!

    by the way, the C# program in this thread gives an exception, when executing;
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1560137&SiteID=1
    Friday, May 25, 2007 2:37 PM
  • Hi,

      "still i was NOT able to find how to do this - adding a rectangle obj. to a panel"  Okay - if you want to add a rectangle / circle / triangle etc object to your form so that the user can resize it you will have to create your own custom shape controls, you cannot just add a rectangle to a Controls collection since it does not mean anything, a rectangle is not a visual element it has no way to display itself, there is no default shape control in .Net you can use out of the box.

     

      What you can try is the example I noted previously which shows you how to draw a rectangle on a control by overriding the OnPaint method to paint a rectangle onto the control, you can also change this to draw a circle / triangle etc.  In terms of resizing, if you want something like a graphical editor where the user drops in shapes and can resize by clicking on a little square in the top corner etc then you will have to make a control e.g.

     

    class MyControl : Control

    {

      ....

    }

     

    That handles the drawing / user input and resizing, then you can add these items to your form:

     

    MyControl mc = new MyControl();

    myForm.Controls.Add(mc);

    ...

     

    I would suggest you look on the web for some GDI+ tutorials, to get you starte, something like:http://www.c-sharpcorner.com/UploadFile/mahesh/gdi_plus12092005070041AM/gdi_plus.aspx

     

    "by the way, the C# program in this thread gives an exception, when executing;
    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1560137&SiteID=1 "

    Did you change the image that the sample was loading, it probably will not exist on your computer.  What was the exact exception, it works on my computer.

     

     

    Mark.

    Friday, May 25, 2007 7:32 PM
  • Just draw the shape in the panel's Paint event.  Here's an example, drop a Panel onto the form and paste this code:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
          // Anchor panel so it gets resized when form size changes
          this.panel1.Anchor |= AnchorStyles.Bottom | AnchorStyles.Right;
          this.panel1.Resize += panel1_Resize;
          this.panel1.Paint += panel1_Paint;
        }
        private void panel1_Paint(object sender, PaintEventArgs e) {
          // Paint rectangle and ellipse
          Rectangle rc = panel1.ClientRectangle;
          rc.Inflate(-10, -10);
          e.Graphics.DrawRectangle(Pens.Black, rc);
          e.Graphics.DrawEllipse(Pens.Black, rc);
        }
        private void panel1_Resize(object sender, EventArgs e) {
          // Graphic needs to be repainted when panel size changes
          panel1.Invalidate();
        }
      }

    Sunday, May 27, 2007 1:57 PM
    Moderator
  • change: Graphics graph = this.CreateGraphics();

    to: Graphics graph = this.panel1.CreateGraphics();

    or: Graphics graph = panel1.CreateGraphics();

    Thursday, January 23, 2020 8:05 AM