線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1596
推到 Plurk!
推到 Facebook!

TStringGrid Color Problem

尚未結案
BorlandUser
中階會員


發表:148
回覆:217
積分:73
註冊:2004-02-19

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-09-15 10:13:23 IP:203.185.xxx.xxx 未訂閱
procedure TMainCustomerForm.ContactGridDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); begin with Sender as TStringGrid do begin // ©?¦²®? //if State=[gdSelected] then begin if (ACol = 2) and (ARow > 0) Then Begin Canvas.Brush.Color:= $0080FFFF; //©³¦â Canvas.FillRect(Rect); //ø©³¦â End; end; End; 這句在當我不是在grid中打字時,是沒有問題,但當我在grid中打字時,color郤消失了.......為何呢?
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-09-15 14:52:09 IP:147.8.xxx.xxx 未訂閱
When editing a grid, an inplace editor (TInplaceEdit) will be created and shown on TOP of the grid.
BorlandUser
中階會員


發表:148
回覆:217
積分:73
註冊:2004-02-19

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-09-15 16:04:00 IP:203.185.xxx.xxx 未訂閱
那即是....???
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-09-15 16:38:41 IP:147.8.xxx.xxx 未訂閱
The proper way is to create your own grid/inplace editor  >> >> > < class="code">unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, Grids, StdCtrls; type TStringGrid = class(Grids.TStringGrid) protected function CreateEditor: TInplaceEdit; override; end; TForm1 = class(TForm) StringGrid1: TStringGrid; private public end; var Form1: TForm1; implementation {$R *.dfm} type TMyEdit = class(TCustomEdit); function TStringGrid.CreateEditor: TInplaceEdit; begin Result := inherited CreateEditor; TMyEdit(Result).Color := clBlue; end; end.
Miles
尊榮會員


發表:27
回覆:662
積分:622
註冊:2002-07-12

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-09-16 16:29:15 IP:220.135.xxx.xxx 未訂閱
Hi BorlandUser 您好: 試試這個~~
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
   with Sender as TStringGrid do begin
        if (ACol = 2) and (ARow > 0) Then Begin
           Canvas.Brush.Color:= $0080FFFF;
           Canvas.Font.Color := clBlack; // 文字色自己決定
           Canvas.FillRect(Rect);
           Canvas.TextOut(Rect.TopLeft.x,Rect.TopLeft.y 1,Cells[ACol, ARow]);            End;
   end;
end;
我不是高手, 高手是正在銀幕前微笑的人.
------


我不是高手, 高手是正在銀幕前微笑的人.
BorlandUser
中階會員


發表:148
回覆:217
積分:73
註冊:2004-02-19

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-09-16 16:56:06 IP:203.185.xxx.xxx 未訂閱
To: Miles 很多謝你的意見,但是你這個方法如果在stringgrid裏打text時,顏色又會轉回白色.. 再者我想問一個問題,有什麼方法可以讓某一個欄位不會進入editor模式.即可以打字,因為現在我是用keypress或keydown來控制,如果有些欄位是不能給user輸入,有些郤可以,我不能設定goediting為false,所以我想請問大家有什麼好方法嗎? 謝
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-09-16 17:10:37 IP:147.8.xxx.xxx 未訂閱
Override CanEditShow method of your grid.
BorlandUser
中階會員


發表:148
回覆:217
積分:73
註冊:2004-02-19

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-09-16 17:24:18 IP:203.185.xxx.xxx 未訂閱
請問能詳細一點嗎?
william
版主


發表:66
回覆:2535
積分:3048
註冊:2002-07-11

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-09-16 17:37:39 IP:147.8.xxx.xxx 未訂閱
Modify the above example to show editor for odd column only:    
unit Unit1;    interface    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, Grids, StdCtrls;    type
    TStringGrid = class(Grids.TStringGrid)
    protected
        function CreateEditor: TInplaceEdit; override;
        function CanEditShow: Boolean; override;
    end;        TForm1 = class(TForm)
        StringGrid1: TStringGrid;
    private
    public
    end;    var
    Form1: TForm1;    implementation    {$R *.dfm}    type
    TMyEdit = class(TCustomEdit);    function TStringGrid.CanEditShow: Boolean;
