简体   繁体   中英

Recurssion + .Net Compact framework

I am working on .Net compact framework. As per requirement we have drop down and text box; When user selected value from drop down then selected index changed event fires and value gets updated in textbox. Now suppose user typed value in textbox then we need to again set the drop downs selected index.

so with this above scenario it went to recursion.

please help me out to resolve this.

Two simple solutions come to my mind:

a) A flag
Use a simple variable _preventRecursion to indicate which update should take place, for example like this:

private volatile bool _preventRecursion;

public void TextBox_TextChanged(...)
{
    if (!_preventRecursion)
    {
        _preventRecursion = true;
        try
        {
            // Do stuff to update the combo box.
        }
        finally
        {
            _preventRecursion = false;
        }
    }
}

Add the same code to the event handler for the combo box.

b) Manually attach event handlers
If you manually attach the event handlers in your code, you could control when the events are available:

public void TextBox_TextChanged(...)
{
    comboBox1.SelectedIndexChanged -= selectedIndexChangedEventHandler;
    try
    {
        // Do stuff to update the combo box
    }
    finally
    {
        comboBox1.SelectedIndexChanged += selectedIndexChangedEventHandler;
    }
}

selectedIndexChangedEventHandler would be a proper delegate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM