Getting the size of a Windows Form
Asked Answered
J

4

10

I'm creating a Windows Forms application. How do I capture the size of the windows form?

Currently I have something that looks like this in my code:

PictureBox display = new PictureBox();
display.Width = 360;
display.Height = 290;
this.Controls.Add(display);
display.Image = bmp;

However, the size of my display is hard-coded to a specific value.

I know that if I want to draw a square that re-sizes I can use something like this:

private Rectangle PlotArea;
private int offset = 30;
protected override void OnPaint(PaintEventArgs e)
{
    Graphics g = e.Graphics;

    //// Calculate the location and size of the plot area
    //// within which we want to draw the graphics:

    Rectangle ChartArea = ClientRectangle;
    PlotArea = new Rectangle(ChartArea.Location, ChartArea.Size);

    PlotArea.Inflate(-offset, -offset);

    Draw PlotArea:
    g.DrawRectangle(Pens.Black, PlotArea);
}

Is there a way for me to use method one and grab the size of the form for Height and Width?

I've tried the following code, but it doesn't work...

PictureBox display = new PictureBox();
display.Width = ClientRectangle.Y;
display.Height = ClientRectangle.X;
this.Controls.Add(display);
display.Image = bmp;
Jalousie answered 30/10, 2011 at 2:24 Comment(0)
H
12
    PictureBox display = new PictureBox();
    display.Width = ClientRectangle.Width;
    display.Height = ClientRectangle.Height;
    this.Controls.Add(display);
    display.Image = bmp;

When you resize the window:

private void Form1_Resize(object sender, System.EventArgs e)
{
   Control control = (Control)sender;
   // set the PictureBox width and heigh here using control.Size.Height 
   // and control.Size.Width
}
Hinckley answered 30/10, 2011 at 2:34 Comment(1)
Hmmm... i tried this but it doesn't re-size the lines when the form is being re-sized...Jalousie
Z
17

For Getting the size of a Windows Form:

int formHeight = this.Height;
int formWidth = this.Width;
Zoes answered 29/7, 2013 at 9:2 Comment(0)
H
12
    PictureBox display = new PictureBox();
    display.Width = ClientRectangle.Width;
    display.Height = ClientRectangle.Height;
    this.Controls.Add(display);
    display.Image = bmp;

When you resize the window:

private void Form1_Resize(object sender, System.EventArgs e)
{
   Control control = (Control)sender;
   // set the PictureBox width and heigh here using control.Size.Height 
   // and control.Size.Width
}
Hinckley answered 30/10, 2011 at 2:34 Comment(1)
Hmmm... i tried this but it doesn't re-size the lines when the form is being re-sized...Jalousie
R
4

You want to set Dock = DockStyle.Fill to dock the control to fill its parent.

Raveaux answered 30/10, 2011 at 3:2 Comment(0)
R
3

Set it to ClientRectangle.Width.

Raveaux answered 30/10, 2011 at 2:26 Comment(2)
Hmmm I added an edit at the end of my code - is this what you meant i should try? If not please provide an example.Jalousie
Did you read what you just wrote? X means the location, not the size.Raveaux

© 2022 - 2024 — McMap. All rights reserved.