Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello.

I have 7 textBox in my Form1

textBox0
textBox1
.
.
textBox6

my question:

I have some event for each textbox (doing exactly the same thing, but for each textBox):

C#
private void textBox0_Leave(object sender, EventArgs e)
        {
            if (textBox0.Text == "")
            {
                textBox0.Text = "0";
            }
        }


Is there a way to do this in one event like this:
C#
private void textBox[FOR ALL]_Leave(object sender, EventArgs e)
        {
            if (textBox[FOR ALL].Text == "")
            {
                textBox[FOR ALL].Text = "0";
            }
        }
Posted
Comments
Philippe Mori 23-Aug-15 21:11pm    
Not hard to try yourself... In fact, you can select an existing compatible handler directly from the designer by picker an handler instead of double-clicking which would generate default handler).

 
Share this answer
 
Instead of handling these events in the form, make your own version of the TextBox that does this by creating a new class and inheriting from TextBox. You can then customize the TextBox to do the things you want, compile it and it'll show up in the ToolBox. Once there, you simply drag that TextBox onto your form and use that in place of the standard TextBox control.
 
Share this answer
 
You can do something like this:

C#
private void TextBox_event(Control ctl)
{
    foreach (Control c in ctl.Controls)
    {
        if (c.HasChildren)
            TextBox_event(c);
        else
        {
            if (c.GetType() != typeof (TextBox)) continue;
            c.Leave += TextBoxLeave;
        }
    }
}

private static void TextBoxLeave(Object sender, EventArgs e)
{
    var value = ((TextBox) sender).Text;
    if (value == "")
        ((TextBox) sender).Text = "0";
}
 
Share this answer
 
This would be the easiest solution I can come up with.
First you create an event handler for one textbox, then you link the event to all the other textboxes, so they all fire the same event.

The fun thing about an event handler is that it contains the 'sender', which is a reference to the object raising the event. This would be one of the textboxes linked to it.

The only thing left is to take the object, cast it as a textbox, and alter it the way you want.

Simply this:

C#
private void textBox0_Leave(object sender, EventArgs e)
{
    if (((TextBox)sender).Text == "")
    {
        ((TextBox)sender).Text = "0";
    }
}
 
Share this answer
 
In addition to Solution 4 you can also do the ff. Its up to you which style of programming you prefer.


C#
private void Form1_Load(object sender, EventArgs e)
	{
                txtbox1.Leave +=textBox_Leave;
                txtbox2.Leave += textBox_Leave;
                txtbox3.Leave +=textBox_Leave;
                ///etc...
	}


C#
private void textBox_Leave(object sender, EventArgs e)
        {
           if (((TextBox)sender).Text == "")
            {
             ((TextBox)sender).Text = "0";
            }
        }
 
Share this answer
 
Perhaps you take a look to here :
Method to check which Radio button is checked always returns null[^]

In fact it is not a TextBox and also not the TextChanged-Event which fires ... but I think it is nevertheless useful for you ...

(First of all the Soultions (both) should be interesting for you)
 
Share this answer
 
v2
Although one could hook event from multiple control to the same method using the designer (as in Solution 4), I generally prefer to generate one method per control (and per event). This can make maintenance easier if you have to do something different for some controls.

Hooking event by code as in Solution 3 could make sense if you have a lot of controls in a single form that need that specific logic. Even then I would probably only hook controls directly on the form or in a specific panel.

However in practice, a form might contains some TextBox that does not contains numbers so using a loop migth not always be the best solution. Sometime, creating an array of controls (TextBox in this case) explicitly, might be the best solution.

Solution 2 make sense if it is something you want to do for any forms. In that case, you might want to have a property to enable/disable the special behavior. For example, if 0 is not valid in one case, then it does not make much sense to replace empty field by 0. One advantage of that solution is that you can add other features like special handling of decimal point from the numeric pad of the keyboard if regional setting use a comma instead of a dot for that separator or you get reformat number with thousand separator too.

Generally I tend to prefer the approach of having distinct handler calling a common function. It make it also easy to pass some additional information to the method like the "index" of the control or that data variable content or reference (and thus avoid ugly switch and cast in the common code).

For completeness, official documentation (MSDN) as proposed by solution 1 is always a good place to start.

C#
private void textBox0_Leave(object sender, EventArgs e)
{
    HandleTextBoxLeave(textBox0, 0);
}

private void textBox1_Leave(object sender, EventArgs e)
{
    HandleTextBoxLeave(textBox1, 1);
}

private void textBox2_Leave(object sender, EventArgs e)
{
    HandleTextBoxLeave(textBox2, 2);
}

void HandleTextBoxLeave(TextBox textBox, int index)
{
    if (string.IsNullOrEmpty(textBox.Text))
    {
        textBox.Text = "0";
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900