简体   繁体   English

递归+ .Net Compact框架

[英]Recurssion + .Net Compact framework

I am working on .Net compact framework. 我正在研究.Net紧凑框架。 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 a)一个标志
Use a simple variable _preventRecursion to indicate which update should take place, for example like this: 使用一个简单的变量_preventRecursion指示应该进行哪个更新,例如:

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 b)手动附加事件处理程序
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. selectedIndexChangedEventHandler将是一个适当的委托。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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