By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,686 Members | 2,392 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,686 IT Pros & Developers. It's quick & easy.

C# 1.1 Windows Control LostFocus GotFocus question

P: n/a
Hi,

In my .Net 1.1 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.
Jason
Jul 17 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Mon, 16 Jul 2007 19:41:28 -0700, Jason Huang
<Ja************@hotmail.comwrote:
[...]
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?
Every time your MessageBox pops up, it causes the loss of focus for
whatever window and control has focus at the time. Which causes the
MessageBox to pop up again. Etc.

It was suggested to you, in a different thread, that you use the debug
console output to monitor focus changes. IMHO, that was the correct
suggestion, and doing so would avoid having the monitoring itself
interfere with the behavior you're monitoring.

So do that instead.

Pete
Jul 17 '07 #2

P: n/a

Since you have only 2 controls on your form, when one loses focus the other
receives it. This implies that you see the lost focus pop up box for one
control and once you click ok, you see the get focus of the other control.
This sequence repeats over and over. You can also use debug statements to
debug your code and avoid having to click any buttons on pop up windows.

Adrian
"Jason Huang" wrote:
Hi,

In my .Net 1.1 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.
Jason
Jul 17 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.