?
Solved

colored String grid

Posted on 2001-06-29
17
Medium Priority
?
436 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
Independent Software Vendors: 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

719 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