C # control Traversal

Source: Internet
Author: User

 

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

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.