Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

colored String grid

Posted on 2001-06-29
17
Medium Priority
?
437 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
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.

 
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

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!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

783 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