如何遍历所有文本框并使它们从动作字典中运行相应的动作? [英] How do I loop through all textboxes and make them run corresponding actions from action dictionary?

查看:28
本文介绍了如何遍历所有文本框并使它们从动作字典中运行相应的动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大约 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";
}

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