Satinder
Satinder

Reputation: 13

When I resize my Form to full screen my controls are still on old position

Here is my code

        private void button1_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Normal;
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Bounds = Screen.PrimaryScreen.Bounds;
            pictureBox1.Dock = DockStyle.Fill;

        }

What code I can applied on picturebox2 so that picturebox2 also displaced according to form ...

Upvotes: 1

Views: 2487

Answers (3)

Markus
Markus

Reputation: 22456

You can use the Dock and Anchor properties of the controls to determine their behavior when the form is resized.
When using docks and anchors in WinForms, you typically decide for a primary control (group) that gets the main part of the screen and another group of controls that are aligned in the remaining area. So if you set DockStyle.Fill for PictureBox1 control, you set the other PictureBox to DockStyle.Right. When the form is resized, the main area is extended. Please note however, that it sometimes depends on the order that the controls were created on how they are aligned and whether it works as expected. It might take some experimenting with putting various controls to foreground in order to reach your goal.
This link lists a lot of tutorials on how to align controls on Windows Forms, specifically on setting anchors and docking controls.
In addition, you can use various layout controls, among them a TableLayoutPanel (Thanks @HansPassant for the hint). For a walk-through see this link.

Upvotes: 4

Vishwaram Sankaran
Vishwaram Sankaran

Reputation: 357

Here you have to scale the child controls as the main window is scaled. try the Scale method by calculating of the scale factor as below:

the code:

Size st = this.Size;
        int height = st.Height;
        int width = st.Width;

        this.WindowState = FormWindowState.Normal;
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Bounds = Screen.PrimaryScreen.Bounds;

        Size newSize = this.Size;

        SizeF scaleFactor = new SizeF();
        scaleFactor.Height = newSize.Height / height;
        scaleFactor.Width = newSize.Width / width;

        this.Scale(scaleFactor);

Upvotes: 0

dburner
dburner

Reputation: 1007

You need to set anchors to all your controls on your form( by default all your controls are "tied" to the top and left of your form) . If anchors are not enough try using dock panels and dock your control.

You can se the anchors from the visual editor. Select a control and on the properties panel you should set the anchors.

Upvotes: 1

Related Questions