0

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.

user1306322
  • 8,561
  • 18
  • 61
  • 122
  • 1
    Not really enough information in the question to tell. Mainly, what kind of application is it, ASP.Net, Windows Forms, WPF? – Kevin Aug 31 '12 at 16:48
  • This post is also useful for finding all textboxes: http://stackoverflow.com/questions/3419159/how-to-get-all-child-controls-of-a-winform-of-a-specific-type-button-textbox – user1306322 Aug 31 '12 at 18:55

4 Answers4

4

You can try with this code

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

} 
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
2

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

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • it comes when `.Enter` event is called for example. There are several events that will use dictionary and there are those like `.Leave` that use common methods – user1306322 Aug 31 '12 at 16:47
  • @user1306322: What dictionary though? You've told us there *is* a dictionary, but nothing about what it's mapping. Does this answer tell you all you need to know? It's hard to help any further with so little information... – Jon Skeet Aug 31 '12 at 16:48
  • There are several dictionaries for specific events and they contain textbox Name as key and a method as value. – user1306322 Aug 31 '12 at 16:49
  • I'm thinking from what the OP said above about the .Enter and .Leave events, he just needs to set up some event handlers to perform the actions. You can point multiple textboxes to the same handler and the sender parameter will have a copy of the textbox object where he can get the name and value. – Kevin Aug 31 '12 at 16:53
  • @Kevin I just don't know how to get the calling textbox to make its .Text what I want. – user1306322 Aug 31 '12 at 16:58
  • I haven't actually tried this, but give it a try... Inside the event handler: ((TextBox)sender).Text = "new text"; – Kevin Aug 31 '12 at 17:03
  • I tried this and it doesn't work for some reason. Loop just ends before the first iteration. – user1306322 Aug 31 '12 at 18:41
2

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";
}
coolmine
  • 4,427
  • 2
  • 33
  • 45
  • So I assume you want to update all the textboxes with the same text, correct ? Because the way I interpreted it is that you wanted to run different method for specific textboxes. – coolmine Aug 31 '12 at 17:04
  • No, each textbox gets its own text according to its name from one method when it's entered. When it loses focus, it is handled by another method which has a switch for the calling textbox's name. – user1306322 Aug 31 '12 at 17:06
  • So, let's see if I get this right. You want the text of the textbox to change when it gains focus to X. And change again when it looses focus to Y ? – coolmine Aug 31 '12 at 17:09
  • Yes. Also when a menu button is pressed, it also needs to iterate through all textboxes and perform actions specific to each textbox. – user1306322 Aug 31 '12 at 17:11
  • Updated the answer for the gaining/loosing focus part. You need to elaborate a bit more what you need the textboxs to do when the button is pressed. – coolmine Aug 31 '12 at 17:15
2

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];
    }
}
Tergiver
  • 14,171
  • 3
  • 41
  • 68