c# - 控件不在焦点时下拉列表更改所选值

标签 c# winforms .net-4.0 combobox

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

Dropdownstyle : Dropdownlist
AutoCompleteMode: SuggestAppend
AutoCompleteSource: ListItems

例如,我有一个下拉列表 cboxStates,在项目集合中手动输入了 50 个美利坚合众国州。当我输入 WI 时,它会在 WA、WV、WI、WY 之间突出显示,但如果我在另一个控件上切换/按回车键/鼠标单击,则会选择 WA 而不是突出显示的 WI。这完全是随机的,它恰好发生在动态绑定(bind)的组合框上。而且,他们没有任何事件。

最佳答案

这似乎是一个提交给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;  
        } 
    }
} 

关于c# - 控件不在焦点时下拉列表更改所选值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16092719/

相关文章:

c# - 我如何计算使用 DateTime 已经过了多少时间?

winforms - Winforms 控件标签的用途是什么?

c# - 使用 LINQ 对备用对进行分组

c# - Asp.net 中的日期时间选择器

.net - .net 中没有间隙的柱形图

.net-4.0 - 在并行调用中,限制每秒执行

C#.NET 4.0 并发字典 : TryRemove within a lock?

c# - 如何获得 System.Threading.Tasks.Task 已完成的通知

c# - iTextSharp Image.GetInstance 的 EMF 格式内存流

c# - 可以加载到 Windows 窗体控件中的最大数据量是多少?