简体   繁体   English

无法按名称查找控件

[英]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. 我正在用C#编写应用程序,要求之一是以编程方式本地化UI的文本/工具提示。 I am reading in the locale info from XML into a variable. 我正在将语言环境信息从XML读入一个变量。 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. 其中cont.Name保存控件的名称。 All I'm getting for c is a null. 我为c获取的所有内容都是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 另外,从目前为止所见,我不确定如何以所需的方式更改ToolTipText,例如

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). 以前,当我遇到相同的问题时,我通过在“控件”列表中循环并询问其名称是否包含在我的控件名称列表中来解决(在您的情况下,来自XML文件)。

Control.ControlCollection controls = this.Controls;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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