Forum > General

2 questions regarding String Grids

(1/2) > >>

Badger:
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").


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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.  

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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  endend

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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.           

cdbc:
Hi
This:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---(*procedure GridScrolling(Sender: TObject; Shift: TShiftState;  WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);begin  Sender as TStringgrid.Parent.Parent.ScrollBy(WheelDelta);end;    *)Can't be a simple procedure, it *has* to be a method on an object/class
- - - Stick it in a class or an object and Bob's your uncle - - -
Regards Benny

cdbc:
Hi ...again
Or you can play dirty like this:
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit datautils; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils,Forms, Controls, Printers,  ExtCtrls,   graphics, Grids;{ Hack start }type  { declare an object to host our method, yes it's a hack :o) }  THack = object    procedure GridScrolling(Sender: TObject;                            Shift: TShiftState;                            WheelDelta: Integer;                            MousePos: TPoint;                            var Handled: Boolean);  end; { THack }{ end of hack }   Procedure PutScrollboxOnForm(TheForm:TForm;PN:Integer);  Procedure PlacePanelInScrollBox(SB:TScrollBox;PN:Integer);  Procedure PutGridOnPanel(Pan:TPanel;PN:Integer);  implementation Uses  MainUnit; var { declare a variable of our object/hack }  Hack: THack; 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;    { here we utilize our hack/object to host the event-handler }    OnMouseWheel:=@Hack.GridScrolling;    refresh;  end;end; { Hack start }{ THack is a nifty way of converting a function or a procedure into a method :o) }procedure THack.GridScrolling(Sender: TObject;                              Shift: TShiftState;                              WheelDelta: Integer;                              MousePos: TPoint;                              var Handled: Boolean);begin  Sender as TStringgrid.Parent.Parent.ScrollBy(WheelDelta);end;{ end of hack }end. It's your unit, with a few modifications of mine, it should work now...
Just look for the word 'Hack' and you'll find my mods...  :D
It's a bit /Old School/, but hey... that's fun too  8-) ...and easy.
HTH
Regards Benny

TRon:

--- Quote from: cdbc on June 12, 2024, 09:52:10 am ---Just look for the word 'Hack' and you'll find my mods...  :D

--- End quote ---
I always name the object 'events' or 'eventhandlers' ... no idea it was considered a hack  :)


--- Quote ---It's a bit /Old School/, but hey... that's fun too  8-) ...and easy.

--- End quote ---
Exactly why I use that all the time: quick and easy.

You can always figure out a more proper solution/location (for the event handlers) when things are working (or just leave it as is).

jamie:
if I am following this correctly, shouldn't a simple


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Handled := True; 
In the MouseWhile Event for the stringgrid be enough to not send it to the scrollbox ?

Navigation

[0] Message Index

[#] Next page

Go to full version