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

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

问题描述

我有大约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天全站免登陆