comboBox TextChanged event fires twice because Items.Insert() fires the event too...

  • Thread starter Thread starter Rob Hindman
  • Start date Start date
R

Rob Hindman

Hi, I have a simple windows form, with two ComboBox controls (and
several other controls). For both of the comboBox controls,
DropDownStyle=DropDown.
For some reason that I can't figure out, when the user pastes text
into the comboBox, the event fires twice. At first I didn't notice
this, but once I added error checking, I started seeing my error
twice. Here some example code:

private void comboBox_doit_TextChanged(object sender, System.EventArgs
e)
{
...
comboBox_doit.Items.Insert(1,comboBox_doit.Text);
...
}

Does anyone know how to prevent the second event from firing? I have
tried using BeginUpdate() & EndUpdate() around Items.Insert(), I have
tried using a simple bool semaphore, I have even tried unregistering
the event before calling Items.Insert(), and then adding a new event
handler after it...

I also can not figure out why this only happens to one of my
comboBoxes, and not the other one... Hmmm...

Alas, I am stuck... I would be grateful for any comments, suggestions,
or advice...
Many Thanks,
-Rob.
 
Hello Rob,

There is no problem with the sample code you paste. I think the reason
should be other code in the project. Could you please paste more code to
reproduce the issue, and I'd like to help you troubleshooting it.

Best regards,

Lion Shi [MSFT]
MCSE, MCSD
Microsoft Support Engineer
Get Secure! ¨C www.microsoft.com/security

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2003 Microsoft Corporation. All rights
reserved.
--------------------
| From: (e-mail address removed) (Rob Hindman)
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Subject: comboBox TextChanged event fires twice because Items.Insert()
fires the event too...
| Date: 22 Aug 2003 13:45:38 -0700
| Organization: http://groups.google.com/
| Lines: 29
| Message-ID: <[email protected]>
| NNTP-Posting-Host: 131.107.3.86
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1061585140 13270 127.0.0.1 (22 Aug 2003
20:45:40 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: 22 Aug 2003 20:45:40 GMT
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!npeer.de.kpn-eurorings.net!news-out.nuthinbutnews.com!propagator2-sterl
ing!news-in-sterling.newsfeed.com!tdsnet-transit!newspeer.tds.net!sn-xit-02!
sn-xit-06!sn-xit-09!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:50793
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Hi, I have a simple windows form, with two ComboBox controls (and
| several other controls). For both of the comboBox controls,
| DropDownStyle=DropDown.
| For some reason that I can't figure out, when the user pastes text
| into the comboBox, the event fires twice. At first I didn't notice
| this, but once I added error checking, I started seeing my error
| twice. Here some example code:
|
| private void comboBox_doit_TextChanged(object sender, System.EventArgs
| e)
| {
| ...
| comboBox_doit.Items.Insert(1,comboBox_doit.Text);
| ...
| }
|
| Does anyone know how to prevent the second event from firing? I have
| tried using BeginUpdate() & EndUpdate() around Items.Insert(), I have
| tried using a simple bool semaphore, I have even tried unregistering
| the event before calling Items.Insert(), and then adding a new event
| handler after it...
|
| I also can not figure out why this only happens to one of my
| comboBoxes, and not the other one... Hmmm...
|
| Alas, I am stuck... I would be grateful for any comments, suggestions,
| or advice...
| Many Thanks,
| -Rob.
|
 
Hello Rob,

Further more, I don't think the TextChanged event is fired by the Insert
method. If so, the event will be fired recursively, untill the stack
overflows. Hence the problem should be other code.

Best regards,

Lion Shi [MSFT]
MCSE, MCSD
Microsoft Support Engineer
Get Secure! ¨C www.microsoft.com/security

This posting is provided "AS IS" with no warranties, and confers no rights.
You assume all risk for your use. 2003 Microsoft Corporation. All rights
reserved.
--------------------
| From: (e-mail address removed) (Rob Hindman)
| Newsgroups: microsoft.public.dotnet.framework.windowsforms
| Subject: comboBox TextChanged event fires twice because Items.Insert()
fires the event too...
| Date: 22 Aug 2003 13:45:38 -0700
| Organization: http://groups.google.com/
| Lines: 29
| Message-ID: <[email protected]>
| NNTP-Posting-Host: 131.107.3.86
| Content-Type: text/plain; charset=ISO-8859-1
| Content-Transfer-Encoding: 8bit
| X-Trace: posting.google.com 1061585140 13270 127.0.0.1 (22 Aug 2003
20:45:40 GMT)
| X-Complaints-To: (e-mail address removed)
| NNTP-Posting-Date: 22 Aug 2003 20:45:40 GMT
| Path:
cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onlin
e.de!npeer.de.kpn-eurorings.net!news-out.nuthinbutnews.com!propagator2-sterl
ing!news-in-sterling.newsfeed.com!tdsnet-transit!newspeer.tds.net!sn-xit-02!
sn-xit-06!sn-xit-09!supernews.com!postnews1.google.com!not-for-mail
| Xref: cpmsftngxa06.phx.gbl
microsoft.public.dotnet.framework.windowsforms:50793
| X-Tomcat-NG: microsoft.public.dotnet.framework.windowsforms
|
| Hi, I have a simple windows form, with two ComboBox controls (and
| several other controls). For both of the comboBox controls,
| DropDownStyle=DropDown.
| For some reason that I can't figure out, when the user pastes text
| into the comboBox, the event fires twice. At first I didn't notice
| this, but once I added error checking, I started seeing my error
| twice. Here some example code:
|
| private void comboBox_doit_TextChanged(object sender, System.EventArgs
| e)
| {
| ...
| comboBox_doit.Items.Insert(1,comboBox_doit.Text);
| ...
| }
|
| Does anyone know how to prevent the second event from firing? I have
| tried using BeginUpdate() & EndUpdate() around Items.Insert(), I have
| tried using a simple bool semaphore, I have even tried unregistering
| the event before calling Items.Insert(), and then adding a new event
| handler after it...
|
| I also can not figure out why this only happens to one of my
| comboBoxes, and not the other one... Hmmm...
|
| Alas, I am stuck... I would be grateful for any comments, suggestions,
| or advice...
| Many Thanks,
| -Rob.
|
 
Back
Top