Solved

C# GotFocus Event Looping

Posted on 2007-12-05
6
4,470 Views
Last Modified: 2013-12-16
Hi all,

In my C# windows form Form1 I have nothing but 2 TextBox controls
on the Form1, the T1 and T2.
I would like to test the sequence of the GotFocus LostFocus things, so I
have the following code on the Form1.

this.T1.LostFocus +=new EventHandler(T1_LostFocus);
this.T1 .GotFocus +=new EventHandler(T1_GotFocus);
this.T2 .LostFocus +=new EventHandler(T2_LostFocus);
this.T2 .GotFocus += new EventHandler(T2_GotFocus);

protected void T1_LostFocus(object sender, EventArgs e)
{
MessageBox.Show("T1_LostFocus");
}

protected void T1_GotFocus(object sender, EventArgs e)
{
MessageBox.Show("T1_GotFocus");
}

protected void T2_LostFocus(object sender, EventArgs e)
{
MessageBox.Show("T2_LostFocus");
}

protected void T2_GotFocus(object sender, EventArgs e)
{
MessageBox.Show("T2_GotFocus");
}

There you go! When I run the Form1, the MessageBox just keep popping out
like an infinitive loop!
I am wondering why that happens! Am I missing anything in testing the
LostFocus GotFocus testing?
Thanks for help.
0
Comment
Question by:AdibRahman77
6 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20412945
well, this is not  a good idea, when you use MessageBox, your form loose the focus either.
You can use System.Diagnostics.Trace.WriteLine() instead. The message will appear in the debugger window in you Visual Studio IDE, without stealing the focus.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 20412948
When you show the MessageBox, the focus is lost, and when you hide the MessageBox, a control gets focus, which shows a MessageBox, and ...

Bob
0
 
LVL 10

Expert Comment

by:Nash2334
ID: 20412955
It's not looping - when you fire a MessageBox you cause the control to lose focus, hence the repeated message.

If you want to test this, use Console.Writeline instead and view the output in the IDE.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Expert Comment

by:arshadd
ID: 20413164
You are not missing anything its just that when you focus on T1 the your message box is fired. and it will get the focus so the lost focus event of T1 is fired this will go on again and again. instead of messagebox use Console.WriteLine("T1_GotFocus") and check the output window of visual studio.
0
 

Author Comment

by:AdibRahman77
ID: 20413649
Sorry guys the whole idea of question is when I am debugging and putting a break point I see the infinite loop repeating on the methods.
I am just testing this.The whole thing I wanna do is chnage the forecolor of the button on getfocus and lostfocus.I am able to do it using this concept.

When I am loading the Form I set the Focus to T1.But I am wondering why this infite loop to call the methods  T1_GotFocus & T1_LostFocus happening on form Load. If u put a break point on these methods u will see it keeps on invoking again and again.

Is there any other way to implement the chnage of Forecolor on focus change. I dont want the loop to occur.
0
 
LVL 3

Accepted Solution

by:
Aurora27 earned 500 total points
ID: 20414691
Debugging a focus event is tricky. Because, when you switch between debug mode and exe , the event is triggered again.

Why dont you just set whaevr you want in the gotfocus and lostfocus event clearly instead of in the form load.

I tried it for you:

private void textBox1_Enter(object sender, EventArgs e)
        {
            textBox1.BackColor = Color.Green;
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            textBox1.BackColor = Color.Blue;
        }

        private void textBox2_Enter(object sender, EventArgs e)
        {
            textBox2.BackColor = Color.Green;
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            textBox2.BackColor = Color.Violet ;
        }


This works perfectly for alternating colors.

CT.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question