Solved

colored String grid

Posted on 2001-06-29
17
424 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
  • 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
 
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:mnasman
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:mnasman
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now