C# 1.1 Windows Control LostFocus GotFocus question

J

Jason Huang

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
 
P

Peter Duniho

[...]
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
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top