ComboBox LostFocus

  • Thread starter Thread starter Arne Garvander
  • Start date Start date
A

Arne Garvander

1. I make a selection in a combobox
2. I click outside my window on the desktop
Result:
The Combobox lostFocus fires multiple times until my application blows up.

How can I work around this problem?
 
I tried this with both Visual Studio 2005 and Visual Studio 2008, but
I'm unable to reproduce this problem. I created a WinForms
application with a combo box. I added an event handler to the "Leave"
event (There is no "lostFocus" event). I have 3 items in my ComboBox
to choose from. I select the 2nd item, then I click on my desktop.
The event is not fired at all. I can only get the event to fire if I
click on another control in the same application.

Have you stepped through your event to make sure the event you think
is firing is actually the event firing? Could it be the Deactivated
event of the form?

One work-around would be to have a flag to check for the event. For
example:

private bool IsComboBoxLeaving = false;
private void comboBox1_Leave(object sender, EventArgs e)
{
if (!this.IsComboBoxLeaving)
{
this.IsComboBoxLeaving = true;
//do your stuff...
this.IsComboBoxLeaving = false;
}
}

But I don't think this is your ultimate solution. First, make sure
the event you think is firing actually IS firing. Then, once you
narrowed down which event is firing, examine it's code to see if
there's something it's doing that's triggering a re-focus of the
control.

Put some debugging code into your event... something like:

private int MyLostFocusCount = 0;
private void comboBox1_Leave(object sender, EventArgs e)
{
this.MyLostFocusCount++;
this.Text = MyLostFocusCount.ToString();
/* comment out the rest of your event handler code, to see
if the above 2 lines execute repeatedly. */
}

Try to reproduce the problem. Does the title of the application show
a never ending incrementing number? If not, then the error is later
in your event handler. Uncomment small pieces of your code until
you've found the offending piece.

Hope this helps,
 
I put a trace listener in my lostfocus event, and I verified my findings.
I also changed the code in my lost focus event. My new code is not sensitive
to multiple events fired.

--
Arne Garvander
Certified Geek
Professional Data Dude


Mike {0A6FF490-CF84-4d78-BD85-FF011A0C31 said:
I tried this with both Visual Studio 2005 and Visual Studio 2008, but
I'm unable to reproduce this problem. I created a WinForms
application with a combo box. I added an event handler to the "Leave"
event (There is no "lostFocus" event). I have 3 items in my ComboBox
to choose from. I select the 2nd item, then I click on my desktop.
The event is not fired at all. I can only get the event to fire if I
click on another control in the same application.

Have you stepped through your event to make sure the event you think
is firing is actually the event firing? Could it be the Deactivated
event of the form?

One work-around would be to have a flag to check for the event. For
example:

private bool IsComboBoxLeaving = false;
private void comboBox1_Leave(object sender, EventArgs e)
{
if (!this.IsComboBoxLeaving)
{
this.IsComboBoxLeaving = true;
//do your stuff...
this.IsComboBoxLeaving = false;
}
}

But I don't think this is your ultimate solution. First, make sure
the event you think is firing actually IS firing. Then, once you
narrowed down which event is firing, examine it's code to see if
there's something it's doing that's triggering a re-focus of the
control.

Put some debugging code into your event... something like:

private int MyLostFocusCount = 0;
private void comboBox1_Leave(object sender, EventArgs e)
{
this.MyLostFocusCount++;
this.Text = MyLostFocusCount.ToString();
/* comment out the rest of your event handler code, to see
if the above 2 lines execute repeatedly. */
}

Try to reproduce the problem. Does the title of the application show
a never ending incrementing number? If not, then the error is later
in your event handler. Uncomment small pieces of your code until
you've found the offending piece.

Hope this helps,
 
Back
Top