How do I loop through all textboxes and make them run corresponding actions from action dictionary?(如何遍历所有文本框并使它们从动作字典中运行相应的动作?)
问题描述
我有大约 60 个文本框,当每个文本框都有自己的代码部分时,情况就变得一团糟.对于某些事件,动作是相同的,而对于其他事件,可以有一个带有动作的字典.问题是我不知道如何遍历文本框、获取当前文本框引用并运行一些方法来更改当前文本框的文本.
I have about 60 textboxes and it's a mess when each one has its own code section. For some events actions are the same and for others there can be a dictionary with actions. Problem is I don't know how to iterate through textboxes, get current textbox reference and run some methods which will change current textbox's text.
UPD: 我要做的是让所有文本框运行 AllTextboxesEnter
方法,该方法将根据文本框的名称更改其文本,运行 AllTextboxesLeave
方法,它将根据文本框的名称从动作字典中执行一些动作.
UPD: What I'm trying to do is make all textboxes run AllTextboxesEnter
method which will change their text according to textbox's name, run AllTextboxesLeave
method which will perform some action from action dictionary according to textbox's name.
推荐答案
假设我理解正确,这里是我对问题解释的回答
Assuming I understand the question correctly, here's my answer to the interpretation of the question
private void Form1_Load(object sender, EventArgs e)
{
foreach (TextBox tb in this.Controls.OfType<TextBox>())
{
tb.Enter += new EventHandler(textBoxAll_Enter);
tb.Leave += new EventHandler(textBoxAll_Leave);
}
}
private void textBoxAll_Enter(object sender, EventArgs e)
{
((TextBox)sender).Text = "textbox gained focus";
}
private void textBoxAll_Leave(object sender, EventArgs e)
{
((TextBox)sender).Text = "textbox lost focus";
}
这篇关于如何遍历所有文本框并使它们从动作字典中运行相应的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何遍历所有文本框并使它们从动作字典中运行相应的动作?


基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01