Board index » delphi » TScrollbox Messaging Issue

TScrollbox Messaging Issue

I am using Delphi 5.0 and TScrollBox. I am trying to get two
scrollboxes to keep their HorzScrollBar position in sync so they
scroll together. I already created my own TScrollBox to trap the
WM_HSCROLL message. This works great when the user selects the scroll
bar, but it does not work when the user tabs through the controls in
the ScrollBox. The horizontal scrollbar moves but the Scroll event is
not fired.

I thought about overriding the SetPosition method of the
TControlScrollBar but it is a private method. It wouldn't have
mattered anyway because the Position property is not changed unless
the user moves the scroll box manually.

Any ideas?

Thanks in advance for your help.

Isaac Alexander

 

Re:TScrollbox Messaging Issue


Funny, you would think that the ScrollBars would have some sort of
OnScroll event

However this seems to do what you want :-

procedure TForm1.ScrollBox1CanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
begin
  ScrollBox2.VertScrollBar.Position :=
ScrollBox1.VertScrollBar.Position;
  ScrollBox2.HorzScrollBar.Position :=
ScrollBox1.HorzScrollBar.Position;
end;

A bit of a frig, but it works in my testbed

On 12 Mar 2003 15:46:24 -0800, is...@goprocura.com (Isaac Alexander)
wrote:

Quote
>I am using Delphi 5.0 and TScrollBox. I am trying to get two
>scrollboxes to keep their HorzScrollBar position in sync so they
>scroll together. I already created my own TScrollBox to trap the
>WM_HSCROLL message. This works great when the user selects the scroll
>bar, but it does not work when the user tabs through the controls in
>the ScrollBox. The horizontal scrollbar moves but the Scroll event is
>not fired.

>I thought about overriding the SetPosition method of the
>TControlScrollBar but it is a private method. It wouldn't have
>mattered anyway because the Position property is not changed unless
>the user moves the scroll box manually.

>Any ideas?

>Thanks in advance for your help.

>Isaac Alexander

Re:TScrollbox Messaging Issue


Quote
> However this seems to do what you want :-

> procedure TForm1.ScrollBox1CanResize(Sender: TObject; var NewWidth,
>   NewHeight: Integer; var Resize: Boolean);
> begin
>   ScrollBox2.VertScrollBar.Position :=
> ScrollBox1.VertScrollBar.Position;
>   ScrollBox2.HorzScrollBar.Position :=
> ScrollBox1.HorzScrollBar.Position;
> end;

But if you try to make this symmetric, by including similar code with
ScrollBox2, aren't you then ending up in a loop ?

Re:TScrollbox Messaging Issue


"Tom de Neef" <tden...@qolor.nl> wrote in message
news:3e70c433$0$49101$e4fe514c@news.xs4all.nl...

Quote
> > However this seems to do what you want :-

> > procedure TForm1.ScrollBox1CanResize(Sender: TObject; var NewWidth,
> >   NewHeight: Integer; var Resize: Boolean);
> > begin
> >   ScrollBox2.VertScrollBar.Position :=
> > ScrollBox1.VertScrollBar.Position;
> >   ScrollBox2.HorzScrollBar.Position :=
> > ScrollBox1.HorzScrollBar.Position;
> > end;

> But if you try to make this symmetric, by including similar code with
> ScrollBox2, aren't you then ending up in a loop ?

Probably, try:

procedure TForm1.ScrollBox1CanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
begin
if ScrollBox2.VertScrollBar.Position<>ScrollBox1.VertScrollBar.Position then
ScrollBox2.VertScrollBar.Position := ScrollBox1.VertScrollBar.Position;
if ScrollBox2.HorzScrollBar.Position<>ScrollBox1.horzScrollBar.Position then
ScrollBox2.HorzScrollBar.Position := ScrollBox1.horzScrollBar.Position;
end;

Cheers,
Nicholas Sherlock

Re:TScrollbox Messaging Issue


Thanks. That worked perfectly.

Re:TScrollbox Messaging Issue


Bottom posted

On Thu, 13 Mar 2003 18:44:47 +0100, "Tom de Neef" <tden...@qolor.nl>
wrote:

Quote
>> However this seems to do what you want :-

>> procedure TForm1.ScrollBox1CanResize(Sender: TObject; var NewWidth,
>>   NewHeight: Integer; var Resize: Boolean);
>> begin
>>   ScrollBox2.VertScrollBar.Position :=
>> ScrollBox1.VertScrollBar.Position;
>>   ScrollBox2.HorzScrollBar.Position :=
>> ScrollBox1.HorzScrollBar.Position;
>> end;

>But if you try to make this symmetric, by including similar code with
>ScrollBox2, aren't you then ending up in a loop ?

True - but that is easy to overcome

Possible a 'saniity check'

More safely a variable within TForm ... FInScrollJiggle : Boolean

Of course the event should be posted by both controls to the same
routine ...

Since the OP is well capable of modifying controls, a TDualScrollBox
'creation' looks sensible.

Re:TScrollbox Messaging Issue


On Thu, 13 Mar 2003 18:44:47 +0100, "Tom de Neef" <tden...@qolor.nl>
wrote:

Quote
>> However this seems to do what you want :-

>> procedure TForm1.ScrollBox1CanResize(Sender: TObject; var NewWidth,
>>   NewHeight: Integer; var Resize: Boolean);
>> begin
>>   ScrollBox2.VertScrollBar.Position :=
>> ScrollBox1.VertScrollBar.Position;
>>   ScrollBox2.HorzScrollBar.Position :=
>> ScrollBox1.HorzScrollBar.Position;
>> end;

>But if you try to make this symmetric, by including similar code with
>ScrollBox2, aren't you then ending up in a loop ?

Something like (i'm not sure of some names because I have only D3, but the
principle is to set both scrollboxes to the same event handler, and set the
responder's event handler to nil while you move the potential initiator of a
circular loop...

procedure TForm1.ScrollBox1CanResize(Sender: TObject; var NewWidth,
    NewHeight: Integer; var Resize: Boolean);
var
  Leader, Follower : TScrollBox;
  OldCanResize : TNotifyScrollBar // ??? I have only D3
begin
  Leader := TScrollBox(Sender)
  if Leader = ScrollBox1 then
    Follower := ScrollBox2
  else
    Follower := ScrollBox1;
  OldCanResize := Follower.CanResize;  // ??? I have only D3
  Follower.CanResize := nil;
  Follower.VertScrollBar.Position := Leader.VertScrollBox.Position;
  Follower.HorzScrollBar.Position := Leader.HorzScrollBar.Position;
  Follower.CanResize := OldCanResize;  // ??? Ihave only D3
end;

Alan Lloyd
alangll...@aol.com

Other Threads