C # The widget size varies with the size of the form,

Source: Internet
Author: User

C # The widget size varies with the size of the form,

I believe that when many bloggers develop C # winForm for the first time, when the form size changes, the controls in the form do not change with the changes in the form. Recently, due to a project, this problem also needs to be solved. Through Reading and learning, this problem has been solved. It may not be a good solution, but it is also worth learning...

Below I will create a Demo to explain the implementation of this method: (Note: I am using VS2010)

Step 1: Open VS2010 and create a C # Winform Project (Note: I name it test1 here. You can play it freely !)

Drag the control to achieve the effect after it is created:

Private float X; // The width of the current form private float Y; // The height of the current form

2. FunctionSetTag

/// <Summary> /// set the width and height of the control, left margin, the top margin and font size are saved to the tag attributes. /// </summary> /// controls in the <param name = "cons"> recursive Control </param> private void setTag (control cons) {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 );}}

3. FunctionSetControls

// Adjust the Control size according to the form size. private void setControls (float newx, float newy, Control cons) {// traverse the controls in the form, reset the Control value foreach (Control con in cons. controls) {string [] mytag = con. tag. toString (). split (new char [] {':'}); // obtain the Tag attribute value of the control, and store the string array float a = System. convert. toSingle (mytag [0]) * newx; // determines the value of the control based on the scale of the form. The width is con. width = (int) a; // Width a = System. convert. toSingle (mytag [1]) * newy; // height con. height = (int) (a); a = System. convert. toSingle (mytag [2]) * newx; // left distance from con. left = (int) (a); a = System. convert. toSingle (mytag [3]) * newy; // distance from the upper edge to con. top = (int) (a); Single currentSize = System. 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 );}}}

4. Add

Private void Form1_Load (object sender, EventArgs e) {X = this. width; // obtain the form Width Y = this. height; // obtain the Height of the form setTag (this); // call method}

5. Add in the Resize event of the form

Private void Form1_Resize (object sender, EventArgs e) {float newx = (this. width)/X; // float newy = (this. height)/Y; // format Height scaling ratio setControls (newx, newy, this); // adjust the widget size with the form}

 

Step 3: Click start debugging. Now this function has been implemented. Are you sure you have found that all controls can change proportionally! (Over)

 

Conclusion: of course, this is only a small Demo. in actual application, you need to open your brain holes to better fit your code, better Application to actual projects. This is my first blog post. Although it is not long enough, the content is full! I hope you will have more support in the future ~

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.