2

I have several comboboxes with the same properties.

Dropdownstyle : Dropdownlist
AutoCompleteMode: SuggestAppend
AutoCompleteSource: ListItems

For example, I have a dropdownlist cboxStates that has 50 states of United States of America in Items Collection entered manually. When I type in WI, it is highlighted among WA,WV,WI,WY but if I tab over/press enter/mouse click on another control, WA is selected instead of WI which is highlighted. This is total random and it happens to comboboxes that are binded dynamically. And also, they do not have any events.

Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
Latha
  • 23
  • 2

1 Answers1

1

This seems to be an issue that has been submitted to Connect. There's a workaround there which extends the default ComboBox control and fixes the issue. The extended ComboBox code is horribly formatted on the Connect site, so here's the nicer version :)

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
  • 68,824
  • 13
  • 156
  • 205
  • Thanks KeyboardP for your prompt reply. I created a new class called BetterComboBox.cs in my UI project and added your code. Now should I create combobox controls with this new class? Help please! – Latha Apr 18 '13 at 21:27
  • Yes, instead of the standard ComboBox you're using, use this new class. It will function the same as the normal one but also fix your issue. You should be able to see it at the top of your Toolbox in the designer view (if you want to drag and drop it like the other controls). – keyboardP Apr 18 '13 at 21:56
  • 1
    Thanks again.I recreated the class as a usercontrol, this way i can directly drag it to my form and the code above is working. – Latha Apr 19 '13 at 13:55