如何在 C# 中通过字符串访问 EventHandler 中的表单文本框?
How to access Form textboxes inside EventHandler by string in C#?
我正在使用 Visual Studio 2017。有一个带有文本框的表单。这些文本框需要每 10 秒刷新一次。为了实现这一点,我使用了一个定时器事件。
public partial class status_window : Form
{
public status_window()
{
InitializeComponent();
shutdown_button.Click += new EventHandler(shutdown_click);
Timer timer = new Timer();
timer.Interval = (1 * 10000); // 10 secs
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
}
timer_tick 函数是 status_window class 的成员。在该事件处理程序中,我可以按预期按名称访问文本框。但是,如果文本框 "adress" 是 i 变量,该怎么做。看:
private void timer_Tick(object sender, EventArgs e)
{
Int32 unixtime = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
// for all boxes per exname
for (int i = 0; i < something.Count() ; i++)
{
// try to find textbox 1 -> embty result
Console.WriteLine( this.Controls.Find("nam1_last_event", true) );
Console.WriteLine( this.Controls.Find("nam2_last_event", true) ); // also empty result
// this works and fills the formbox as ecxpected
nam1_last_event.Text = "somevalue";
nam1_event_count.Text = "anothervale";
nam2_last_event.Text = "somemorevalue";
nam2_event_count.Text = "andsoon";
// thats what i want later to use my for loop for those:
// something[i] exuals to nam1,nam2 and so on
this.Controls.Find( String.Format("{0}_last_event", something[i].ToLower()) , true)[0].Text = "somevalue"; // this fails cause array returned by find is empty
this.Controls.Find(String.Format("{0}_last_event", ex_name.ToLower()), true)[0].Text = "anothervale"; // same
}
}
所以我受限于自己的知识而卡在这里。 Google 上的大多数结果建议使用控件查找方法。
在您的代码中,您同样使用名称 nam1_last_event
作为 class status_window
的成员和控件名称。如果您的控制继电器名称为 nam1_last_event
.
,请检查设计器
函数Controls.Find
使用的键是控件属性Name
的值。
创建一个列表或字典变量来保存这些文本框,并在 timer_Tick.
中获取它
这对我有用:
var inPanel = this.Controls.Find("inPanel", true).OfType<TextBox>().FirstOrDefault();
inPanel?.Text = "Found it";
我正在使用 Visual Studio 2017。有一个带有文本框的表单。这些文本框需要每 10 秒刷新一次。为了实现这一点,我使用了一个定时器事件。
public partial class status_window : Form
{
public status_window()
{
InitializeComponent();
shutdown_button.Click += new EventHandler(shutdown_click);
Timer timer = new Timer();
timer.Interval = (1 * 10000); // 10 secs
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
}
timer_tick 函数是 status_window class 的成员。在该事件处理程序中,我可以按预期按名称访问文本框。但是,如果文本框 "adress" 是 i 变量,该怎么做。看:
private void timer_Tick(object sender, EventArgs e)
{
Int32 unixtime = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
// for all boxes per exname
for (int i = 0; i < something.Count() ; i++)
{
// try to find textbox 1 -> embty result
Console.WriteLine( this.Controls.Find("nam1_last_event", true) );
Console.WriteLine( this.Controls.Find("nam2_last_event", true) ); // also empty result
// this works and fills the formbox as ecxpected
nam1_last_event.Text = "somevalue";
nam1_event_count.Text = "anothervale";
nam2_last_event.Text = "somemorevalue";
nam2_event_count.Text = "andsoon";
// thats what i want later to use my for loop for those:
// something[i] exuals to nam1,nam2 and so on
this.Controls.Find( String.Format("{0}_last_event", something[i].ToLower()) , true)[0].Text = "somevalue"; // this fails cause array returned by find is empty
this.Controls.Find(String.Format("{0}_last_event", ex_name.ToLower()), true)[0].Text = "anothervale"; // same
}
}
所以我受限于自己的知识而卡在这里。 Google 上的大多数结果建议使用控件查找方法。
在您的代码中,您同样使用名称 nam1_last_event
作为 class status_window
的成员和控件名称。如果您的控制继电器名称为 nam1_last_event
.
函数Controls.Find
使用的键是控件属性Name
的值。
创建一个列表或字典变量来保存这些文本框,并在 timer_Tick.
中获取它这对我有用:
var inPanel = this.Controls.Find("inPanel", true).OfType<TextBox>().FirstOrDefault();
inPanel?.Text = "Found it";