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.
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.
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.
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).