Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to highlight the current cell in a TStringGrid Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
05-Nov-02
Category
VCL-General
Language
Delphi 2.x
Views
96
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

How to highlight the current cell in a TStringGrid

Answer:

You solve your problem by using a handler for the grids OnDrawCell event. There you 
draw both cell background and text when the cell is in the column or row of the 
selected cell. You also need a handler for OnSelectCell. Here you invalidate the 
row and column for the old selected cell (still indicated by the grids Col and Row 
property) and then you invalidate the row and column for the newly selected cell 
(indicated by the handlers aCol and arow parameters). The TStringGrid class 
inherits some protected methods from its ancestors which you need for this task. 
These are InvalidateRow and InvalidateCol. You get at them using a cracker class.

1   type
2     TGridCracker = class(TStringgrid);
3   
4   procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
5     Rect: TRect; State: TGridDrawState);
6   var
7     grid: TStringGrid;
8   begin
9     grid := Sender as TStringGrid;
10    if not (gdfixed in State) and ((grid.row = arow) or (grid.col = acol)) then
11    begin
12      with grid.canvas do
13      begin
14        brush.color := clYellow;
15        if gdSelected in State then
16          Font.color := clBlue
17        else
18          Font.color := clBlack;
19        Fillrect(rect);
20        TextRect(Rect, Rect.left + 2, rect.top + 2, grid.Cells[acol, arow]);
21      end;
22    end;
23  end;
24  
25  procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
26    ARow: Integer; var CanSelect: Boolean);
27  begin
28    with TGridCracker(Sender as TStringGrid) do
29    begin
30      {redraw column and row if currently selected cell}
31      Invalidaterow(row);
32      Invalidatecol(col);
33      {redraw column and row of new selected cell}
34      Invalidaterow(arow);
35      Invalidatecol(acol);
36    end;
37  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC