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

有關 DBGrid 的問題 (Enter & Tab)

答題得分者是:timhuang
懷舊的人
高階會員


發表:28
回覆:152
積分:141
註冊:2003-01-08

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-06-22 12:17:09 IP:152.104.xxx.xxx 未訂閱
引言: 發問之前我有找到一篇相關的文章,回應的人很多,但是和我的問題還是有些差異。 http://delphi.ktop.com.tw/topic.php?TOPIC_ID=27456 因此我再提出來討論 目的:在 DBGrid 使用 Enter 就如同使用 Tab 方法(一):1. 將 Form 的 Property KeyPreview 設成 True 2. 在 Form 的 OnKeyPress 寫下 // 按 Enter 鍵如同按 Tab 鍵 if Key = #13 then if ActiveControl is TDBGrid then with TDBGrid(ActiveControl) do begin if SelectedIndex < (FieldCount-1) then SelectedIndex := SelectedIndex 1 else SelectedIndex := 0; end else begin Key := #0; Perform(WM_NEXTDLGCTL,0,0); end; 結果:確實可以達到如 Tab 的功能 變更:DBGrid 中的某幾個 Column 因為不同的條件 而必須將 Visible 設成 True or False, 當設成 False 後, 使用 Tab 還是正常, 但 ENTER 就出現問題了。因此將方法(一)改為方法(二) 方法(二):1. 將 Form 的 Property KeyPreview 設成 True 2. 在 Form 的 OnKeyPress 寫下 // 按 Enter 鍵如同按 Tab 鍵 if Key = #13 then if ActiveControl is TDBGrid then with TDBGrid(ActiveControl) do begin repeat SelectedIndex := SelectedIndex 1; until (SelectedIndex = Columns.Count-1) or Columns[SelectedIndex].Visible; if SelectedIndex = Columns.Count-1 then begin DataSource.DataSet.Next; if DataSource.DataSet.Eof and (not ReadOnly) then DataSource.DataSet.Append; SelectedIndex := 0; // or SendMessage(ActiveControl.Handle, WM_HSCROLL, SB_TOP, 0); end; end else begin Key := #0; Perform(WM_NEXTDLGCTL,0,0); end; 結果:不管 Column Visible 是否設成 False , Enter 也確實可以達到如 Tab 的功能 討論:1. 當 Column Visible 被設成 False 時 , Tab 可以正確操作無誤,Tab 應該是依 Tab Order 的順序來移動, 難到 Column Visible 設成 False 時 Table Order 有被變更了嗎 ? 在 DBGrid 的 Columns 有 Table Order 這樣的屬性嗎? 2. 難到沒有方法當按 Enter 鍵時就送出 Tab 鍵 ?
timhuang
尊榮會員


發表:78
回覆:1815
積分:1608
註冊:2002-07-15

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-06-22 18:11:17 IP:61.62.xxx.xxx 未訂閱
用這招如何:    
procedure TForm1.FormKeyPress(Sender: TObject; var Key: Char);
begin
  if (Key = #13) and (ActiveControl is TDBGrid) then
  begin
    Key := #0;
    PostMessage(DBGrid1.Handle, WM_KEYDOWN, VK_TAB, 0);
  end;
end;
timhuang
尊榮會員


發表:78
回覆:1815
積分:1608
註冊:2002-07-15

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-06-22 18:16:10 IP:61.62.xxx.xxx 未訂閱
其中紅字的部分也可以使用這個來代替, 是一樣的:    keybd_event(VK_TAB, 0, 0, 0);
懷舊的人
高階會員


發表:28
回覆:152
積分:141
註冊:2003-01-08

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-06-22 18:51:05 IP:152.104.xxx.xxx 未訂閱
1. 這方法確實不錯,這樣就不用考慮 Columns Visible 是否 True or False 2. 但是原來其他 Enter Key 有效的地方仍須保留,所以把它改為 if Key = #13 then begin Key := #0; if ActiveControl is TCustomGrid then PostMessage(ActiveControl.Handle, WM_KEYDOWN, VK_TAB, 0) else Perform(WM_NEXTDLGCTL,0,0); end; 3. 再次感謝指導 發表人 - 懷舊的人 於 2003/06/22 18:52:44
系統時間:2024-04-29 23:11:54
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!