Winform Control Sizing (automatic adjustment of Anchor Dock override methods)

Source: Internet
Author: User
automatic adjustment of the position and size of C # WinForm control in C #, the size of the control can be automatically adjusted to the window by two ways 1. Set anchor or Dock properties

Anchor is used to set the distance of the control to the edge to remain unchanged, mainly to ensure the relative layout of each control is neat, here take the button as an example, the correct setting method is as follows

Control size and layout unchanged when picture is stretched

The dock is used to set the control attached to the edge of the form, and set the following correctly (note that the order of the dock for the control will have a significant effect on the result, in this example, set up and down, then around, and finally in the middle, set the order to see the need)

The layout of the space is unchanged after the picture is stretched, and the size changes as the form is resized
2. The control adjusts according to the size percentage of the form (seen on the net)

This approach is equivalent to the entire form as a picture scaling, more beautiful. Double-click the form to enter the load function for edit Form1

private void Form1_Load (object sender, EventArgs e)
{
This. Resize + = new EventHandler (form1_resize);//When the form is resized, events are raised
X = this. width;//get the width of the form
Y = this. height;//get the height of the form
Settag (this);//Calling method
Write your own method:

    float X, Y; The private void Settag (Control cons) {//traverses controls in the form foreach in cons. Controls) {con. Tag = con. Width + ":" + con. Height + ":" + con. Left + ":" + con. Top + ":" + con.
            Font.Size; if (Con.
        Controls.Count > 0) Settag (con); }} private void Setcontrols (float newx, float newy, control cons) {//traverse the controls in the form to reset the value of the control Forea CH (Control con-cons. Controls) {string[] MyTag = con. Tag.tostring (). Split (new char[] {': '})//Get the Tag property value of the control, and then split the storage string array float a = Convert.tosingle (mytag[0]) * newx;//determines the control based on the form scaling scale The value of the width of con. width = (int) a;//Breadth a = Convert.tosingle (mytag[1]) * newy;//height con.
            Height = (int) (a); A = Convert.tosingle (mytag[2]) * newx;//left distance con.
            left = (int) (a); A = Convert.tosingle (mytag[3]) * newy;//Upper edge distance con.
            top = (int) (a); Single CurrentsizE = Convert.tosingle (mytag[4]) * newy;//font size con. Font = new Font (con. Font.Name, CurrentSize, con. Font.style, Con.
            Font.unit); if (Con.
            Controls.Count > 0) {setcontrols (newx, newy, con);
 }
        }
    }
the form's load add

This. Resize + = new EventHandler (form1_resize);//When the form is resized, events are raised
X = this. width;//get the width of the form
Y = this. height;//get the height of the form
Settag (this); Call method Double-click Resize Write in Form1 method

private void Form1_Resize (object sender, EventArgs e)
{
Float newx = (this. Width)/X; Form width Scale
Float Newy = this. height/y;//form Height Zoom ratio
Setcontrols (Newx, Newy, this);//change control size with form
This. Text = this. Width.tostring () + "" + this. Height.tostring ()//form title bar text
}

source program

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;

Namespace control percent sizing
{
public partial class Form1:form
{
Public Form1 ()
{
InitializeComponent ();
}
float X, Y;
private void Settag (Control cons)
{
Traversing controls in a form
foreach (control con) in cons. Controls)
{
Con. Tag = con. Width + ":" + con. Height + ":" + con. Left + ":" + con. Top + ":" + con. Font.Size;
if (Con. Controls.Count > 0)
Settag (con);
}
}
private void Setcontrols (float newx, float newy, control cons)
{
Iterate through the controls in the form to reset the control's value
foreach (control con) in cons. Controls)
{
string[] MyTag = con. Tag.tostring (). Split (new char[] {': '})//Get the Tag property value of the control and store the string array after split
float a = Convert.tosingle (mytag[0]) * newx;//determines the value of the control based on the form scaling, width
Con. width = (int) a;//widths
A = Convert.tosingle (mytag[1]) * newy;//height
Con. Height = (int) (a);
A = Convert.tosingle (mytag[2]) * newx;//left distance
Con. left = (int) (a);
A = Convert.tosingle (mytag[3]) * newy;//Upper edge Distance
Con. top = (int) (a);
Single currentsize = Convert.tosingle (mytag[4]) * newy;//font size
Con. Font = new Font (con. Font.Name, CurrentSize, con. Font.style, Con. Font.unit);
if (Con. Controls.Count > 0)
{
Setcontrols (newx, newy, con);
}
}
}
private void Form1_Load (object sender, EventArgs e)
{
This. Resize + = new EventHandler (form1_resize);//When the form is resized, events are raised
X = this. width;//get the width of the form
Y = this. height;//get the height of the form
Settag (this);//Calling method
}

private void Form1_Resize (object sender, EventArgs e)
{
Float newx = (this. Width)/X; Form width Scale
Float Newy = this. height/y;//form Height Zoom ratio
Setcontrols (Newx, Newy, this);//change control size with form

    }
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.