Solved

C# GotFocus Event Looping

Posted on 2007-12-05
6
4,482 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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