Search code examples
c++buildertcombobox

Disabling mouse wheel and keyboard events in TComboBox


When I use TComboBox, user can select an item by clicking the control and selecting and item with mouse, or they can hover cursor over the control and use scroll wheel, or they can use keyboard when the control is selected.

How can I disable mouse wheel and keyboard events so that the user always has to click the control when they want to change the value? I want to prevent them from changing the value by accident. If this is not possible with TComboBox, is there some other combo box control that I could use?

For key press, I tried to disable the combo box on KeyDown event, but the selection still changes. TComboBox doesn't seem to have events for mouse wheel.


Solution

  • You can subclass the ComboBox's WindowProc property to intercept and discard scroll and keyboard window messages:

    private:
        TWndMethod PreviousWndProc;
    
    __fastcall TMyForm::TMyForm(TComponent *Owner)
    {
        PreviousWndProc = ComboBox1->WindowProc;
        ComboBox1->WindowProc = &ComboBoxWndProc;
    }
    
    void __fastcall TMyForm::ComboBoxWndProc(TMessage &Message)
    {
        if (
            ((Message.Msg < WM_KEYFIRST) || (Message.Msg > WM_KEYLAST))
            && (Message.Msg != WM_MOUSEWHEEL)
            )
        {
            PreviousWndProc(Message);
        }
    }