Solved

colored String grid

Posted on 2001-06-29
17
432 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

617 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