請教 GridPanel 動態物件 |
缺席
|
blue
中階會員 發表:170 回覆:136 積分:81 註冊:2002-04-15 發送簡訊給我 |
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 發送簡訊給我 |
已解,
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. |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |