[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 512
  • Last Modified:

TStringGrid - How do you draw a TPanel

Hi,
How do you draw a TPanel component in a TStringGrid Cell?
Many Thanks
0
Vector7
Asked:
Vector7
  • 7
  • 3
  • 2
1 Solution
 
mikelittlewoodCommented:
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
 
Vector7Author Commented:
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
 
mikelittlewoodCommented:
Do you want to paint all the panels at once or just when you select a cell?
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.

 
Vector7Author Commented:
At once thanks.
0
 
mikelittlewoodCommented:
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
 
EddieShipmanCommented:
Hey, Mike, maybe you should use the DrawCell event, instead?
0
 
mikelittlewoodCommented:
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
 
EddieShipmanCommented:
Check, also if the Object in the Objects is nil... ;-0
0
 
mikelittlewoodCommented:
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
 
mikelittlewoodCommented:
In fact, maybe before that line put

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

  // insert rest of code here

end;
0
 
mikelittlewoodCommented:
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
 
Vector7Author Commented:
Code works well.
Many thanks Mike.
0

Featured Post

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.

  • 7
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now