all groups > dotnet windows forms > april 2008 >
You're in the

dotnet windows forms

group:

ComboBox LostFocus



ComboBox LostFocus Arne Garvander
4/21/2008 7:21:00 AM
dotnet windows forms: 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?



--
Arne Garvander
Certified Geek
Re: ComboBox LostFocus Mike {0A6FF490-CF84-4d78-BD85-FF011A0C310C}
4/21/2008 3:27:29 PM
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 =3D false;
private void comboBox1_Leave(object sender, EventArgs e)
{
if (!this.IsComboBoxLeaving)
{
this.IsComboBoxLeaving =3D true;
//do your stuff...
this.IsComboBoxLeaving =3D 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 =3D 0;
private void comboBox1_Leave(object sender, EventArgs e)
{
this.MyLostFocusCount++;
this.Text =3D 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,

--------------------------
Owner/Operator of
www.MichaelsAttic.com



On Apr 21, 10:21=A0am, Arne Garvander
[quoted text, click to view]
Re: ComboBox LostFocus Arne Garvander
4/22/2008 6:08:02 AM
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


[quoted text, click to view]
AddThis Social Bookmark Button