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

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

Post by robhindma » Sun, 24 Aug 2003 05:45:38


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.
 
 
 

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

Post by lionsh » Tue, 26 Aug 2003 14:54:35

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! 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: XXXX@XXXXX.COM (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://www.yqcomputer.com/
| Lines: 29
| Message-ID: < XXXX@XXXXX.COM >
| 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: XXXX@XXXXX.COM
| 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.
|

 
 
 

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

Post by lionsh » Tue, 26 Aug 2003 15:10:54

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! 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: XXXX@XXXXX.COM (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://www.yqcomputer.com/
| Lines: 29
| Message-ID: < XXXX@XXXXX.COM >
| 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: XXXX@XXXXX.COM
| 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.
|