Traverse controls
Foreach (Control in this. controls) {If (control. getType () = typeof (picturebox) // query by type {picturebox Pb = control as picturebox; // convert it to MessageBox of the specific control type. show (Pb. image. height. tostring ();} If (control. name = "picturebox2") // query the control {MessageBox. show ("I Am a control named picturebox2 ");}}
Control in this. Controls
Traverse all the child controls of the current control. The child control here is form.
Since every control on form inherits from the control class, it is generally used in foreach (Control in this. in a sentence pattern, each control element on the form is traversed, and each current control is packed into a control instance for operations.
For example, tabcontrol1.tabpages [tabcontrol1.selectedindex]. controls; // obtain the child control in the selected Tab
DragCode
Public class moveablepicturebox: picturebox {[system. runtime. interopservices. dllimport ("user32.dll")] Private Static extern intptr sendmessage (intptr hwnd, int MSG, int wparam, int lparam); protected override void onmousedown (mouseeventargs e) {base. onmousedown (E); If (E. button = mousebuttons. left) // click the left mouse button {capture = false; // release the mouse to manually operate sendmessage (this. handle, 0x00a1, 2, 0); // drag the form }}}
Converting string type to control object
1. Use reflection
Reflection provides an infrastructure for implicit late binding by a language compiler (such as Microsoft Visual Basic. NET and JScript. Binding is the process of finding the declaration (Implementation) corresponding to the unique specified type. Because this process does not occur during compilation at runtime, it is called late binding. Visual Basic. Net allows you to use implicit late binding in your code. The Visual Basic compiler calls a helper method that uses reflection to obtain the object type. Parameters passed to the helper method help call the correct method at runtime. These parameters include the instance (object) of the method to be called, the name (string) of the called method, and the parameters (Object array) passed to the called method ).
2. Control Traversal
Control [] CTR;
CTR = form object. Controls. Find ("string", true );