petter
petter

Reputation: 83

Delphi, Show button in a different TGridPanelLayout cell

Hi I am working with XE6 and I am using a TGridPanelLayout with 4 col and 4 rows. On the first cell I am displaying a Button. What I would like to do is, that when I click on this Button, get that Button to appear in a different cell. But I cannot find how to do it, so far I tried this, but nothing happens.

procedure TForm4.Button1Click(Sender: TObject);
begin
GridMyPannel.ControlCollection.BeginUpdate;
GridMyPannel.ControlCollection.AddControl(Button1, 2, 2);
Button1.Parent := GridMyPannel;
end;

I am really new on Delphi. Could anyone give me an example of how I could do it?

Upvotes: 0

Views: 2155

Answers (2)

A TGridPanel has a ControlCollection property which allows access to the Row and Column properties that also appear on your TButton once you've placed in inside your TGridpanel. A TButton (or rather its superclass TControl) does not have a Row or Column property. So we need to get a grip of the TControlItem wrapper the TGridpanel uses.

procedure TForm8.Button1Click(Sender: TObject);
var
    selectedControl:        TControl;
    itemIndex:              Integer;
    selectedControlItem:    TControlItem; // This knows about "Row" and "Column"
begin
    // This is the button we've clicked
    selectedControl := Sender as TControl;

    itemIndex := GridPanel1.ControlCollection.IndexOf(selectedControl);
    if (itemIndex <> -1) then begin
        selectedControlItem := GridPanel1.ControlCollection.Items[itemIndex];
        selectedControlItem.Row := Random(GridPanel1.RowCollection.Count);
        selectedControlItem.Column := Random(GridPanel1.ColumnCollection.Count);
    end;
end;

The above code finds the button and changes its Row and Column property to a random value. Note that you didn't specify whether the TButton is the only control within the TGridpanel. Is that the case?

Upvotes: 2

Blurry Sterk
Blurry Sterk

Reputation: 1607

I did the below in normal VCL and XE3 and with a TGridPanel (no TGridPanelLayout in my Delphi).

The problem with the GridPanel is that it does not allow controls (Buttons, etc) to be placed in any cell (like Cell:1,1) without having controls in the cells before that cell. GridPanel always fills itself from Index 0 upwards.

So the trick is to fool it. Now depending on whether you already have other cells in the GridPanel you will have to make place for the button to go to and also put something else in its place if the button was in a cell of lower index.

Have a look at the form before the button is pressed:

enter image description here

Note that I have not created a ControlItem at cell 1,0 yet.

I want to move Button 1 to cell 1,0. I cannot do that unless I first place something else in its place (cell 0,0). I have to create a new ControlItem at cell 1,0 to house button1.

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Places CheckBox1 in the same cell as BUtton1
  GridPanel1.ControlCollection.ControlItems[0,0].Control := CheckBox1;
  // Create a new ControlItem for Button1 and in the same breath move
  // Button1 to it
  GridPanel1.ControlCollection.AddControl(Button1,1,0);
  // You know what this does. :)
  CheckBox1.Parent := GridPanel1;
end;

The result:

enter image description here

Upvotes: 1

Related Questions