Recent

Author Topic: [SOLVED] How do I set the StringGrid column title at run time?  (Read 12089 times)

Hopestation

  • Full Member
  • ***
  • Posts: 181
[SOLVED] How do I set the StringGrid column title at run time?
« 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?
« Last Edit: February 13, 2018, 12:02:15 pm by Hopestation »

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: How do I set the StringGrid column title at run time?
« Reply #1 on: February 10, 2018, 10:33:20 pm »
Try
Code: Pascal  [Select][+][-]
  1. StrGrd.Columns.Items[1].Title.Caption := 'ABC';
  2.  
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Hopestation

  • Full Member
  • ***
  • Posts: 181
Re: How do I set the StringGrid column title at run time?
« Reply #2 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?

Hopestation

  • Full Member
  • ***
  • Posts: 181
Re: How do I set the StringGrid column title at run time?
« Reply #3 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?



bigeno

  • Sr. Member
  • ****
  • Posts: 266
Re: How do I set the StringGrid column title at run time?
« Reply #4 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]

bigeno

  • Sr. Member
  • ****
  • Posts: 266
Re: How do I set the StringGrid column title at run time?
« Reply #5 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;

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: How do I set the StringGrid column title at run time?
« Reply #6 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;
« Last Edit: February 11, 2018, 01:05:58 pm by wp »

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: How do I set the StringGrid column title at run time?
« Reply #7 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

wp

  • Hero Member
  • *****
  • Posts: 11855
Re: How do I set the StringGrid column title at run time?
« Reply #8 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.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: How do I set the StringGrid column title at run time?
« Reply #9 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
The only true wisdom is knowing you know nothing

Hopestation

  • Full Member
  • ***
  • Posts: 181
Re: How do I set the StringGrid column title at run time?
« Reply #10 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.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: How do I set the StringGrid column title at run time?
« Reply #11 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.
The only true wisdom is knowing you know nothing

Hopestation

  • Full Member
  • ***
  • Posts: 181
Re: How do I set the StringGrid column title at run time?
« Reply #12 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