Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C# GotFocus Event Looping

Posted on 2007-12-05
6
Medium Priority
?
4,571 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.
1
 
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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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 1500 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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 …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

782 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