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

dbGrid 要能選取一個 Block , 並填入一個字元

答題得分者是:wameng
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-06-08 01:15:09 IP:203.203.xxx.xxx 未訂閱
各位先進, 請教 dbGrid 可否像 StringGrid 一樣, 可以用滑鼠左鍵點選 -> 拖拉 -> 放開 就能 Select 一個 Block 並且在 MouseUp 時, 在這個 Block 中的每個 Cell 填入字元 'A'
wameng
版主


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

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-06-08 13:08:11 IP:61.222.xxx.xxx 未訂閱
可以 用 DBGrid1.Selection ~~~~~~~~~~~ 難得聰明,常常糊塗。 ~~~~~~~~~~~
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-06-08 14:02:52 IP:203.203.xxx.xxx 未訂閱
版主你好    dbGrid 沒有 selection 這個 property or Event or Method 還是 SelectedField ? SelectedIndex ? SelectedRows ? 然後該怎樣設定 ? 用哪個 event 寫填入的程式    萬分給他感謝 ..... 
wameng
版主


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

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-06-08 14:55:33 IP:61.222.xxx.xxx 未訂閱
procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  Cell: TGridCoord;
  SelectRect: TGridRect;
begin
  Cell := DBGrid1.MouseCoord(X,Y);
  SelectRect := TStringGrid(DBGrid1).Selection;      if ssLeft in Shift then
    if (Cell.X>-1) and (Cell.Y>-1) then
    begin
      SelectRect.BottomRight := Cell;
      TStringGrid(DBGrid1).Selection := SelectRect;
    end;
end;
參考,我簡單寫了一小段。 只能由左向右。 ~~~~~~~~~~~ 難得聰明,常常糊塗。 ~~~~~~~~~~~
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-06-08 17:47:55 IP:203.203.xxx.xxx 未訂閱
版主兄 : 果然功力深厚, 完全是我想像不到的程式碼 不過與我的問題, 似乎有點距離    Step 1 : 我想要在 dbGrid 上, 能夠像 StringGrid 一樣, 能夠用滑鼠左擊, 點在某個Cell上 -> 移動滑鼠 -> 放開滑鼠左鍵 如果是 stringGrid 會有一塊 selected Block , dbGrid 不行    Step 2 :  滑鼠放開的同時, 在 Block 中, 每個 Cell 填入 'A' 字元    p.s. 完全沒用到鍵盤 , 我的環境是 D5 , 應該沒差吧 ?    < >< >
wameng
版主


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

發送簡訊給我
#6 引用回覆 回覆 發表時間:2005-06-08 18:42:41 IP:61.222.xxx.xxx 未訂閱
事實上,要實現這樣的功能是不難的。 若不用 Selection的功能,就可能要自己畫。 可定義一變數為BRect:TRect  Selected Block 區域。 並在 OnMouseDown  ..MouseMove及MouseUp 處理。    另外在定義 2 個BOOKMARK 為第一個位置紀錄及最後的紀錄位置。 現在我要下班... 嘿嘿!回家先    晚點在上來,若有其他大大願意代勞!那就..... 或者您可以自己寫寫看。... ~~~~~~~~~~~ 難得聰明,常常糊塗。 ~~~~~~~~~~~
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#7 引用回覆 回覆 發表時間:2005-06-09 09:31:55 IP:203.203.xxx.xxx 未訂閱
不好意思, 為了這個功能已經花了近兩周的時間 實在是太肉腳了. 大大提到的幾個名詞, 正在努力找資訊消化中 ..... but 不樂觀 如果可以幫我寫具體一點, 我會 很.感.激  
wameng
版主


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

發送簡訊給我
#8 引用回覆 回覆 發表時間:2005-06-09 09:53:31 IP:61.222.xxx.xxx 未訂閱
type
  TDBGrid = Class(DBGrids.TDBGrid)
  Private
    fSelectedCells : TRect;
    Procedure SetSelectedCells(Value:TRect);
  Protected
    function  HighlightCell(DataCol, DataRow: Integer; const Value: string;
      AState: TGridDrawState): Boolean; Override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
      X, Y: Integer); override;
    procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
    procedure MouseUp(Button: TMouseButton; Shift: TShiftState;
      X, Y: Integer); override;
  Public
    ButtonDown :Boolean;
    Property SelectedCells:TRect read fSelectedCells write SetSelectedCells;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;      TForm1 = class(TForm)
    DBGrid1: TDBGrid;
    ....
    procedure DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
  public
  end;    var
  Form1: TForm1;    implementation    {$R *.DFM}    Function SameRect(ARect,BRect:TRect):Boolean;
