Gol*_*old 5 c# compact-framework windows-ce
我string MyText持有"L1"
我有标签控制,他的名字是"L1"
有没有办法用MyText读取标签L1?
就像是: TMT = MyText.Text
要么: TMT = ((Control)MyText.ToString()).Text;
提前致谢
查找具有指定名称的控件:
var arr = this.Controls.Where(c => c.Name == "Name");
var c = arr.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
或在指定类型的控件内搜索:
var arr = this.Controls.OfType<Label>();
var c = arr.FirstOrDefault();
Run Code Online (Sandbox Code Playgroud)
编辑:
如果您有一系列控件名称,您可以找到它们:
var names = new[] { "C1", "C2", "C3" };
// search for specified names only within textboxes
var controls = this.Controls
.OfType<TextBox>()
.Where(c => names.Contains(c.Name));
// put the search result into a Dictionary<TextBox, string>
var dic = controls.ToDictionary(k => k, v => v.Text);
Run Code Online (Sandbox Code Playgroud)
(以上所有内容都需要.NET 3.5)
如果您没有,您可以执行以下操作:
Control[] controls = this.Controls.Find("MyControl1");
if(controls.Lenght == 1) // 0 means not found, more - there are several controls with the same name
{
TextBox control = controls[0] as TextBox;
if(control != null)
{
control.Text = "Hello";
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22303 次 |
| 最近记录: |