I have two questions marginally related.
I have a String Grid on a panel in a scroll box. This is initial the configuration of the attached program.
If you run the program you can scroll up and down with the mouse wheel with the cursor over the scroll box. You can also scroll with the cursor over the panel. Normally, if you scroll up and down over the StringGrid it only scrolls the cells. I have added an OnMouseWheel event to override this. Unfortunately, the scrollbox keeps on scrolling past its limits. Can anyone give me a fix for this or suggest an alternative way of doing it.
My second question, assuming I can get the scroll box responding properly, is that I I am using the dataUtils unit to create a number of components and do common tasks. (Click the button to activate this) To do complete this I need to add the OnMouseWheel event at run time. I thought I knew how to do this but it refuses to compile. (Reverse all the commented out code in "DataUtils").
unit MainUnit;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, DataUtils,
Grids, Buttons, StdCtrls, Types;
type
{ TMainForm }
TMainForm = class(TForm)
Button1: TButton;
Panel1: TPanel;
ScrollBox1: TScrollBox;
StringGrid1: TStringGrid;
procedure Button1Click(Sender: TObject);
procedure StringGrid1MouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
private
public
end;
var
MainForm: TMainForm;
StGrids:Array of TStringGrid;
Panels:Array of TPanel;
Scrolls:Array of TScrollbox;
PageNum:Integer;
implementation
{$R *.lfm}
{ TMainForm }
procedure TMainForm.StringGrid1MouseWheel(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
Scrollbox1.ScrollBy(0,Wheeldelta div 10);
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
PutScrollboxOnForm(MainForm,PageNum);
PlacePanelInScrollBox(Scrolls[PageNum],PageNum);
PutGridOnPanel(Panels[PageNum],PageNum);
end;
end.
object MainForm: TMainForm
Left = 346
Height = 496
Top = 119
Width = 714
Caption = 'MainForm'
ClientHeight = 496
ClientWidth = 714
LCLVersion = '2.2.4.0'
object ScrollBox1: TScrollBox
Left = 72
Height = 296
Top = 80
Width = 408
HorzScrollBar.Page = 376
VertScrollBar.Page = 292
ClientHeight = 292
ClientWidth = 387
TabOrder = 0
object Panel1: TPanel
Left = 11
Height = 304
Top = 16
Width = 365
Caption = 'Panel1'
ClientHeight = 304
ClientWidth = 365
TabOrder = 0
object StringGrid1: TStringGrid
Left = 24
Height = 272
Top = 16
Width = 312
TabOrder = 0
OnMouseWheel = StringGrid1MouseWheel
end
end
end
object Button1: TButton
Left = 160
Height = 25
Top = 424
Width = 184
Caption = 'Click to add programatically'
OnClick = Button1Click
TabOrder = 1
end
end
unit datautils;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils,Forms, Controls, Printers,
ExtCtrls,
graphics, Grids;
Procedure PutScrollboxOnForm(TheForm:TForm;PN:Integer);
Procedure PlacePanelInScrollBox(SB:TScrollBox;PN:Integer);
Procedure PutGridOnPanel(Pan:TPanel;PN:Integer);
// Procedure GridScrolling(Sender: TObject; Shift: TShiftState;
// WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
implementation
Uses
MainUnit;
procedure PutScrollboxOnForm(TheForm: TForm;PN:Integer);
begin
SetLength(Scrolls,PN+1);
Scrolls[PN]:=TScrollBox.Create(TheForm);
with Scrolls[PN] do
begin
Parent:=TheForm;
width:=500;
Height:=400;
Left:=8;
Top:=8;
visible:=True;
refresh;
end;
end;
procedure PlacePanelInScrollBox(SB:TScrollBox;PN:Integer);
begin
SetLength(Panels,PN+1);
Panels[PN]:=TPanel.Create(MainUnit.Scrolls[PN]);
with Panels[PN] do
begin
Parent:=SB;
width:=300;
Height:=500;
Left:=8;
Top:=8;
color:=clwhite;
visible:=True;
refresh;
end;
end;
procedure PutGridOnPanel(Pan: TPanel; PN: Integer);
begin
SetLength(StGrids,PN+1);
StGrids[PN]:=TStringGrid.Create(MainUnit.Panels[PN]);
with StGrids[PN] do
begin
Parent:=Pan;
width:=250;
Height:=400;
Left:=8;
Top:=8;
color:=clwhite;
visible:=True;
//OnMouseWheel:=@GridScrolling;
refresh;
end;
end;
(*
procedure GridScrolling(Sender: TObject; Shift: TShiftState;
WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
begin
Sender as TStringgrid.Parent.Parent.ScrollBy(WheelDelta);
end;
*)
end.