简体   繁体   中英

Unable to find a control by name

I'm writing an app in C# and one of the requirements is to programmatically localize the text/tooltips of the UI. I am reading in the locale info from XML into a variable. I need to be able to ID the control by its name and then change the text (button) and/or tooltips. So far I have been unable to even grab the appropriate control.

As part of a loop I have tried:

Control c = this.Controls.Find(cont.Name, true).FirstOrDefault() as Control;

Where cont.Name holds the name of the control. All I'm getting for c is a null. I know the control does exist on the form, I'm just not able to access it.

Also, from what I have seen so far, I'm not sure how I can change the ToolTipText the way I need to, eg

c.ToolTipText = cont.ToolTip;

Also, this is all happening in a user control and all of the child controls I'm trying to access are on toolstrips.

I really appreciate any help with this.

Time ago, when i had the same problem, I solved by looping in Controls list and asking if its name was contained in my list of controls names (in your case, from your XML file).

Control.ControlCollection controls = this.Controls;

foreach (Control cont1 in controls)
{
     if(contList.Contains(cont1.Name))
     {
           //Your code
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM