Lazarus

Programming => LCL => Topic started by: Hopestation on February 10, 2018, 10:22:03 pm

Title: [SOLVED] How do I set the StringGrid column title at run time?
Post by: Hopestation on February 10, 2018, 10:22:03 pm
Hi.

I have tried using StringGrid.Cells[1,0] := 'ABC';

This was accepted during compilation but ignored at run time.

I changed it to StringGrid.Cells[1,1] := 'ABC'; and it changed the text in the first row

Then I found TGridColumnTitle.

I tried StrGrd.Columns.Items[1].Title := 'ABC';

This was rejected because 'ABC' isn't a TGridColumnTitle.

I tried declaring

VAR
  aTitle: TGridColumnTitle;

Then I set aTitle := 'ABC'

I got

 Error: Incompatible types: got "Constant String" expected "TGridColumnTitle"

How do I set a Column title?
Title: Re: How do I set the StringGrid column title at run time?
Post by: Blaazen on February 10, 2018, 10:33:20 pm
Try
Code: Pascal  [Select][+][-]
  1. StrGrd.Columns.Items[1].Title.Caption := 'ABC';
  2.  
Title: Re: How do I set the StringGrid column title at run time?
Post by: Hopestation on February 11, 2018, 10:58:27 am
Thanks for your help,

I am still mystified as to why setting the cell values didn't work.

Trying to read from row 0 doesn't work either.

I tried it after reading Taazz's reply to a similar question in 2013:

https://forum.lazarus.freepascal.org/index.php?topic=21164.0

You also answered that query and didn't disagree with him.

Has the StringGrid changed since then?
Title: Re: How do I set the StringGrid column title at run time?
Post by: Hopestation on February 11, 2018, 12:37:17 pm
After writing to the column title caption I went on to try to set its font size.

Following the above format I went through the procedure of adding a property followed by a dot and waited for the box of options to appear.

I got StrGrd.Columns.Items[N].Title.Font.Size:= 9;

This doesn't work, so I tried

  StrGrd.Columns.Items[N].Title.Font.Caption.

and got a long list of options, but size wasn't included.

What is going wrong?


Title: Re: How do I set the StringGrid column title at run time?
Post by: bigeno on February 11, 2018, 12:41:50 pm
AFAIK if you use custom columns then column title will override the values specified in cells[x,0]
Title: Re: How do I set the StringGrid column title at run time?
Post by: bigeno on February 11, 2018, 12:45:35 pm
And I always use StrGrd.Columns[N].Title.Font.Size:= 9; instead of StrGrd.Columns.Items[N].Title.Font.Size:= 9;
Title: Re: How do I set the StringGrid column title at run time?
Post by: wp on February 11, 2018, 01:03:56 pm
If you want to do something at runtime but don't know how things are named you're out of luck. Before you begin at runtime do the same at designtime, then you see all the names and their relation in the ObjectInspector:

Add a stringgrid to the form.
Add some columns (Click at '...' next to "Columns", click "+ Add")
Select a column in the component tree above the property list in the object inspector.
In the property list find the node "Title", open it. It has a "Font", open it. Find the "Size" and change the value.

In total you have this:
  - StringGrid1
  - Columns[index]  ("index" is the index of the column which you clicked, it is listed in the componenttree)
  - Title
  - Font
  - Size

Write all these names separated by a '.' - and you got it!

Code: Pascal  [Select][+][-]
  1.  StringGrid1.Columns[index].Title.Font.Size := something;
Title: Re: How do I set the StringGrid column title at run time?
Post by: Bart on February 11, 2018, 02:08:19 pm
If you want to do something at runtime but don't know how things are named you're out of luck.

Not quite, if you can guess a little bit what you're after then  use the Lazarus CodeTools to help you: type StringGrid1. waite a while, select Columns from dropdownlist, type [index] and another period (.) wait for next dropdownlist, select Title, type another period etc.

Bart
Title: Re: How do I set the StringGrid column title at run time?
Post by: wp on February 11, 2018, 02:23:03 pm
If you want to do something at runtime but don't know how things are named you're out of luck.

Not quite, if you can guess a little bit what you're after then  use the Lazarus CodeTools to help you: type StringGrid1. waite a while, select Columns from dropdownlist, type [index] and another period (.) wait for next dropdownlist, select Title, type another period etc.

Bart

Yes. That's another way. Or use the source code: CTRL-Click on TStringGrid in the interface of the mainform, and the editor will open the file in which TStringGrid is implemented, and you see all properties and methods of it. You may want to CTRL-Click again on properties or their type to get to their implementation/declaration.
Title: Re: How do I set the StringGrid column title at run time?
Post by: jamie on February 11, 2018, 03:10:19 pm
Ha, that's a neat trick in deed! Thanks... that saves a lot of time..

One of these days I need to take a shot at putting in a PRINT function for the Short Cut keys so I can
have a quick reference to them..
Thanks
Title: Re: How do I set the StringGrid column title at run time?
Post by: Hopestation on February 11, 2018, 06:29:26 pm
Thanks for all your answers. They've helped a lot.

Is there an answer why StringGrid.Cells[1,0] := 'ABC'; doesn't work?

According to Taazz it should.
Title: Re: How do I set the StringGrid column title at run time?
Post by: jamie on February 11, 2018, 06:42:39 pm
if you have implemented COLUMNS.Count > 0 then the header gets overridden.

More than likely when you make an attempt to change that, it just gets over written
on the next repaint.

 have you read back what you put there ? If it comes back as to what you put there and you don't
see it appearing on the cell, that verifies the case, cell is getting over painted via the COLUMNS.TGridColmn..

 You can change the contents of the columns[1].Title.Caption :'ABC'; I think that will work or its close to it.
Title: Re: How do I set the StringGrid column title at run time?
Post by: Hopestation on February 13, 2018, 12:01:44 pm
I set the number of columns at design time. It doesn't change at run time.
Thanks for your help.
TinyPortal © 2005-2018