Solved

C# GotFocus Event Looping

Posted on 2007-12-05
6
4,490 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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