Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

TScrollbox: How can I prevent automatic "scrollintoview" of large components?

Posted on 2009-05-12
8
2,065 Views
Last Modified: 2012-05-06
Hi, a very annoying problem using BDS2006:

I have a Form with a Scrollbox containing two TStringGrids aligned vertically.
The StringGrids are sized so that the bottom one is only partly displayed on the Form, the ScrollBox allows accessing the whole StringGrid.

The problem: if I click the bottom StringGrid (which is partly visible), the Scrollbox "jumps" so that the clicked StringGrid scrolls into view completely. I want to be able to click the StringGrid without any "jump effect" on the scrollbox!
How can I do this?
unit1.pas
==============================
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids;
 
type
  TForm1 = class(TForm)
    ScrollBox1: TScrollBox;
    StringGrid1: TStringGrid;
    StringGrid2: TStringGrid;
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
end.
==============================
 
unit1.dfm
==============================
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 533
  ClientWidth = 742
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object ScrollBox1: TScrollBox
    Left = 0
    Top = 0
    Width = 742
    Height = 533
    Align = alClient
    TabOrder = 0
    ExplicitWidth = 688
    object StringGrid1: TStringGrid
      Left = 5
      Top = 5
      Width = 668
      Height = 300
      RowCount = 20
      TabOrder = 0
    end
    object StringGrid2: TStringGrid
      Left = 5
      Top = 311
      Width = 668
      Height = 400
      RowCount = 20
      TabOrder = 1
    end
  end
end
==============================

Open in new window

0
Comment
Question by:waeberd
  • 3
  • 3
  • 2
8 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 250 total points
ID: 24365240
You can create a component derived by TScrollBox, and then override the TScrollingWinControl.AutoScrollInView.

Eg:

--------------------------------
unit ScrollBoxEx;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TScrollBoxEx      =  class(TScrollBox)
  private
     // Private declarations
  protected
     // Protected declarations
     procedure      AutoScrollInView(AControl: TControl); override;
  public
     // Public declarations
  published
     // Published declarations
     property       Align;
     property       Anchors;
     property       AutoScroll;
     property       AutoSize;
     property       BiDiMode;
     property       BorderStyle;
     property       Constraints;
     property       DockSite;
     property       DragCursor;
     property       DragKind;
     property       DragMode;
     property       Enabled;
     property       Color nodefault;
     property       Ctl3D;
     property       Font;
     property       ParentBiDiMode;
     property       ParentColor;
     property       ParentCtl3D;
     property       ParentFont;
     property       ParentShowHint;
     property       PopupMenu;
     property       ShowHint;
     property       TabOrder;
     property       TabStop;
     property       Visible;
     property       OnCanResize;
     property       OnClick;
     property       OnConstrainedResize;
     property       OnContextPopup;
     property       OnDblClick;
     property       OnDockDrop;
     property       OnDockOver;
     property       OnDragDrop;
     property       OnDragOver;
     property       OnEndDock;
     property       OnEndDrag;
     property       OnEnter;
     property       OnExit;
     property       OnGetSiteInfo;
     property       OnMouseDown;
     property       OnMouseMove;
     property       OnMouseUp;
     property       OnMouseWheel;
     property       OnMouseWheelDown;
     property       OnMouseWheelUp;
     property       OnResize;
     property       OnStartDock;
     property       OnStartDrag;
     property       OnUnDock;
  end;

procedure Register;

implementation

procedure TScrollBoxEx.AutoScrollInView(AControl: TControl);
begin
  // No-op
end;

procedure Register;
begin
  RegisterComponents('Win32', [TScrollBoxEx]);
end;

end.

-------------------

- Save component source
- Install the component
- Change the dfm (view as text) to read:

 object ScrollBox1: TScrollBoxEx

- Change the form declaration to read

type
  TForm1 = class(TForm)
    ScrollBox1: TScrollBoxEx;

----------

Regards,
Russell




 
0
 
LVL 12

Assisted Solution

by:Hypo
Hypo earned 250 total points
ID: 24365439
The auto scrolling is generated by the function AutoScrollInView, which is called from the SetFocusedControl function (which is called when you change focus, like clicking in the other grid). You can disable this by deriving the TScrollBox class locally in your unit, with the same name, and then override the AutoScrollInView with an empty function. I don't know if it is the best way to do it in this case, but it works.
Setting the TScrollBox.AutoScroll property to false prevents auto scrolling, but it also removes the scroll bars, and I don't think that's what you are looking for.

regards
Hypo
Add this type to above your form declaration in your unit... 
type
  TScrollBox = class(Forms.TScrollBox)
   protected
    procedure AutoScrollInView(AControl: TControl); Override;
  end;
 
  TForm1 = class(TForm)... 
 
...
// Then add code this to your implementation part
procedure TScrollBox.AutoScrollInView(AControl: TControl);
begin
  // Do nothing 
end;
  

Open in new window

0
 
LVL 12

Expert Comment

by:Hypo
ID: 24365445
Damn, I was slow... :)
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 26

Expert Comment

by:Russell Libby
ID: 24365529
Hypo,

Your method is actually simpler to implement (both do the same thing by stubbing the method).

Russell
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24365626
Yeah,
It's a quick n' dirty sollution I saw somewhere on this forum a couple of months ago, it was another control, but the principals was the same; and It's a nice way to tweak your standard VCL's when you need to change their behaviour to test something quickly. :)

regards
Hypo
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 24365657
I have used it before, but my delphi is starting to get rusty from too much C++ work ;-)

Russell
0
 

Author Closing Comment

by:waeberd
ID: 31580533
wow, thanks a million!
0
 

Author Comment

by:waeberd
ID: 24365943
Thanks a lot! Fast, helpful answers!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error E2158 compiling with Delphi XE10 Seattle 2 112
SUM 2 INTEGER ARRAYS INTO 1 10 108
JAudiorecorder record freezing the app 29 74
how to resize animated Gif image in delphi ? 1 34
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question