為什麼我在TSortGrid內動態產生Button,都會變成兩個! |
答題得分者是:AndrewK
|
bill534
一般會員 發表:38 回覆:51 積分:17 註冊:2007-02-22 發送簡訊給我 |
顯示結果如附件圖檔!
procedure TMyRecipe.SetSortGrid(const Value: TSortGrid); begin if FSortGrid <> value then begin if FCount <= 1 then FCount := 99; FSortGrid := Value; FSortGrid.ClickSorting := False; FSortGrid.Height := 36 * 16 19; FSortGrid.Ctl3D := false; FSortGrid.RowCount := FCount 1; FSortGrid.FixedCols := 0; FSortGrid.FixedRows := 1; FSortGrid.DefaultRowHeight := 36; FSortGrid.Font.Size := 12; FSortGrid.Options := FSortGrid.Options [goRowSelect] - [goHorzLine]; FSortGrid.Width := 851; FSortGrid.ColCount := 4; FSortGrid.ColWidths[0] := 48; FSortGrid.ColWidths[1] := 180; FSortGrid.ColWidths[2] := 360; FSortGrid.ColWidths[3] := 241; FSortGrid.OnGetCellFormat := GridGetCellFormat; FSortGrid.OnSelectCell := GridSelectCell; if FSelectBtn = nil then begin FSelectBtn := TMyButton.Create(Owner); FSelectBtn.Visible := False; FSelectBtn.Height := 32; FSelectBtn.Width := 78; FSelectBtn.Caption := 'SELECT'; FSelectBtn.OffColor := clBlue; FSelectBtn.OnColor := clBlue; FSelectBtn.Font.Color := clWhite; FSelectBtn.Font.Size := 12; FSelectBtn.Parent := FSortGrid; FSelectBtn.Tag := 0; FSelectBtn.OnBtnClick := DoBtnClick; // 一個會觸發事件,一個不會觸發! end; if FEditBtn = nil then begin FEditBtn := TMyButton.Create(Owner); FEditBtn.Visible := False; FEditBtn.Height := 32; FEditBtn.Width := 78; FEditBtn.Caption := 'EDIT'; FEditBtn.OffColor := clGreen; FEditBtn.OnColor := clGreen; FEditBtn.Font.Color := clWhite; FEditBtn.Font.Size := 12; FEditBtn.Parent := FSortGrid; FEditBtn.Tag := 2; FEditBtn.OnBtnClick := DoBtnClick; // 一個會觸發事件,一個不會觸發! end; if FDeleteBtn = nil then begin FDeleteBtn := TMyButton.Create(Owner); FDeleteBtn.Visible := False; FDeleteBtn.Height := 32; FDeleteBtn.Width := 78; FDeleteBtn.Caption := 'DELE'; FDeleteBtn.OffColor := clRed; FDeleteBtn.OnColor := clRed; FDeleteBtn.Font.Color := clWhite; FDeleteBtn.Font.Size := 12; FDeleteBtn.Parent := FSortGrid; FSelectBtn.Tag := 1; FDeleteBtn.OnBtnClick := DoBtnClick; // 一個會觸發事件,一個不會觸發! end; Load; end; end; // 選定顯示位置 procedure TMyRecipe.GridSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean); var nRect: TRect; b1: boolean; nLeft: integer; begin b1 := (ARow > 0) and (FSortGrid.Cells[1, ARow] <> ''); nRect := FSortGrid.CellRect(3, ARow); nLeft := nRect.Left 2; if FSelectBtn <> nil then begin if b1 then begin FSelectBtn.Top := nRect.Top 2; FSelectBtn.Left := nLeft; nLeft := FSelectBtn.Left FSelectBtn.Width 2; end; FSelectBtn.Visible := b1; end; if FDeleteBtn <> nil then begin if b1 then begin FDeleteBtn.Top := nRect.Top 2; FDeleteBtn.Left := nleft; nLeft := FDeleteBtn.Left FDeleteBtn.Width 2; end; FDeleteBtn.Visible := b1; end; if FEditBtn <> nil then begin if b1 then begin FEditBtn.Top := nRect.Top 2; FEditBtn.Left := nleft; nLeft := FEditBtn.Left FEditBtn.Width 2; end; FEditBtn.Visible := b1; end; CanSelect := True; end;
附加檔案:487f1c72511ff_E1.jpg
|
AndrewK
高階會員 發表:6 回覆:151 積分:161 註冊:2006-10-09 發送簡訊給我 |
因提供的資訊有限,有以下幾點給你做參考
1. 於 procedure TMyRecipe.SetSortGrid(const Value: TSortGrid); Load 函式是用來做什麼的呢 2. TSortGrid 我下載不到,我拿 TStringGrid 來測試是沒有問題 3. TMyButton 在 Designed 畫面有幾個,程式碼中於 動態 Create FEditBtn 、 FSelectBtn 、 FDeleteBtn 時有限制只能 Create 一次 看看那個是不是額外自己產生 4. 檢查看看 TMyButton 這個自製元件 Create 函式
------
Just Do It ------------------------- 其實男生不是真的喜歡你不減肥,而是喜歡你愛吃還不肥;也不是真的喜歡你不化妝,而是喜歡你素顏也好看;也不是真的喜歡你瘦,而是喜歡你瘦卻有胸;也不是真喜歡你獨立,而是他忙的時候別煩他。女孩子,太認真你就輸了。 |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
元件屬性不同於 一般基本資料型態的屬性
對於被置換的舊物件需謹慎處理 並且元件屬性中所指向物件的Owner及Parent 也必須適當加以安排 以下程式碼,通常教科書上決不會建議這樣寫 以一個TComponent(不可視元件)去包含一個TControl(可視元件) 純粹舉例說明提問中可能的問題所在, 僅供參考.... [code delphi] unit fMain; interface uses Windows, Forms, Grids, StdCtrls, SysUtils, Classes, Controls; type TSetupGridEvent = procedure(Sender: TObject; Grid: TStringGrid) of object; TTheRecipe = class(TComponent) private FGrid: TStringGrid; FOnSetupGrid: TSetupGridEvent; procedure SetGrid(const Value: TStringGrid); procedure SetGridParented(Value: TStringGrid); function GetGrid: TStringGrid; public destructor Destroy; override; property Grid: TStringGrid read GetGrid write SetGrid; published property OnSetupGrid: TSetupGridEvent read FOnSetupGrid write FOnSetupGrid; end; TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Button0: TButton; procedure FormCreate(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure Button0Click(Sender: TObject); public TheRecipe: TTheRecipe; procedure DoOnSetupGrid(Sender: TObject; Grid: TStringGrid); end; var Form1: TForm1; implementation {$R *.dfm} { TTheRecipe } destructor TTheRecipe.Destroy; begin //FreeAndNil(FGrid);<--Owner 負責最後釋放工作 inherited; end; function TTheRecipe.GetGrid: TStringGrid; begin if FGrid = nil then begin //Create Default Grid FGrid := TStringGrid.Create(Self); SetGridParented(FGrid); end; Result := FGrid; end; procedure TTheRecipe.SetGrid(const Value: TStringGrid); var temp: TObject; begin if Assigned(Value) and (Value <> FGrid) then begin temp := FGrid; FGrid := Value; SetGridParented(FGrid); if Assigned(temp) then temp.Free; end; end; procedure TTheRecipe.SetGridParented(Value: TStringGrid); begin if Owner is TWinControl then begin FGrid.Parent := TWinControl(Owner); if Assigned(FOnSetupGrid) then FOnSetupGrid(Self, FGrid); end; end; { TForm1 } procedure TForm1.FormCreate(Sender: TObject); begin Randomize; end; procedure TForm1.Button0Click(Sender: TObject); begin if TheRecipe = nil then begin TheRecipe := TTheRecipe.Create(Self); TheRecipe.OnSetupGrid := DoOnSetupGrid; Button1Click(nil); end; end; procedure TForm1.Button3Click(Sender: TObject); begin if Assigned(TheRecipe) then FreeAndNil(TheRecipe); end; procedure TForm1.DoOnSetupGrid(Sender: TObject; Grid: TStringGrid); begin Grid.BoundsRect := Rect(0,0,340,150); Grid.Color := RGB(Random($100),Random($100),Random($100)); Grid.FixedColor := RGB(Random($100),Random($100),Random($100)); Grid.Font.Color := Grid.Color xor $FFFFFF; end; procedure TForm1.Button1Click(Sender: TObject); begin if Assigned(TheRecipe) then TheRecipe.Grid.Cells[1,1] := 'TEST_AAA'; end; procedure TForm1.Button2Click(Sender: TObject); begin if Assigned(TheRecipe) then begin TheRecipe.Grid := TStringGrid.Create(TheRecipe); TheRecipe.Grid.Cells[1,1] := 'TEST_BBB'; end; end; end. [/code] |
jow
尊榮會員 發表:66 回覆:751 積分:1253 註冊:2002-03-13 發送簡訊給我 |
沒有問題了嗎???
希望你有抓到重點 其實我要提點的是以下的程式碼... procedure TMyRecipe.SetSortGrid(const Value: TSortGrid); begin if FSortGrid <> value then begin if FCount <= 1 then FCount := 99; FSortGrid := Value; <<--這樣寫會有問題喔! 若有舊物件的話... 僅供參考...
編輯記錄
jow 重新編輯於 2008-07-18 17:04:06, 註解 無‧
|
bill534
一般會員 發表:38 回覆:51 積分:17 註冊:2007-02-22 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |