Click here to Skip to main content
16,018,114 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working with text boxes A, B, C & D; they are all sharing only one LEAVE EVENT, the one I have pasted bellow.
C#
private void txtA_Leave(object sender, EventArgs e)
        {
            string[] y = new string[] { "txtA", "txtB", "txtC", "txtD"};
            if (y.Contains(ActiveControl.Name) && ActiveControl.Text == string.Empty)
            {
                ActiveControl.Text = 0.0;
                ActiveControl.TextAlign = HorizontalAlignment.Center;
            }
        }
When I leave A for B, C or D, I expected txtA_Leave to execute but its not working; someone help me to amend the code please.

Thank you.
Posted
Updated 4-May-14 4:08am
v3
Comments
[no name] 4-May-14 9:37am    
Did you wire up the Leave event for txtA? What does "its not working" mean?
Salisu Shaibu 4-May-14 9:45am    
@Wes Aday:
I mean, no execution!
Thanks.
[no name] 4-May-14 9:52am    
Then you did not wire up the event to the control.

First of all, you can't have put a double in the Text property, you need to put a string in it, for example "0.0" instead of 0.0.

Also, instead of ActiveControl, use the sender attribute.

It is also better to use String.IsNullOrEmpty instead of == String.Empty because now you also check whether the string is null. And if you consider a whitespace-only string as 'empty', then use IsNullOrWhiteSpace.

Put the y variable outside the method; this avoids that it gets recreated every time the method gets called.
C#
string[] y = new string[] { "txtA", "txtB", "txtC", "txtD"};
private void txtA_Leave(object sender, EventArgs e)
        {
            if (!(sender is TextBox))
            {
                 return; // 'sender' cannot be converted to a TextBox
            }
            TextBox control = sender as TextBox;
            if (y.Contains(control.Name) && String.IsNullOrEmpty(control.Text))
            {
                control.Text = 0.0;
                control.TextAlign = HorizontalAlignment.Center;
            }
        }

More information about the sender attribute:
http://stackoverflow.com/questions/1303145/net-events-what-are-object-sender-eventargs-e[^]
 
Share this answer
 
v2
Comments
Salisu Shaibu 4-May-14 10:17am    
Thanks very much man, just perfect.
You're appreciated.
Shaibu.
Thomas Daniels 4-May-14 10:28am    
You're welcome!
Drop the ActiveControl stuff: every Event hands you the instance of the class that raised it as the sender parameter.
So:
C#
private static string[] y = new string[] { "txtA", "txtB", "txtC", "txtD"};
private void txtA_Leave(object sender, EventArgs e)
    {
        TextBox tb = sender as TextBox;
        if (tb != null)
            {   
            if (y.Contains(tb.Text))
            {
                tb.Text = "0.0";
                tb.TextAlign = HorizontalAlignment.Center;
            }
        }
 
Share this answer
 
Comments
Thomas Daniels 4-May-14 10:11am    
Your Contains method checks for the text, not for the name. Also, the check for empty string seems to be gone.
OriginalGriff 4-May-14 10:17am    
Yeah - I've seen a couple of questions by this poster, and he's trying to do things the hard way.
http://www.codeproject.com/Answers/768528/Help-correct-my-codes-for-me-please#answer1
And I suspect this is related:
http://www.codeproject.com/Answers/768342/How-to-change-textbox-within-an-if-statement#answer1
Salisu Shaibu 4-May-14 10:20am    
Thank very much. You solved the problem.
OriginalGriff 4-May-14 10:21am    
You're welcome!

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