Home > database >  Make a Rectangle
Make a Rectangle

Time:12-17

It is as simple as it sounds I need to draw a rectangle but no matter what I do I cant seem to get it to work. This is done on a windows form and all I want is to output one rectangle, I managed to do this at school but now at home it doesn't work.

using System.Drawing;

namespace Rectanglr
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void FormPaint(object sender, PaintEventArgs e)
        {
            Pen pen = new Pen(Color.Red, 3);
            Rectangle rect = new Rectangle(0, 0, 100, 100);
            e.Graphics.DrawRectangle(pen, rect);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

any help would be greatly appreciated.

CodePudding user response:

You can try overriding the OnPaint method of your form:

draw rectangle

This code should do the trick:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        using (Pen pen = new Pen(Color.Red))
        {
            Rectangle rect = new Rectangle(0, 0, 100, 100);
            e.Graphics.DrawRectangle(pen, rect);
        }
    }
}

It's also recommended to wrap it with a using block to ensure that the Pen resource will be properly disposed of when you are finished drawing.

CodePudding user response:

Here's a project that I set up to draw a rectangle:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        using (Pen pen = new Pen(Color.Red, 3))
        {
            Rectangle rect = new Rectangle(0, 0, 100, 100);
            e.Graphics.DrawRectangle(pen, rect);
        }
    }
}

The important part to recognize here is the partial keyword. That's a hint that there is a second file, or maybe more, that defines this class. In this case, it's the designer file - Form1.Designer.cs.

If I look in the designer I can see this line:

  this.Paint  = new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);

That's the magic that makes sure this works. Without it the event isn't wired up.

Now, having said that, events are usually used by external classes to your form. The preferred approach to modify the Paint method is to override it.

That looks like this:

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        using (Pen pen = new Pen(Color.Red, 3))
        {
            Rectangle rect = new Rectangle(0, 0, 100, 100);
            e.Graphics.DrawRectangle(pen, rect);
        }
    }

There's no need to wire this up as an event handler.

As a sidenote, please make sure you dispose every disposable in your code. A Pen is disposable and the using statement does the disposing for you.

  • Related