Solved

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

Posted on 2009-05-12
8
2,134 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
[X]
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
  • 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month6 days, 11 hours left to enroll

623 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