【问题标题】:Dropdownlist changing the selected value when control is out of focus控件失焦时更改所选值的下拉列表
【发布时间】:2013-04-18 20:43:47
【问题描述】:

我有几个具有相同属性的组合框。

Dropdownstyle : Dropdownlist
AutoCompleteMode: SuggestAppend
AutoCompleteSource: ListItems

例如,我有一个下拉列表 cboxStates,它在 Items Collection 中手动输入了 50 个美国州。当我输入 WI 时,它会在 WA、WV、WI、WY 中突出显示,但如果我在另一个控件上按 Tab/按 Enter/鼠标单击,则会选择 WA 而不是突出显示的 WI。这是完全随机的,它发生在动态绑定的组合框上。而且,他们没有任何活动。

【问题讨论】:

    标签: c# winforms .net-4.0 combobox


    【解决方案1】:

    这似乎是已提交给Connect 的问题。有一种解决方法可以扩展默认的 ComboBox 控件并解决问题。扩展的 ComboBox 代码在 Connect 站点上的格式非常糟糕,所以这里是更好的版本:)

    public class BetterComboBox : ComboBox 
    {
        private int _windows7CorrectedSelectedIndex = -1;
        private int? _selectedIndexWhenDroppedDown = null; 
    
        protected override void OnDropDown(EventArgs e)
        {  
            _selectedIndexWhenDroppedDown = SelectedIndex;    
            base.OnDropDown(e);
        }
    
        private bool _onDropDownClosedProcessing = false; 
    
        protected override void OnDropDownClosed(EventArgs e) 
        { 
            if (_selectedIndexWhenDroppedDown != null && _selectedIndexWhenDroppedDown != SelectedIndex)   
            {    
                try   
                {      
                    _onDropDownClosedProcessing = true;     
                    OnSelectionChangeCommitted(e);   
                }    
                finally   
                { 
                    _onDropDownClosedProcessing = false;   
                }   
            }   
            base.OnDropDownClosed(e);   
            if (SelectedIndex != _windows7CorrectedSelectedIndex)   
            {  
                SelectedIndex = _windows7CorrectedSelectedIndex;      
                OnSelectionChangeCommitted(e);  
            }
        } 
    
        protected override void OnSelectionChangeCommitted(EventArgs e)
        {   
            if (!_onDropDownClosedProcessing)
                _windows7CorrectedSelectedIndex = SelectedIndex;    
            _selectedIndexWhenDroppedDown = null; 
            base.OnSelectionChangeCommitted(e); 
        } 
    
        protected override void OnSelectedIndexChanged(EventArgs e)
        {  
            bool alreadyMatched = true;   
            if (_windows7CorrectedSelectedIndex != SelectedIndex)  
            {  
                _windows7CorrectedSelectedIndex = SelectedIndex; 
                alreadyMatched = false; 
            }   
            base.OnSelectedIndexChanged(e);  
    
            //when not dropped down, the SelectionChangeCommitted event does not fire upon non-arrow keystrokes due (I suppose) to AutoComplete behavior   
            //this is not acceptable for my needs, and so I have come up with the best way to determine when to raise the event, without causing duplication of the event (alreadyMatched)   
            //and without causing the event to fire when programmatic changes cause SelectedIndexChanged to be raised (_processingKeyEventArgs implies user-caused)   
            if (!DroppedDown && !alreadyMatched && _processingKeyEventArgs)
                OnSelectionChangeCommitted(e); 
        } 
    
        private bool _processingKeyEventArgs = false;
    
        protected override bool ProcessKeyEventArgs(ref Message m)
        {  
            try   
            {   
                _processingKeyEventArgs = true; 
                return base.ProcessKeyEventArgs(ref m);  
            }  
            finally 
            { 
                _processingKeyEventArgs = false;  
            } 
        }
    } 
    

    【讨论】:

    • 感谢 KeyboardP 的及时回复。我在我的 UI 项目中创建了一个名为 BetterComboBox.cs 的新类并添加了您的代码。现在我应该用这个新类创建组合框控件吗?请帮忙!
    • 是的,使用这个新类,而不是您正在使用的标准 ComboBox。它的功能与普通功能相同,但也可以解决您的问题。您应该能够在设计器视图的工具箱顶部看到它(如果您想像其他控件一样拖放它)。
    • 再次感谢。我将这个类重新创建为用户控件,这样我可以直接将它拖到我的表单中,上面的代码就可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 2011-09-09
    • 2017-06-10
    • 1970-01-01
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多