Recent

Author Topic: How to keep a StringGrid Column invisible?  (Read 7358 times)

fred

  • Full Member
  • ***
  • Posts: 201
How to keep a StringGrid Column invisible?
« on: November 07, 2017, 12:10:35 pm »
When i want to make a StringGrid column invisible i used "ColWidth[n] := 0;".
I always thought that a column was invisible and could not be resized using a mouse with "goColSizing := True;" but it can be resized/made visible using a mouse.
How do i make a column invisible even with goColSizing?
Or is the only way using the StringGrid in Columns mode?

Tested with a new Application, put a StringGrid on the form.
In FormCreate:
  StringGrid1.ColWidths[4] := 0;
  StringGrid1.Options := StringGrid1.Options + [goColSizing];

After running there are 4 columns visible, drag the fifth column open with the mouse.
So i was wrong in my assumption... how can i keep the column invisible?

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How to keep a StringGrid Column invisible?
« Reply #1 on: November 07, 2017, 12:31:47 pm »
If you work with Columns, you'll see a property "Visible" for each column in the Object Inspector.

mig-31

  • Sr. Member
  • ****
  • Posts: 305
Re: How to keep a StringGrid Column invisible?
« Reply #2 on: November 07, 2017, 12:55:24 pm »
or with code
Code: Pascal  [Select][+][-]
  1. StringGrid.Collumns.Items.Item[0].Visible
  2.  
Lazarus 2.2.6 - OpenSuse Leap 15.4, Mageia 8, CentOS 7

fred

  • Full Member
  • ***
  • Posts: 201
Re: How to keep a StringGrid Column invisible?
« Reply #3 on: November 07, 2017, 01:04:36 pm »
Thanks, i know, "Or is the only way using the StringGrid in Columns mode?".
So it is not possible in the default StringGrid mode?
In the past i used "Colwidth[n] := -1; in Delphi 5 ( := 0; in Lazarus ) to disable a column.
It's not too dificult to change the StringGrid to Column mode but i was hoping for less work.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How to keep a StringGrid Column invisible?
« Reply #4 on: November 07, 2017, 01:28:11 pm »
Thanks, i know, "Or is the only way using the StringGrid in Columns mode?".
So it is not possible in the default StringGrid mode?
Without having looked at the source code, I don't think so because there must be something to store the information that the column is hidden. I am rather sure that the designers of TStringGrid did not abuse Width=0 as a way to pseudo-hide a column.

fred

  • Full Member
  • ***
  • Posts: 201
Re: How to keep a StringGrid Column invisible?
« Reply #5 on: November 07, 2017, 03:41:13 pm »
Since the old Delphi style is not working with goColSizing it seems i have at least three options, custom columns, KGrid, TECGrid.
With custom columns i think a fixed first column Columns[0] has Cells[1, aRow] :)
I will see what is easier to convert/use, thanks :)

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: How to keep a StringGrid Column invisible?
« Reply #6 on: November 07, 2017, 04:22:10 pm »
I think you can keep everything as before - look at attached demo: it shows a stringgrid with columns and I added some data to the cells. I can address each cell correctly as if there were no columns.

The only thing which is not possible is to extend to number of columns by changing ColCount. You must add a column instead (StringGrid.Columns.Add)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to keep a StringGrid Column invisible?
« Reply #7 on: November 07, 2017, 04:38:18 pm »
Thanks, i know, "Or is the only way using the StringGrid in Columns mode?".
So it is not possible in the default StringGrid mode?
You can use a horribly inefficient hack, if you must avoid Columns mode (which as wp says, is definitely the way to go).
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
  2. var
  3.   sg: TStringGrid absolute Sender;
  4.   col, row: Integer;
  5. begin
  6.   if Sender is TStringGrid then begin
  7.     sg.MouseToCell(X, Y, col, row);
  8.     if col = 4 then
  9.       sg.Options:=sg.Options - [goColSizing]
  10.     else sg.Options:=sg.Options + [goColSizing];
  11.   end;
  12. end;

tk

  • Sr. Member
  • ****
  • Posts: 361
Re: How to keep a StringGrid Column invisible?
« Reply #8 on: November 07, 2017, 09:08:03 pm »
With KGrid you can use OnBeginColSizing event and for hidden columns where you assign CanBeginSizing := False they remain invisible.
Example is shown in the KGrid demo.
« Last Edit: November 07, 2017, 09:13:07 pm by tk »

fred

  • Full Member
  • ***
  • Posts: 201
Re: How to keep a StringGrid Column invisible?
« Reply #9 on: November 08, 2017, 05:42:58 pm »
@wp: Yes, i have used custom columns before but sometimes i stll have an old program to convert.
But it is not difficult to change that the program(s) are small, it only takes some time :)
For the current program i have set goColSizing to False, the window/data is smaller than the screen width.

@howardpc: Nice trick but like you say it will get called on every mouse move.
Another bad trick is to set the ColWidth to zero in the HeaderSizing/HeaderSized or OnDrawCell events.
Only when i don't know another way i would use this kind of things, hopefully never :)

@tk: I know of KGrid for years but i have never used it, might be a good time to start.
Hmm, when compiling kcontrolslaz.lpk dated 2017-09-24 i got: kgrids.pas(2945,15) Error: There is no method in an ancestor class to be overridden: "DoAutoAdjustLayout(const TLayoutAdjustmentPolicy;const Double;const Double;const Boolean);"
I suppose i have something wrong, will look into it later, should be in another thread...

Thanks all, i can make it work, wish only more time for programming :)

 

TinyPortal © 2005-2018