1. Home
  2. Computing & Technology
  3. Delphi Programming

How to correctly use the mouse wheel in TDBGrid

By Zarko Gajic, About.com

Mouse wheel behaves strangely with dbgrids - this code handler will correct this behavior.

Just drop a TApplicationEvents ("Additional" tab on the Component Palette) component on a form and handle it's OnMessage event as:

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure TForm1.ApplicationEvents1Message
   (var Msg: TMsg; var Handled: Boolean) ;
var
   i: SmallInt;
begin
   if Msg.message = WM_MOUSEWHEEL then
   begin
     Msg.message := WM_KEYDOWN;
     Msg.lParam := 0;
     i := HiWord(Msg.wParam) ;
     if i > 0 then
       Msg.wParam := VK_UP
     else
       Msg.wParam := VK_DOWN;

     Handled := False;
   end;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Note: This fixes the mouse wheel behavior not only for DBGrid-s but for all other list component (TListBox, TListView, etc).

Delphi tips navigator:
» Adding QuickReports to the Delphi 7 Component Palette
« Connect and disconnect to/from the Internet

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2002 Delphi Tips
  7. How to correctly use the mouse wheel in Delphi's TDBGrid

©2009 About.com, a part of The New York Times Company.

All rights reserved.