How do I loop through all textboxes and make them

2019-04-17 05:54发布

问题:

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: 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.

回答1:

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


回答2:

You can try with this code

foreach( var control in this.Controls.OfType<TextBox>() )
{
   control.Text = "";

} 


回答3:

Your question is far from clear, but I suspect you want something like:

foreach (var textBox in Controls.OfType<TextBox>())
{
    textBox.Text = /* insert action here */
}

It's not clear where the dictionary of actions comes in though...



回答4:

I think I understand what is being asked:

Updated

This version is better as it works with TextBoxes that were created via the designer (simulated InitializeControls), and it uses the Dictionaries to locate the TextBoxes that are found in the dictionary. Controls.Find is useful in case the TextBoxes are contained in sub-containers.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

class Form1 : Form
{
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

    private static readonly Dictionary<string, string> TextBoxEnterText = new Dictionary<string, string>
    {
        { "T1", "Enter T1" },
        { "T2", "Enter T2" },
        { "T3", "Enter T3" },
        { "T4", "Enter T4" },
        { "T5", "Enter T5" },
    };

    private static readonly Dictionary<string, string> TextBoxLeaveText = new Dictionary<string, string>
    {
        { "T1", "Leave T1" },
        { "T2", "Leave T2" },
        { "T3", "Leave T3" },
        { "T4", "Leave T4" },
        { "T5", "Leave T5" },
    };

    private void InitializeControls()
    {
        Controls.Add(new TextBox { Name = "T1", Location = new Point(10, 10) });
        Controls.Add(new TextBox { Name = "T2", Location = new Point(10, 40) });
        Controls.Add(new TextBox { Name = "T3", Location = new Point(10, 70) });
        Controls.Add(new TextBox { Name = "T4", Location = new Point(10, 100) });
        Controls.Add(new TextBox { Name = "T5", Location = new Point(10, 130) });
    }

    public Form1()
    {
        InitializeControls();

        foreach (string name in TextBoxEnterText.Keys)
            Controls.Find(name, true).First().Enter += textBox_Enter;
        foreach (string name in TextBoxLeaveText.Keys)
            Controls.Find(name, true).First().Leave += textBox_Leave;
    }

    static void textBox_Leave(object sender, EventArgs e)
    {
        TextBox textBox = (TextBox)sender;
        textBox.Text = TextBoxLeaveText[textBox.Name];
    }

    static void textBox_Enter(object sender, EventArgs e)
    {
        TextBox textBox = (TextBox)sender;
        textBox.Text = TextBoxEnterText[textBox.Name];
    }
}