Solved

TStringGrid - How do you draw a TPanel

Posted on 2006-11-14
12
461 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
  • 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

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…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 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