Use the control name in winform to find controls in the form or container

Source: Internet
Author: User

In projects, you often encounter the need to dynamically generate controls to dynamically display specified controls or hide them, which involves the issue of finding controls.

The following describes how to use the control name in winform to find controls in a form or container. The Code is as follows:

/// <Summary>
/// Obtain the control using the control name
/// </Summary>
/// <Param name = "strname"> </param>
/// <Returns> </returns>
Private control getpbcontrol (string strname)
{
String pbname = strname;
Return getcontrol (this, pbname );
}
/// <Summary>
/// Obtain the control using the control name
/// </Summary>
/// <Param name = "CT"> container or form of the Control </param>
/// <Param name = "name"> name of the control to be searched </param>
/// <Returns> </returns>
Public static control getcontrol (control CT, string name)
{
Control [] CTLs = CT. Controls. Find (name, false );
If (CTLs. length> 0)
{
Return CTLs [0];
}
Else
{
Return NULL;
}
}

The Demo code of the query control is as follows:

Strpxname = "softkeyboard ";
Control Sk = getpbcontrol (strpxname );
If (Sk = NULL)
{
Return;
}

 

 

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.