Solved

C# GotFocus Event Looping

Posted on 2007-12-05
6
4,448 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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now