如何遍历所有文本框并使它们从动作字典中运行相应的动作?

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";
}

这篇关于如何遍历所有文本框并使它们从动作字典中运行相应的动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)