0

I've found numerous things in this regard, but they're either for third-party controls, different specific situations, or for a completely different language. What I need should be fairly simple.

I have a TDBGrid with a TComboBox placed over a specific cell. As the user scrolls through this grid, the combo box moves along to the corresponding cell. User is further able to change the value of this combo box to update the database.

However, when using the mouse wheel to scroll, if the mouse pointer just happens to be over this combo box, it ends up changing the value of the combo box, rather than scrolling the grid.

How do I suppress the scrolling in the combo box?

Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327

1 Answers1

0

I am answering this QA style, because I found such a simple solution on my own which I couldn't find on various forums.

The simplest method is to add an OnKeyDown event handler to the TComboBox, and add the following:

procedure TfrmMain.cboStatusKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key in [VK_UP, VK_DOWN] then
    Key:= 0;
end;

Keep in mind that this also blocks keyboard up/down events. If you also wish to block keyboard left/right events, then you can also do this:

procedure TfrmMain.cboStatusKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key in [VK_UP, VK_DOWN, VK_LEFT, VK_RIGHT] then
    Key:= 0;
end;
Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327
  • 1
    So if you don't have a pointing device, you cannot navigate the combo box at all? Not even drop it down using Alt+Down? – Andreas Rejbrand Jan 27 '22 at 21:51
  • @andreas I highly doubt anybody is using a PC in 2022 without some sort of pointing device. The UI is very mouse-centric, and isn't even an application which will be distributed to anyone. If you can provide a simple alternative solution, that would be great. – Jerry Dodge Jan 27 '22 at 22:33
  • I know! There will be a pointing device. My *actual* concern is that you then are forced to use it. I typically don't use my mouse, because it is so much faster to do things with the keyboard. (For instance, if I want to spell-check the contents of my clipboard, I press Win+5, Ctrl+N, Ctrl+V, F7, Enter, Ctrl+W, N, Alt+Space, M to start Microsoft Word (the fifth button on my taskbar on every computer I use), create a new document, paste the contents of the clipboard, perform a spell check (F7), confirm the lack of spelling errors (Enter), close the document (Ctrl+W), Not saving, opening the ... – Andreas Rejbrand Jan 27 '22 at 22:35
  • ...window's system menu and activating the Minimera (Swedish for Minimize) action to get Word back to the taskbar. This takes me no longer than a couple of seconds. Although this is only one example, I think it clearly shows that you can be much more efficient with the keyboard if you have the right kind of autism -- and if you are that kind of person, you will get very annoyed by user interfaces that requires the use of a pointing device.) – Andreas Rejbrand Jan 27 '22 at 22:37
  • 3
    I don't know about the `TDBGrid` (I have never used it), but a similar problem happens in an ordinary `TScrollBox`. Well, before Delphi 11, the scroll box didn't support the mouse wheel, so you had to write your own code for that. And then you have to consider what happens if the scroll box contains a combo box. As you say, it is very easy to scroll the combo box by mistake! My golden rule is that IF the cursor is above a scrollable control (like a combo box) AND that control has keyboard focus, THEN scrolling happens in that control. OTHERWISE, scrolling happens in the scroll box. – Andreas Rejbrand Jan 27 '22 at 22:44
  • 1
    Here's an example implementation of that: https://algosim.org/SynViewSource/ScrollBoxEx.html – Andreas Rejbrand Jan 27 '22 at 22:44
  • Personally, I wouldn't put the `TComboBox` over the `TDBGrid` to begin with. I would put the `TComboBox` somewhere off to the side, and then let the user select a DB row to update the `TComboBox`, and have updates to the `TComboBox` update the selected DB row. Or, I would find a 3rd party DBGrid that supports embedded controls (since putting a `TComboBox` over a native VCL grid is a bit of a hack to begin with - neither were designed for that purpose). – Remy Lebeau Jan 27 '22 at 23:29
  • @JerryDodge as for "nobody is using a PC without pointing device", you would be surprised how often I encounter PCs that only have a keyboard connected to them and require me to bring an additional USB keyboard with integrated touchpad to fix issues that require a mouse. (There is also the Move Mouse Keys feature in Windows, but that's rather awkward to use because the mouse pointer moves really slowly.). But on the other hand: When there is a scroll wheel, as mentioned in the question, there should also be a mouse. – dummzeuch Jan 28 '22 at 09:07