Solved

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

Posted on 2009-05-12
8
2,100 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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