Solved

TStringGrid - How do you draw a TPanel

Posted on 2006-11-14
12
453 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now