Use the control name in winform to obtain the control

Source: Internet
Author: User
In the past, the control. findcontrol (string) method has been used in webform to obtain a control on the page. However, the controlcollection in winform does not provide the findcontrol method:

Http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformscontrolcontrolcollectionmemberstopic.asp? Frame = true

No way, you have to create a hash table.

// Populate data



Ushort [] Mydata =   New   Ushort [] { 111 , 222 , 333 , 444 , 555 , 666 , 777 , 888 , 999 , 1000 } ;





// Create a hashtable reference to all the textboxes



Hashtable httextbox =   New Hashtable ();


Foreach (Control C In   This . Controls)


{


If (C. GetType (). tostring () =   " System. Windows. Forms. textbox " )


Httextbox. Add (C. Name, C );


}


// Search textbox from hashtable and evalute it.



For ( Int I = 0 ; I < 10 ; I ++ )


{


Textbox T;


T = (Textbox) httextbox [ " Textbox " + (I + 1 ). Tostring ()];


T. Text = Mydata [I]. tostring ();


}  

I checked the. NET Framework 2.0 document and found that. NET 2.0 added the find Method to the control. controlcollection object...

Http://msdn2.microsoft.com/library/1hb809fy.aspx

This post is provided as "the status quo" without any guarantee and has not been granted any rights

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.