Solved

colored String grid

Posted on 2001-06-29
17
430 Views
Last Modified: 2010-04-06
hi all!
i created my own component based on the TStringGrid one, but i want to let the user choose the color of the grid. i think i should add a 'gridCol' property, and then i should override the painting method. but i don't know how to proceed to override this method, and to draw my grid (with the strings) with the grid colored with my color.

regard, ralph
0
Comment
Question by:ralph78
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +4
17 Comments
 
LVL 4

Expert Comment

by:jsweby
ID: 6237573
Doesn't the TStringGrid already have a Color property?

J.
0
 

Author Comment

by:ralph78
ID: 6237593
yes, but this property concerns the background color. i want to be able to choose the color of the grids (lines across the control ;-))

raph
0
 
LVL 4

Expert Comment

by:jsweby
ID: 6237609
Something like this:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
   With StringGrid1.Canvas Do
   Begin
      If {your criteria} then
      Begin
         brush.color := clRed;
         Font.Color := clWhite;
      End
      Else
      Begin
         brush.color := clWhite;
         Font.Color := clRed;
      End;

      FillRect(Rect);
      TextOut(Rect.Left, Rect.Top, 'Your text');
   End;
end;
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:ITugay
ID: 6237659
Hi ralph78,

then you should draw rectangle around the grid's cell manually.

  InflateRect(Rect, 1, 1);
  Canvas.Pen.Color := clRed;
  Canvas.Rectangle(Rect);

-----
Igor.
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6237734
Hello

  you can do that by override the cell color in the stringgird drawcell event

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
    with StringGrid1 do
      begin
        Canvas.Brush.Color := clYellow;
        Canvas.FillRect(Rect);
      end;
end;

Mohammed
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6237761
and there are alot of stringgird that allow u to change the color, look at torry site

http://homepages.borland.com/torry/stringgrids.htm

0
 
LVL 3

Expert Comment

by:alzv
ID: 6238074
Well...
Declaring such property is not so hard...
property GridLineColor: TColor read FGridLoneColor write SetGridLineColor;

But drawing grid lines yourself is not so easy... There is a special procedure DrawLines inside a Paint method of TCustomGrid (line number 1616 in Grids.pas VCL unit). So, to draw it, you need to fully rewrite Paint method of TCustomGrid control.

Best regards,
Alexey Zverev.
0
 
LVL 4

Expert Comment

by:YodaMage
ID: 6238139
Buy Infopower. TwwDBGrid does what you want.
0
 
LVL 5

Expert Comment

by:alanwhincup
ID: 6238723
Heres how it could be done:

var
  Form1 : TForm1;
  Drawn : Boolean;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Drawn := False;
  with StringGrid1 do
  begin
    FixedCols := 0;
    FixedRows := 0;
    Options := [goFixedVertLine,goFixedHorzLine,goRangeSelect];
    cells[1,1] := 'hello';
  end;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  S : string;
begin
  S := '';
  with StringGrid1.Canvas do
  begin
    if Drawn = False then
    begin
      if StringGrid1.Cells[ACol, ARow] <> '' then
        S := StringGrid1.Cells[ACol, ARow];
      Brush.Color := clRed;
      FillRect(Canvas.ClipRect);
      Brush.Color := clWhite;
      FillRect(Rect);
      if S <> '' then
        StringGrid1.Cells[ACol, ARow] := S;
      Drawn := True;
    end;
  end;
end;

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
begin
  Drawn := False;
end;

Cheers,

Alan
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6238775
hi ralph,

nothing difficult, here is working sample how to make your grid with red lines:

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  InflateRect(Rect, 1, 1);
  with StringGrid1,Canvas do
  begin
    TextRect(Rect, Rect.Left+1, Rect.Top+1, Cells[ACol, ARow]);
    Pen.Color := clRed;

    Brush.Style := bsClear;
    Rectangle(Rect);
    Brush.Style := bsSolid;
  end;
end;

-----
Igor.
0
 
LVL 4

Expert Comment

by:jsweby
ID: 6238790
All pretty similar to mine, then. :)

J.
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6238816
jsweby,

no, my sample draws colored lines instead of colored background.
0
 

Author Comment

by:ralph78
ID: 6247867
ok, thanks to everybody. now i can use my grid component, but another problem has occured: i can do everything i want with my grid in the conception mode, but when i try to run a form containing my component, an error occurs and tell me that my 'ForeColor' property does not exist. (but i can modify this property in the conception mode).
i'm sorry to post this second question, but i really need help (the project is quite urgent)

thanks to all!

regards, raph
0
 
LVL 9

Expert Comment

by:ITugay
ID: 6247938
You should recompile your package (.DPK). You removed ForeColor property from your component, but it still there in designtime package. After recompiling DPK, you should open form with the grid and choose "ignore" options in dialog apears.

-----
Igor
0
 

Author Comment

by:ralph78
ID: 6248018
ok, but this i don't want to remove this property, and i didn't removed it!
0
 
LVL 9

Accepted Solution

by:
ITugay earned 100 total points
ID: 6248040
The message you saw mean that ForeColor property missed in runtime package. May be you have more then one copy of your component. Check you HDD for duplicate unit.

-----
Igor.
0
 

Author Comment

by:ralph78
ID: 6248146
ok, it's working!
thanks to everyone!

raph
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question