Asp tutorial. net automatically obtains the control name value after the control is generated
// Convenient to use the tag attribute of the control
Private void message (object sender, eventargs e)
{
Picturebox pic = sender as picturebox;
String lblname = (label) pic. tag). name;
Messagebox. show (lblname );
}
// Specify the name when dynamically generating the control and find it:
Foreach (control ct in form1.controls)
{
// C # Only traverse the child control of the form, not the child Control
// When the controls on the form have subcontrols, You need to traverse them recursively to list all controls on the form.
If (ct. name = "")
{
}
}
}
// Instance
Picturebox p = new picturebox ();
P. name = "p" + I. tostring ();
P. sizemode = system. windows. forms. pictureboxsizemode. stretchimage;
P. imagelocation = @ "d: www.bKjia.c0mhome4.png ";
P. size = new system. drawing. size (60, 60 );
P. location = new system. drawing. point (5*17 * I-60, 50 );
P. cursor = system. windows. forms. cursors. hand;
P. doubleclick + = new eventhandler (message );
P. mousemove + = new system. windows. forms. mouseeventhandler (this. mouseo tutorial ver );
P. mouseleave + = new system. eventhandler (this. mouseo tutorial ut );
P. contextmenustrip = contextmenustrip1;
This. sp_l_r.panel2.controls.add (p );
Label la = new label ();
La. name = "la" + I. tostring ();
La. text = "0000 ";
La. location = new system. drawing. point (50,120 );
This. sp_l_r.panel2.controls.add (la );