begin
  With ARect do
    Result := (Left = BRect.Left) and (Right = BRect.Right)
              and (Top = BRect.Top) and (Bottom = BRect.Bottom);
end;    constructor  TDBGrid.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  ButtonDown := False;
  fSelectedCells:= Rect(-1,-1,-1,-1);
end;    destructor  TDBGrid.Destroy;
begin
  inherited Destroy;
end;    Procedure TDBGrid.SetSelectedCells(Value:TRect);
  Function GetBlockRect(GridRect:TRect):TRect;
  var
    RBRect : TRect;
  begin
    With GridRect do
    begin
      Result := CellRect(Left,Top);
      RBRect := CellRect(Right,Bottom);
      Result.BottomRight := RBRect.BottomRight;
    end;
  end;
var
  OldSltRect,TMPRect :TRect;
begin
  if Not SameRect(fSelectedCells,Value) then
  begin
    OldSltRect := fSelectedCells;
    fSelectedCells := Value;        if (OldSltRect.Left>-1) then
    begin
      TMPRect :=GetBlockRect(OldSltRect);
      Windows.InvalidateRect(Handle, @TMPRect, True);
    end;
    if (fSelectedCells.Left>-1) then
    begin
      TMPRect :=GetBlockRect(fSelectedCells);
      Windows.InvalidateRect(Handle, @TMPRect, True);
    end;
  end;
end;    procedure TDBGrid.MouseDown(Button: TMouseButton; Shift: TShiftState;X, Y: Integer);
var
  Cell: TGridCoord;
  TmpRect : TRect;
begin
  inherited MouseDown(Button, Shift, X, Y);
  if Not Assigned(DataSource.DataSet) then Exit;      TmpRect := Rect(-1,-1,-1,-1);
  With DataSource.DataSet do
    if (Button=mbLeft) and Active then
    begin
      Cell := MouseCoord(X,Y);
      if (Cell.X>-1) and (Cell.Y>-1) then
      begin
        TmpRect.TopLeft := Point(Cell.X,Cell.Y);
        TmpRect.BottomRight := TmpRect.TopLeft;            ButtonDown := True;
      end;
    end;
  SelectedCells := TmpRect;
end;    procedure TDBGrid.MouseMove(Shift: TShiftState; X, Y: Integer);
var
  Cell: TGridCoord;
  TmpRect : TRect;
begin
  inherited MouseMove(Shift, X, Y);      With DataSource.DataSet do
    if ButtonDown and (ssLeft in Shift) then
    begin
      Cell := MouseCoord(X,Y);
      if (Cell.X>-1) and (Cell.Y>-1) then
      begin
        TmpRect.TopLeft := Point(COL,ROW);
        TmpRect.BottomRight := TmpRect.TopLeft;            if Cell.X<=Col then TmpRect.Left:= Cell.X else TmpRect.Right:= Cell.X;
        if Cell.Y<=Row then TmpRect.Top:= Cell.Y else TmpRect.Bottom:= Cell.Y;            SelectedCells := TmpRect;
      end;
    end;
end;    procedure TDBGrid.MouseUp(Button: TMouseButton; Shift: TShiftState;X, Y: Integer);
begin
  inherited MouseUp(Button, Shift, X, Y);
  ButtonDown := False;
end;    function  TDBGrid.HighlightCell(DataCol, DataRow: Integer; const Value: string;AState: TGridDrawState): Boolean;
begin
  With fSelectedCells do
  begin
    Result := inherited HighlightCell(DataCol,DataRow,Value,AState)
              or ((Left>-1) and PtinRect(Rect(Left,Top,Right 1,Bottom 1),Point(DataCol 1,DataRow 1)));
  end;
end;    procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  I,Direction,MoveCount: Integer;
begin
  With DBGrid1,TStringGrid(DBGrid1),DBGrid1.DataSource.DataSet do
    if ButtonDown  then
    begin
      BeginUpdate;
      Try
        Direction := ORD(SelectedCells.Top=Row)*2-1;
        MoveCount := Abs(SelectedCells.Bottom-SelectedCells.Top) 1;            Repeat
          Edit;
          For I:=SelectedCells.Left to SelectedCells.Right do
            Columns[I-1].Field.AsString := '1';
          UpdateRecord;              Dec(MoveCount);
          if MoveCount>0 then MoveBy(Direction);
        until MoveCount<=0;
      Finally
        EndUpdate;
      end;
    end;
end;
~~~~~~~~~~~ 難得聰明,常常糊塗。 ~~~~~~~~~~~ 發表人 - wameng 於 2005/06/09 20:25:17
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#9 引用回覆 回覆 發表時間:2005-06-09 11:17:33 IP:203.203.xxx.xxx 未訂閱
wameng 兄, 真是太感人了, 在此先一拜  可見你的功力必定十分高深, 另人敬佩 這是傳說中的 製作元件 嗎? 因為我是初學者, 還沒碰過這樣大的陣仗... 待我先學一下, 元件的封裝及安裝, 再見分曉 >
wameng
版主


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

發送簡訊給我
#10 引用回覆 回覆 發表時間:2005-06-09 12:35:52 IP:61.222.xxx.xxx 未訂閱
暫時不用封裝元件。 可在 TForm1 =Class(Tform) 前將 Type TDBGrid = Class(DBGrids.TDBGrid) ..... TForm1=Class(TFrom); .... ~~~~~~~~~~~ 難得聰明,常常糊塗。 ~~~~~~~~~~~
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#11 引用回覆 回覆 發表時間:2005-06-09 13:24:24 IP:203.203.xxx.xxx 未訂閱
歹勢啦 ! TDBGrid 宣告放在 type 與 TForm1=class(TForm) TFBGrid 程式段, 放在 implementation 後面    在 Form 畫面, 拉一個 dbGrid 元件    compile , 出現錯誤訊息 , 如下 : [Error] Undeclared Identifier :'TBookMark' [Fatal Error] could not compile used unit 'unit1.pas'    可是, 查 help 確實有 TBookMark 這個 type 呀 ! 好怪 !
wameng
版主


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

發送簡訊給我
#12 引用回覆 回覆 發表時間:2005-06-09 13:42:41 IP:61.222.xxx.xxx 未訂閱
Uses DB 這個單元嗎! ~~~~~~~~~~~ 難得聰明,常常糊塗。 ~~~~~~~~~~~
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#13 引用回覆 回覆 發表時間:2005-06-09 15:49:28 IP:203.203.xxx.xxx 未訂閱
是, 沒 uses db    填值好像是用 dbgrid1.columns[0].field.value := 'A';    but 怎樣知道 , cell 有被 selected ?    快完成了 ! 請大大大幫個忙 .  
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#14 引用回覆 回覆 發表時間:2005-06-09 18:16:43 IP:203.203.xxx.xxx 未訂閱
嘻嘻 , 可以了 With DBGrid1 do begin   if ButtonDown then   begin     dbGrid1.DataSource.DataSet.First;     for i:=1 to dbGrid1.rowcount do begin       dbGrid1.DataSource.DataSet.Edit;       for j:=1 to dbGrid1.ColCount do begin         if (j>=dbGrid1.fSelectedCells.Left) and (j<=dbGrid1.fSelectedCells.Right) and            (i>=dbGrid1.fSelectedCells.Top) and (i<=dbGrid1.fSelectedCells.Bottom) then begin            dbGrid1.Fields[j-1].AsString:=cMod;         end;       end;       dbGrid1.DataSource.DataSet.next;     end;   end; end;    三塊給你買蔴薯 怎樣才能練到像你一樣厲害 ?! 
cwc65536
初階會員


發表:47
回覆:121
積分:48
註冊:2004-10-14

發送簡訊給我
#15 引用回覆 回覆 發表時間:2005-06-09 18:22:26 IP:203.203.xxx.xxx 未訂閱
1. 有沒有比較優的寫法? 我寫得笨笨的 2. 大大幫我解答這麼難的題目, 分數有沒有比較多呀! 3. 我一定不會讓大大失望的,等我練成武功,一定也會效法大大出來行俠仗義
wameng
版主


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

發送簡訊給我
#16 引用回覆 回覆 發表時間:2005-06-09 20:23:26 IP:61.31.xxx.xxx 未訂閱
引言: 1. 有沒有比較優的寫法? 我寫得笨笨的 2. 大大幫我解答這麼難的題目, 分數有沒有比較多呀! 3. 我一定不會讓大大失望的,等我練成武功,一定也會傚法大大出來行俠仗義
我重新在貼過程式碼。請往上查閱。 嘿嘿!分數不會比較多! 為了台灣的IT產業添加一份心力!(開玩笑的!我沒那麼偉大) 回題志不在答分,在於一方面加深自己的功力另一方面心得共享。互相交流經驗。 {感觸}心胸放大,不必侷限自己在一小塊地方相互競爭。 祝你早日練得絕世武功!成為大內高手中的高高手! 呵呵... ~~~~~~~~~~~ 難得聰明,常常糊塗。 ~~~~~~~~~~~
系統時間:2024-06-02 20:42:30
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!