szafran81
asked on
Setting StringGrids Scrollbar to be AlwaysVisible
Hi
I have a TStringGrid on a Form, and that StringGrid has one scrollbar, and i need for that scrollbar to be always visible (even if theres is nothing to display in the string grid except the title row). how can I do it?
I have a TStringGrid on a Form, and that StringGrid has one scrollbar, and i need for that scrollbar to be always visible (even if theres is nothing to display in the string grid except the title row). how can I do it?
set the Scrollbars property to ssBoth
ASKER
Nope. only one is needed, and only one can be displayed. No need for 2 scrollbars.
ASKER
...and setting it ssBoth didn't help either - the scroll bar isn't visible until there are more rows to display than visible.
I see what you mean. I tested some codes from the net, with createparams and ShowScrollBar function but to no avail.
I mus step out now but I'll try some more when I return.
I mus step out now but I'll try some more when I return.
ASKER
also it would be nice to make it visible, but disabled (grayed out) until there will more rows than visible.
I can't believe myself how stupid mistake I did :D. it actually works the way I tried I just had to set scrollbars to ssnone.
here is unit code and dfm for your reference
here is unit code and dfm for your reference
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, ComCtrls, XPMan;
type
TStringGrid=class(Grids.TStringGrid)
protected
procedure CreateParams(var Params: TCreateParams); override;
end;
TForm1 = class(TForm)
StringGrid1: TStringGrid;
XPManifest1: TXPManifest;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
EnableScrollBar(stringgrid1.handle, SB_BOTH, ESB_DISABLE_BOTH);
end;
{ TStringGrid }
procedure TStringGrid.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.Style := Params.Style OR WS_HSCROLL OR WS_VSCROLL;
end;
end.
DFM follows
object Form1: TForm1
Left = 192
Top = 114
Width = 870
Height = 640
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object StringGrid1: TStringGrid
Left = 168
Top = 96
Width = 473
Height = 217
ScrollBars = ssNone
TabOrder = 0
end
object XPManifest1: TXPManifest
Left = 608
Top = 32
end
end
oh, I forgot. you only need one of the scxrololbars. well, remove the otehr one from create params and enablescrollbar ;)
ASKER
thanks for the reply. now i need to go to get a drink to blow off some steam after a bad day. i'll try the code when i come back or in the morning.
ok
ASKER
I've got the time to test before I go. It shows the scrollbar but it stays diabled even if no of rows > visible rows.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I've just came back from the bar. But I'm unable to check the code today (now it's midnight here). Dorry for the inconvence, i thouth that it'll be a simple code for that. I'll check the code in the morning. (maybe just enabling the ssVertical srollbar after the rowcount > visible rows wiil do the trick ?)
ASKER
i've worked it out.
just put EnableScrollBar(handle, SB_VERT, ESB_ENABLE_BOTH) if the row count is > than visible row count (real visible row count - eg. 15 - and not the VisibleRowCount property), and EnableScrollBar(handle, SB_VERT, ESB_DISABLE_BOTH) when it's less.
thanks.
just put EnableScrollBar(handle, SB_VERT, ESB_ENABLE_BOTH) if the row count is > than visible row count (real visible row count - eg. 15 - and not the VisibleRowCount property), and EnableScrollBar(handle, SB_VERT, ESB_DISABLE_BOTH) when it's less.
thanks.
ASKER
hmmm... i thought i worked it out... everything is fine... except that the thumb tracker (at least i think that's what it's called - the scrolling button inside the scrollbar, between the up and down buttons) isn't moving... hmmm... that's weird... anyways... time to go to sleep... will try something else in the morning.
>> the scrolling button inside the scrollbar, between the up and down buttons) isn't moving.
that's what I meant by "note that the scrollbar itself is not scrolling." :)
I'm working on it.
that's what I meant by "note that the scrollbar itself is not scrolling." :)
I'm working on it.
ASKER
sorry ;)
i was after a few b**rs, and apparently i've missed that ;)
i was after a few b**rs, and apparently i've missed that ;)
ASKER
it works =o]
i've added this on the of the proc that refreshes the contents of the grid:
if (grid.RowCount > 12) then //12 is my visible row count
begin
EnableScrollBar(grid.Handl e, SB_VERT, ESB_ENABLE_BOTH);
grid.ScrollBars := ssVertical;
end
else
begin
grid.ScrollBars := ssNone;
EnableScrollBar(grid.Handl e, SB_VERT, ESB_DISABLE_BOTH);
end;
Thank you for your help.
i've added this on the of the proc that refreshes the contents of the grid:
if (grid.RowCount > 12) then //12 is my visible row count
begin
EnableScrollBar(grid.Handl
grid.ScrollBars := ssVertical;
end
else
begin
grid.ScrollBars := ssNone;
EnableScrollBar(grid.Handl
end;
Thank you for your help.
find attached updated and working unit
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, StdCtrls, ComCtrls, XPMan;
type
TStringGrid=class(Grids.TStringGrid)
protected
procedure CreateParams(var Params: TCreateParams); override;
procedure TopLeftChanged; override;
public
procedure updatescrollbar;
end;
TForm1 = class(TForm)
StringGrid1: TStringGrid;
XPManifest1: TXPManifest;
Button1: TButton;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses math;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
EnableScrollBar(stringgrid1.handle, SB_VERT, ESB_DISABLE_BOTH);
end;
{ TStringGrid }
procedure TStringGrid.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.Style := Params.Style OR WS_VSCROLL;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
stringgrid1.rowcount:=stringgrid1.rowcount+1;
stringgrid1.updatescrollbar;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
stringgrid1.rowcount:=stringgrid1.rowcount-1;
stringgrid1.updatescrollbar;
end;
procedure TStringGrid.TopLeftChanged;
var s:TScrollInfo;
begin
inherited;
s.cbSize:=sizeof(s);
s.fMask:=SIF_POS;
s.nPos:=TopRow;
setscrollinfo(handle, SB_VERT, s, true);
end;
procedure TStringGrid.updatescrollbar;
var i,h:integer;
s:TScrollInfo;
begin // TODO: change this ss it onlly gets called when really needed. no sense in enableing an enabled scroll bar or disableing a disabled scrollbar.
i:=BorderWidth;
h:=0;
while i<TopRow+VisibleRowCount do
begin
h:=h+RowHeights[i]+GridLineWidth;
inc(i);
end;
if i<rowcount then
h:=h+RowHeights[i];
if ClientHeight<=h then
begin
EnableScrollBar(handle, SB_VERT, ESB_ENABLE_BOTH);
s.cbSize:=sizeof(s);
s.fMask:=SIF_POS or SIF_RANGE or SIF_PAGE;
s.nMin:=FixedRows;
s.nMax:=FixedRows+RowCount-TopRow-VisibleRowCount;
s.nPos:=TopRow;
s.nPage:=1;
setscrollinfo(handle, SB_VERT, s, true);
if getlasterror<>0 then
showmessage(syserrormessage(getlasterror));
end
else
EnableScrollBar(handle, SB_VERT, ESB_DISABLE_BOTH);
{ SetScrollRange(handle, SB_VERT, VisibleRowCount, RowCount, true);
SetScrollPos(handle, SB_VERT, TopRow, true);}
end;
end.
>> i've added this on the of the proc that refreshes the contents of the grid:
my solution works for any stringgrid (and can be adapted to any customgrid descendant), with custom row heights and custom size. the number of actually visible rows change if the grid is resized vertically and/or one or more of the rows heights are changed ;)
my solution works for any stringgrid (and can be adapted to any customgrid descendant), with custom row heights and custom size. the number of actually visible rows change if the grid is resized vertically and/or one or more of the rows heights are changed ;)