Changing ComboBox State from within SelectedIndexChanged

G

Guest

I often have a situation where within the SelecteIndexChanged event of a
ComboBox, I want to change the index again. For example, a changed index
triggers an action that can fail, in which case I want to return the index of
the combobox to its previous value. However, doing this within the event
handler of course triggers another invocation of the event handler. Is there
a standard idiom for handling this? I mean other than temporarily disabling
the event handler, which seems pretty kludgey. Thanks.
 
M

Maqsood Ahmed

Hello,
You can control the behavior using a boolean value something like
bInternal...

private void ComboBoxSelectedIndexChanged(object sender,EventArgs e)
{
if(!this.bInternal)
{
try
{
this.bInternal = true;
//Manipulate the values here.
}
catch
{
//Error Handling, logging etc.
}
finally
{
this.bInternal = false;
}
}
}

HTH. Cheers :)

Maqsood Ahmed [MCP C#,SQL Server]
Kolachi Advanced Technologies
http://www.kolachi.net
 
G

Guest

Hmm...it's actually simpler just to disable/enable the handler. But I like
this for a more general approach to preventing recursion. Thanks!
 

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