C # create, delete, and assign values to the winform control dynamically in the center of the form

Source: Internet
Author: User

1. Use lable as an example:

 

Place a widget in form to keep the widget centered during startup and dynamic window change.

 

Int gleft = This. width/2-lable1.width/2; // This indicates form
Int gtop = This. Height/2-lable1.height/2;

Lable1.location = new point (gleft, gtop );

 

2. dynamically create and locate or delete controls

1. Use lable as an example to create a control:

Label LBL = new label ();
LBL. Name = "lblnum" + m;
LBL. autosize = true;
LBL. backcolor = system. Drawing. color. transparent;
LBL. font = new system. drawing. font ("", 36f, system. drawing. fontstyle. bold, system. drawing. graphicsunit. point, (bytes) (134 )));
LBL. forecolor = system. Drawing. color. White;
LBL. Location = new system. Drawing. Point (lable1.location. X-150, 29 );
LBL. textalign = system. Drawing. contentalignment. topcenter;
LBL. Anchor = system. Windows. Forms. anchorstyles. Top;
// This. Controls. Add (LBL); add controls to the form
This. groupbox2.controls. Add (LBL );//Add controls to groupbox2

2. Use lable as an example to delete the control:

 

If (this. groupbox2.controls. Containskey("Lable2") = true)
{
This. groupbox2.controls.Removebykey("Lable2 ");
}

 

 

 

 3. Locate the control and assign values to it.

 

 

Control [] control = This. Controls. Find ("lable1", true );

If (control. Length = 1 & Control [0] Is label)
{

(Control [0] As label). Text = "hello ";

}

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.