How do you change the Font Color for a TStringGrid Cell? I tried the following, but it doesn't work:
SGrid.Canvas.Font.Color := clGreen;
SGrid.Cells[i,j] := 'My String';
Mailing lists / lazarus / 2012-11
12 messages · 7 people ·
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
How do you change the Font Color for a TStringGrid Cell? I tried the following, but it doesn't work:
SGrid.Canvas.Font.Color := clGreen;
SGrid.Cells[i,j] := 'My String';Terry A. Haimann wroteHow do you change the Font Color for a TStringGrid Cell?
Use the OnPrepareCanvas event.
Juha
Juha, why not use onDrawCell ? Is there any performance issue?
2012/11/17 Juha Manninen
I am wanting to change the color based on a value. The cell already exists and I am not sure how I am going to fire either OnDrawCell or OnPrepareCanvas. But I am probably totally misunderstanding it.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20121117/88a56c68/attachment.html>
How do you change the Font Color for a TStringGrid Cell? I tried the following, but it…
In the IDE there is a form for inserting characters (Menu->Edit->Insert from Character map).
Take a look at the code of that form to see how it is done.
Bart
You know that's not a lot of help. I don't know what the program name is and I don't know where the source is. If you know how to do this, why can't you provide a snippet of code with maybe a few comments? If you want Lazarus to be a popular RAD then don't obfuscate, make thing ease for the user instead.
Terry
The program is Lazarus!!!! IDE means integrated development environment.
While I appreciate your feelings that code snippets are easier for you, and I agree that finding where that code is exactly may be a bit daunting, do you think people answering these questions always like/have time for doing all the lookup work? Perhaps he doesn't know by heart what to do exactly but remembers the code was there.
If you get the answer you are looking for, please update the wiki [1], which was built by thousands of hours of volunteer work by me and many others, to help document Lazarus better.
Thanks.
[1] http://wiki.lazarus.freepascal.org/Grids_Reference_Page
By the way, that wiki article says:
Some properties can affect the way the grid looks by acting when the cell is about to be painted in PrepareCanvas/OnPrepareCanvas by changing default canvas properties like brush color or font. Following is a list of such properties:
...
Color. This sets the primary color used to draw non fixed cells background
Now when does the OnPrepareCanvas event happen?
In
Description of grid's drawing process
...
OnPrepareCanvas. If the programmer wrote an event handler for
OnPrepareCanvas event, it is called at this point.
This event can be used for doing simple customization like changing
cell's background color,
Which seems to confirm Juha's suggestion in his post: use OnPrepareCanvas, which will be fired by the drawing cycle of the grid, no need for you to specifically do something.
(From memory, may not be accurate)
In the Object Inspector, select the grid, then go to OnPrepareCanvas, then click on the ellipses (...) which will give you the OnPrepareCanvas event handler.
There modify the .Color property of the cell in question and see if tt works.
You know that's not a lot of help. I don't know what the program name is and I don't…
I remember back when I used Delphi would ask my mentor how to do this or that, and without fail he would reply "Did you look in the VCL". By looking in the VCL I discovered many things on my path to whatever this or that was. So, by suggesting the working example the poster is helping you by teaching you to fish.
Here's what I did to follow the suggestion.
In a console window I changed directory to where lazarus exists. I then searched for filenames containing ide.
I changed to the directory that had the most.
I then grepped the source files for map and piped that to another grep for character. The reult was one pas file and one lfm file. Since the code would not be in the form file I looked in the source file.
charactermapdlg.pas
It took me longer to write this than to find it.
I am wanting to change the color based on a value. The cell already exists and I am not…
2012/11/17 Terry A. Haimann :
I am wanting to change the color based on a value. The cell already exists
TSemaphorGrid enlight a cell based on the value (red yellow and green labels for StringRed, StringYellow and StringGreen values). If you need more values you can easily extend this component. You can find it in /examples/gridexamples/grid_semaphor and then share the source with comments to make Lazarus IDE a little bit more easy and popular
Regards,
Salvatore
Juha, why not use onDrawCell ? Is there any performance issue? 2012/11/17 Juha Manninen…
William Oliveira Ferreira wroteJuha, why not use onDrawCell ? Is there any performance issue?
No performance issue but you will potentially duplicate the grid's drawing code when doing custom painting with onDrawCell. OnPrepareCanvas works with default drawing.
It is explained well in the Grids_Reference_Page in Wiki, search for OnPrepareCanvas.
About finding code examples, I can again recommend the "Find in Files" dialog (Ctrl-Shift-F) in Lazarus. Search your key word from Lazarus source dir, including subdirs, and you get results almost for sure. There are ~ 1.5 million LOC. FPC sources have still much more code. Lazarus keeps many search result tabs open, it is one of the best search GUIs in any IDE.
Let's see: Searching for PrepareCanvas gives 39 matches. They are in Lazarus IDE sources and in 2 example projects.
One more advertisement: On Linux the search in those 1.5 million source lines takes < 2 seconds when the files are cached by OS (first search is slow). On Windows it is always much slower, maybe around 15 seconds.
For me it makes a difference because I do such searches often.
Juha
You know that's not a lot of help. I don't know what the program name is and I don't…
Terry A. Haimann wroteYou know that's not a lot of help.
Thank you very much ;-)
If you know how to do this, why can't you provide a snippet of code with maybe a few comments?
This is from one of my programs.
It changes color of background of a cell when the mouse hoovers over it. The relevat code is in EntityGridPrepareCanvas (as Juha suggested) (The other code is just to select the cell the mouse is hoovering over.) I leave it up to you to change font color...
procedure THtmlCharmapForm.EntityGridMouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
Row, Col, Idx: Integer;
S: String;
begin
//debugln('THtmlCharmapForm.EntityGridMouseDown');
if (Button = mbLeft) and (EntityGrid.MouseToGridZone(X, Y) = gzNormal) then
begin
EntityGrid.MouseToCell(X, Y, Col, Row);
//debugln(' Col = ',dbgs(col),' Row = ',dbgs(row));
Idx := RowColToIndex(Row, Col);
//debugln(' Idx = ',dbgs(idx));
S := HtmlEntityFromIndex(SelectionBox.ItemIndex, Idx);
//debugln(' S = ',S);
DoOnHtmlCharClick(S);
end;
end;
procedure THtmlCharmapForm.EntityGridMouseMove(Sender: TObject;
Shift: TShiftState; X, Y: Integer);
var
Row, Col, Idx: Integer;
S: String;
begin
//debugln('THtmlCharmapForm.EntityGridMouseMove');
if (EntityGrid.MouseToGridZone(X, Y) = gzNormal) then
begin
EntityGrid.MouseToCell(X, Y, Col, Row);
//debugln(' Col = ',dbgs(col),' Row = ',dbgs(row));
Idx := RowColToIndex(Row, Col);
//debugln(' Idx = ',dbgs(idx));
S := HtmlEntityFromIndex(SelectionBox.ItemIndex, Idx);
//debugln(' S = ',S);
EntityGrid.Row := Row;
EntityGrid.Col := Col;
ExampleLabel.Caption := S;
end;
end;
procedure THtmlCharmapForm.EntityGridPrepareCanvas(sender: TObject; aCol,
aRow: Integer; aState: TGridDrawState);
begin
if (gdSelected in aState) then
begin
EntityGrid.Canvas.Brush.Color := clLime;
end
end;Bart