0

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?

Randyka Yudhistira
  • 3,612
  • 1
  • 26
  • 41
petter
  • 83
  • 1
  • 5
  • I don't know how new you are to Delphi but a word of advice; Visually play around with controls to figure out what is required by them. That will give you some insight. Try many things. Even things which you would think would not be done. For instance; I have never worked with TGridPanel before. I first played with it, adding and removing, trying to add a button to the a cell already with another button. That is how I noticed that the GridPanel does not want empty cells in the lower cells if there are already filled cells higher up. Just a suggestion. :) – Blurry Sterk Feb 20 '15 at 06:49
  • I am a proper "Beginner". Thanks for the advise. – petter Feb 20 '15 at 22:27
  • If any of the answers gave the solution or are your preferred answer then please remember to accept that answer. – Blurry Sterk Feb 21 '15 at 06:57

2 Answers2

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?

  • Thanks for this example! It worked straight away with a little change “selectedControlItem: TGridPanelLayout.TControlItem;”. Thank you very much for your help! I was working only with one control (TButton) within the TGridPanelLayout. Now I am going to try to fill all the TGridPanelLayout (one Button per Cell) less one cell that will be empty. The aim is that when you click on one Button that Button will appear in the empty cell. Thanks again for the help. – petter Feb 20 '15 at 23:06
1

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

Blurry Sterk
  • 1,595
  • 2
  • 9
  • 18
  • Thank for this, now I understand why it was not working when I tried TGridPanel. I really appreciated. Cheers. – petter Feb 20 '15 at 23:11