begin
    Result := inherited CanEditShow and Odd(Col); 
end;    function TStringGrid.CreateEditor: TInplaceEdit;
begin
    Result := inherited CreateEditor;
    TMyEdit(Result).Color := clBlue;
end;    end.
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#10 引用回覆 回覆 發表時間:2004-09-16 17:40:15 IP:61.222.xxx.xxx 未訂閱
在 SelectCell事件中添加。 procedure TFS23.GD02SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean); begin GD02.EditorMode := Acol = 5 ; { COL = 5 允許編輯 } if Acol = 5 then GD02.Options := GD02.Options [goediting] else GD02.Options := GD02.Options - [goediting]; end; 最好加上 .EditorMode 用來關閉游標,避免 游標移位。這點很重要! 在 Drawcell 事件中,可透過 Grid.Canvas 繪製背景灰色(無法編輯的地方)。
BorlandUser
中階會員


發表:148
回覆:217
積分:73
註冊:2004-02-19

發送簡訊給我
#11 引用回覆 回覆 發表時間:2004-09-17 07:54:10 IP:203.218.xxx.xxx 未訂閱
"最好加上 .EditorMode 用來關閉游標,避免 游標移位。這點很重要!" 這個我不太明白,因為就算不加這句,也會做到同樣效果,所以請問這句的用意為何呢? 還有,要多謝你的方法,你的方法可以不用改grid的原有function....
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#12 引用回覆 回覆 發表時間:2004-09-17 12:28:31 IP:61.222.xxx.xxx 未訂閱
當 OPTIONS 有勾選goAlwaysShowEditor 在視窗一開啟時,如果這時又Grid 屬於 Focused 狀態 有可能會產生游標在不允許輸入的方格內 這樣就可以編輯不允許異動的欄位。 當然為保險起見。 先將編輯狀態關閉。 Grid.EditorMode := false;
BorlandUser
中階會員


發表:148
回覆:217
積分:73
註冊:2004-02-19

發送簡訊給我
#13 引用回覆 回覆 發表時間:2004-09-17 12:34:18 IP:203.185.xxx.xxx 未訂閱
感謝解答,問題都已解決,但我有一點不明白是為何你 GD02.EditorMode := Acol = 5 ; { COL = 5 允許編輯 } 這句是沒有問題呢? 因為我看help時,editormode是要設為true/false,但你郤設為數字,但又沒有發生錯誤,請問何解?還有這句用意又為何呢? 正如你之前所說,要加一句editormode為false的sentence,那即是否要變成這樣...: if Acol = 5 then begin GD02.Options := GD02.Options [goediting]; GD02.EditorMode := True; End Else Begin GD02.Options := GD02.Options - [goediting]; GD02.EditorMode := False; End; Am I correct?
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#14 引用回覆 回覆 發表時間:2004-09-17 12:40:09 IP:61.222.xxx.xxx 未訂閱
你的頭有點暈歐! Grid.EditorMode := ACOL = 5; -------- 相同於 if ACOL = 5 then Grid.EditorMode := True else Grid.EditorMode := false;
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#15 引用回覆 回覆 發表時間:2004-09-17 12:42:54 IP:61.222.xxx.xxx 未訂閱
那個是 條件式 啦! if ACOL = 5 then Grid.EditorMode := True else Grid.EditorMode := false; 這樣表示方式比較囉唆! 所以我都習慣用 Grid.EditorMode := ACOL = 5;
BorlandUser
中階會員


發表:148
回覆:217
積分:73
註冊:2004-02-19

發送簡訊給我
#16 引用回覆 回覆 發表時間:2004-09-17 12:52:28 IP:203.185.xxx.xxx 未訂閱
哈哈..不好意思,因為我從來沒有用過這種寫法..我總會分幾行if...then..else 因為看來去較清楚,雖然code會較多幾行... 所以一時看不明白在寫什麼 :-p 不過我很多謝你的意見和解答 thx
系統時間:2024-07-01 2:50:24
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!