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;
}