全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2928
推到 Plurk!
推到 Facebook!

請教 GridPanel 動態物件

缺席
blue
中階會員


發表:170
回覆:136
積分:81
註冊:2002-04-15

發送簡訊給我
#1 引用回覆 回覆 發表時間:2020-04-23 09:48:03 IP:125.227.xxx.xxx 未訂閱
Hi,各位先進大家好:
因小弟要做動態選單,使用GridPanel,程式如下,但產生出來的結果相當詭異,
如果在 [0, 0] 上有 Button(如本例 A->[0,0] FMenuList.Add(Format('%s=%d,%d', ['A',0,0]));)
那麼 D->[4,1] 不會放在 [4,1],而是跑到 [0,1]
實在是很奇怪,
不知是何原因?
謝謝!

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type
TForm1 = class(TForm)
GridPanel1: TGridPanel;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FMenuList: TStrings;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
tmpButton: TButton;
CI: TControlItem;
Splited: TArray;
SRow, SCol, i, Index: Integer;
begin
for i := 0 to FMenuList.Count - 1 do begin
Splited := FMenuList.ValueFromIndex[i].Split([',']);
SRow := StrToInt(Splited[0]);
SCol := StrToInt(Splited[1]);
tmpButton := TButton.Create(Self);
tmpButton.Parent := GridPanel1;
tmpButton.Caption := FMenuList.Names[i];
Index := GridPanel1.ControlCollection.IndexOf(TControl(tmpButton));
CI := GridPanel1.ControlCollection.Items[Index];
CI.Row := SRow;
CI.Column := SCol;
end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FMenuList := TStringList.Create;

FMenuList.Add(Format('%s=%d,%d', ['A',0,0]));
// FMenuList.Add(Format('%s=%d,%d', ['A',0,3]));

FMenuList.Add(Format('%s=%d,%d', ['B',0,3]));
FMenuList.Add(Format('%s=%d,%d', ['C',1,1]));
FMenuList.Add(Format('%s=%d,%d', ['D',4,1]));
FMenuList.Add(Format('%s=%d,%d', ['E',4,2]));
end;

end.
blue
中階會員


發表:170
回覆:136
積分:81
註冊:2002-04-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2020-04-27 10:32:53 IP:125.227.xxx.xxx 未訂閱
已解,
CI.Row := SRow;
CI.Column := SCol;
改用 CI.SetLocaltion(SCol, SRow)
謝謝!

===================引 用 blue 文 章===================
Hi,各位先進大家好:
因小弟要做動態選單,使用GridPanel,程式如下,但產生出來的結果相當詭異,
如果在 [0, 0] 上有 Button(如本例 A->[0,0] FMenuList.Add(Format('%s=%d,%d', ['A',0,0]));)
那麼 D->[4,1] 不會放在 [4,1],而是跑到 [0,1]
實在是很奇怪,
不知是何原因?
謝謝!

unit Unit1;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;

type
TForm1 = class(TForm)
GridPanel1: TGridPanel;
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FMenuList: TStrings;
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
tmpButton: TButton;
CI: TControlItem;
Splited: TArray;
SRow, SCol, i, Index: Integer;
begin
for i := 0 to FMenuList.Count - 1 do begin
Splited := FMenuList.ValueFromIndex[i].Split([',']);
SRow := StrToInt(Splited[0]);
SCol := StrToInt(Splited[1]);
tmpButton := TButton.Create(Self);
tmpButton.Parent := GridPanel1;
tmpButton.Caption := FMenuList.Names[i];
Index := GridPanel1.ControlCollection.IndexOf(TControl(tmpButton));
CI := GridPanel1.ControlCollection.Items[Index];
CI.Row := SRow;
CI.Column := SCol;
end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
FMenuList := TStringList.Create;

FMenuList.Add(Format('%s=%d,%d', ['A',0,0]));
// FMenuList.Add(Format('%s=%d,%d', ['A',0,3]));

FMenuList.Add(Format('%s=%d,%d', ['B',0,3]));
FMenuList.Add(Format('%s=%d,%d', ['C',1,1]));
FMenuList.Add(Format('%s=%d,%d', ['D',4,1]));
FMenuList.Add(Format('%s=%d,%d', ['E',4,2]));
end;

end.
系統時間:2024-11-22 12:26:01
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!