Solved

TStringGrid - How do you draw a TPanel

Posted on 2006-11-14
12
462 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

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…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

807 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