0

I have a rectangle which i am drawing on a form. The rectangle is coded.I want to resize that rectangle according to the resizing of the form, that is as and when the form is resized, the rectangle is resized accordinly. How can I do it? Do i need to make changes in the way i am drawing the rectangle which by the way right now is like this : Rectangle rect = new Rectangle(0, 0, 650, 50); I used the Resize events in which i assigned the new size by this I did new Size() but that is making my rectangle disappear from the form.

0

3 Answers 3

2

Add the following in the OnPaint handler of your form:

rect.Width = this.Width;
rect.Height = this.Height;

That should keep the form filled with your rectangle.

The OnPaint event should look something like this:

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        rect.Width = this.Width;
        rect.Height = this.Height;
    }

This would resize your rectangle everytime the form is repainted, which includes resizing as well.

1

You should do all the drawing of your form at the override of the OnPaint method. You need to check the size of the form each time you draw the rectangle if you want to make the size of the rectangle relative to the size of the Form.

You shouldn't do anything with the Resize event (maybe just cache the size needed of the rectangle to avoid having to calculate that size each time the OnPaint is called).

-1

To do that, you'll have to create a resizing function to your rectangle in the Form Resize event of your form. And to keep the rectangle proportions relative to the form size you'll have to code some simple math.

1
  • This will not work because the rectangle has to be drawn at the OnPaint method Jun 29, 2010 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.