简体   繁体   English

从 Windows 窗体控件中按名称查找控件

[英]Find control by name from Windows Forms controls

I have a list of my textbox names, and I want to find a control by name.我有一个文本框名称列表,我想按名称查找控件。 How is it possible?这怎么可能?

Use Control.ControlCollection.Find .使用Control.ControlCollection.Find

TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";

EDIT for asker:编辑提问者:

Control[] tbxs = this.Controls.Find(txtbox_and_message[0,0], true);
if (tbxs != null && tbxs.Length > 0)
{
    tbxs[0].Text = "Found!";
}

You can use:您可以使用:

f.Controls[name];

Where f is your form variable.其中f是您的表单变量。 That gives you the control with name name .这为您提供了名称name的控件。

TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
tbx.Text = "found!";

If Controls.Find is not found "textBox1" => error.如果 Controls.Find 未找到“textBox1”=> 错误。 You must add code.您必须添加代码。

If(tbx != null)

Edit:编辑:

TextBox tbx = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
If(tbx != null)
   tbx.Text = "found!";

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

相关问题 按名称在 Windows 窗体中查找控件 - Find a control in Windows Forms by name 在C#Windows窗体中从Control.Controls绘制控件 - Drawing control from Control.Controls in C# Windows Forms 如何遍历Windows窗体表单中的所有控件或如何查找特定控件是否是容器控件? - How to loop through all controls in a Windows Forms form or how to find if a particular control is a container control? 从现有控件创建Xamarin Forms控件 - Creating Xamarin Forms control from existing controls 将Windows.Controls.UserControl转换为Windows.Forms.Control - Casting Windows.Controls.UserControl to Windows.Forms.Control WCF,从服务访问Windows窗体控件 - WCF, Accessing a windows forms controls from a service C# - 带有子控件和数据绑定的 Windows 窗体用户控件 - C# - Windows Forms User Control with Sub controls and data binding 在Windows窗体中,在窗体上添加此控件后,控件的应用程序位置已更改 - In Windows forms application location of controls changed after adding this control on form 在 C# 中按名称获取 Windows 窗体控件 - Get a Windows Forms control by name in C# 基于控件的安全性,无法从树形视图中删除数据库条目,从Windows窗体应用程序中基于控件的安全性中获取代码 - Control based Security, cant delete from treeview the database entries,took code from controls-based-security-in-a-windows-forms-application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM