Solved

TStringGrid - How do you draw a TPanel

Posted on 2006-11-14
12
467 Views
Last Modified: 2010-04-05
Hi,
How do you draw a TPanel component in a TStringGrid Cell?
Many Thanks
0
Comment
Question by:Vector7
[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
  • 7
  • 3
  • 2
12 Comments
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17939146
Try this


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, Grids, StdCtrls;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Panel1: TPanel;
    procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer; var CanSelect: Boolean);
var
  Rect: TRect;
begin
  if (ARow = 0) and (ACol = 0) then
  begin
    Panel1.Visible := False;
    Exit;
  end;

  Panel1.Parent := StringGrid1;

  Rect := StringGrid1.CellRect( ACol, ARow);

  Panel1.Left := Rect.Left;
  Panel1.Top := Rect.Top;
  Panel1.Width := Rect.Right - Rect.Left;
  Panel1.Height  := Rect.Bottom - Rect.Top;

  Panel1.Visible := True;
end;

end.



object Form1: TForm1
  Left = 260
  Top = 160
  Width = 415
  Height = 315
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object StringGrid1: TStringGrid
    Left = 24
    Top = 16
    Width = 353
    Height = 233
    TabOrder = 0
    OnSelectCell = StringGrid1SelectCell
  end
  object Panel1: TPanel
    Left = 208
    Top = 152
    Width = 105
    Height = 41
    Caption = 'Panel1'
    TabOrder = 1
    Visible = False
  end
end
0
 

Author Comment

by:Vector7
ID: 17939951
Thankyou for the response.
I should have mentioned that the TStringGrid.Objects are TPanels and what I would like to do is paint the TPanel in the TStringGrid cell.
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17940185
Do you want to paint all the panels at once or just when you select a cell?
0
Technology Partners: 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!

 

Author Comment

by:Vector7
ID: 17940679
At once thanks.
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17942517
Ok here is another quick demo application


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, ExtCtrls;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  i, j: Integer;
  Panel: TPanel;
begin
  with StringGrid1 do
  begin
    for i := 0 to ColCount - 1 do
      for j := 0 to RowCount - 1 do
      begin
        Panel := TPanel.Create( nil);
        Panel.Caption := IntToStr( i) + ' , ' + IntToStr( j);
        Objects[ i, j] := Panel;
      end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  i, j: Integer;
  Panel: TPanel;
  Rect: TRect;
begin
  with StringGrid1 do
  begin
    for i := 0 to ColCount - 1 do
      for j := 0 to RowCount - 1 do
      begin
        Panel := TPanel( Objects[ i, j]);
        Panel.Parent := StringGrid1;
        Rect := CellRect( i, j);

        Panel.Left := Rect.Left;
        Panel.Top := Rect.Top;
        Panel.Width := Rect.Right - Rect.Left;
        Panel.Height  := Rect.Bottom - Rect.Top;

        Panel.Visible := True;
      end;
  end;
end;

end.



object Form1: TForm1
  Left = 264
  Top = 160
  Width = 483
  Height = 344
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object StringGrid1: TStringGrid
    Left = 64
    Top = 64
    Width = 345
    Height = 145
    FixedCols = 0
    FixedRows = 0
    TabOrder = 0
  end
  object Button1: TButton
    Left = 328
    Top = 224
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
end
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17949989
Hey, Mike, maybe you should use the DrawCell event, instead?
0
 
LVL 15

Accepted Solution

by:
mikelittlewood earned 125 total points
ID: 17950971
Hey Eddie

You are probably right, but I just wanted to show how to display panels in a grid as and when he wanted.

Here is the OnDrawCell event procedure instead for the grid.

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  Panel: TPanel;
begin
  with StringGrid1 do
  begin
    Panel := TPanel( Objects[ ACol, ARow]);
    Panel.Parent := StringGrid1;
    Rect := CellRect( ACol, ARow);

    Panel.Left := Rect.Left;
    Panel.Top := Rect.Top;
    Panel.Width := Rect.Right - Rect.Left;
    Panel.Height  := Rect.Bottom - Rect.Top;

    Panel.Visible := True;
  end;
end;
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 17955907
Check, also if the Object in the Objects is nil... ;-0
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17957062
Damn you Eddie .. I hate being pointed out that sometimes Im just lazy he he   :op

After this line Vector

Panel := TPanel( Objects[ ACol, ARow]);

put

if Panel = nil then exit;
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17958013
In fact, maybe before that line put

if Objects[ ACol, ARow] <> nil then
begin

  // insert rest of code here

end;
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 17958033
In fact you might even want to check if there is an object, that it is a TPanel as well

if Objects[ ACol, ARow] <> nil then
  if Objects[ ACol, ARow] is TPanel then
  begin

  // insert rest of code here

  end;
0
 

Author Comment

by:Vector7
ID: 17959718
Code works well.
Many thanks Mike.
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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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