1) C # traverse the controls in the container
Code 1:
Private list <control> Find (control C, type T) // C is the container control, and T is the selected type
{
List <control> controls = new list <control> ();
Foreach (control CC in C. Controls)
{
If (CC. GetType () = T)
Controls. Add (CC );
}
Return controls;
}
Code 2:
Public list <textbox> gettextboxlist ()
{
Try
{
List <textbox> lsttext = new list <textbox> ();
Ienumerator ier = This. panel1.controls. getenumerator ();
While (IER. movenext ())
{
If (IER. Current is textbox)
{
Textbox A = ier. Current as textbox;
Lsttext. Add ();
}
}
Return lsttext;
}
Catch (exception ex)
{
System. Diagnostics. Debug. writeline (ex. Message );
}
Return NULL;
}
2) how to obtain the width of a text string in C # (in pixel px)
. Net System. in the drawing space, the graphics object provides a method to achieve this goal: measurestring () Public void printoutbox (string printstr, int printwidth, string printalingment, system. windows. forms. painteventargs g)
{
Int X1 = currentx;
Int Y1 = currenty;
Int t_space = 2;
Sizef Sf = G. measurestring (printstr, printfont (); // rectangle rec1 = new rectangle (currentx, currenty, printwidth, (INT) (SF. height + t_space ));
G. drawrectangle (pens. Black, rec1 );
If (printalingment = "R" | printalingment = "R ")
{
Currentx = X1 + printwidth-(INT) Sf. width;
Currenty = Y1 + (t_space/2 );
}
G. drawstring (printstr, printfont (), brushes. Black, currentx, currenty );
}