傳送標點符號至DBGrid |
答題得分者是:wameng
|
mflyy
初階會員 發表:3 回覆:44 積分:44 註冊:2002-08-13 發送簡訊給我 |
各位大大請教一個問題
我用一組標點符號的button 用於加快輸入 <textarea class="delphi" rows="10" cols="60" name="code">SendMessage(ActiveControl.Handle,EM_REPLACESEL,0,LPARAM(LPCSTR((Sender as TSpeedButton).Caption))); </textarea> 如果是用於DBEdit 或其它元件皆可,但用於DBGrid時則沒有反應不知各位有無其他的方法來處理? |
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
試試如下的方式:
procedure TForm1.SpeedButton1Click(Sender: TObject); var i: integer; dbg: TDBGrid; wc: TWinControl; begin wc := ActiveControl; if ActiveControl is TDBGrid then
begin for i := 0 to TDBGrid(ActiveControl).ControlCount - 1 do if TDBGrid(ActiveControl).Controls[i] is TInplaceEdit then begin wc := TInplaceEdit(TDBGrid(ActiveControl).Controls[i]); Break; end; end; SendMessage(wc.Handle, EM_REPLACESEL, 0, LPARAM(LPCSTR((Sender as TSpeedButton).Caption)));
end; |
mflyy
初階會員 發表:3 回覆:44 積分:44 註冊:2002-08-13 發送簡訊給我 |
|
mflyy
初階會員 發表:3 回覆:44 積分:44 註冊:2002-08-13 發送簡訊給我 |
剛剛搜尋一下 TInplaceEdit
發現版大之前的文章 http://delphi.ktop.com.tw/board.php?cid=30&fid=71&tid=67338 好像問題是類似的,不知版大最後是如何處理的,Thanks!! |
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
改成這樣吧: procedure TForm1.SpeedButton1Click(Sender: TObject); var i: integer; dbg: TDBGrid; wc: TWinControl; begin wc := ActiveControl; if ActiveControl is TDBGrid then begin dbg := TDBGrid(ActiveControl); for i := 0 to TDBGrid(ActiveControl).ControlCount - 1 do if TDBGrid(ActiveControl).Controls[i] is TInplaceEdit then begin wc := TInplaceEdit(TDBGrid(ActiveControl).Controls[i]); Break; end; end; SendMessage(wc.Handle,EM_REPLACESEL,0,LPARAM(LPCSTR((Sender as TSpeedButton).Caption))); if wc is TInplaceEdit then
begin if not (dbg.DataSource.State in [dsInsert, dsEdit]) then dbg.DataSource.DataSet.Edit; dbg.SelectedField.AsString := TInplaceEdit(wc).Text; end; end; |
mflyy
初階會員 發表:3 回覆:44 積分:44 註冊:2002-08-13 發送簡訊給我 |
感謝版大的回應,不過版大的寫法會造成 當DBGrid 不在 Edit 時 ,會無法貼上文字 這是我目前的寫法,不過還是有Bug 假設 DBGrid 有 A,B 兩欄 這個Bug就是 ,如果在同一行A Cell時先Press Button 後,再移至B Cell 在Press Button 則 A Cell 的文字內容會被Copy過來, 目前我還不了解 TInplaceEdit與DBGrid 如何來配合使用 我有Try 二種方式來處理,不過還是沒有很好的解決方法 1.是自動Post它 2.是TInplaceEdit.Clear 都不是理想的方式,不知各位有無更好的方法呢?
<textarea class="delphi" rows="10" cols="60" name="code">procedure TPurchAppF.SpeedButton1Click(Sender: TObject); begin if (ActiveControl is TDBGrid) then begin if NOT(TDBGrid(ActiveControl).SelectedField.IsValidChar(#32)) then Exit; if TDBGrid(ActiveControl).DataSource.State = dsBrowse then begin TDBGrid(ActiveControl).EditorMode:=True; TDBGrid(ActiveControl).DataSource.Edit; end; TInplaceEdit(TDBGrid(ActiveControl).Controls[0]).SelText:=(Sender as TSpeedButton).Caption; TDBGrid(ActiveControl).SelectedField.Value:=TInplaceEdit(TDBGrid(ActiveControl).Controls[0]).Text; // 1. TDBGrid(ActiveControl).DataSource.DataSet.Post; // 2. TInplaceEdit(TDBGrid(ActiveControl).Controls[0]).Clear; end else begin SendMessage(ActiveControl.Handle,EM_REPLACESEL,0,LPARAM(LPCSTR((Sender as TSpeedButton).Caption))); end; end; </textarea> |
hagar
版主 發表:143 回覆:4056 積分:4445 註冊:2002-04-14 發送簡訊給我 |
|
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
Type THackDBGrid = Class(DBGrids.TDBGrid);
procedure TForm1.SpeedButton1Click(Sender: TObject); begin DBGrid1.EditorMode := True; THackDBGrid(DBGrid1).Datalink.Edit; THackDBGrid(DBGrid1).DataLink.Modified; TInplaceEdit(DBGrid1.Controls[0]).SelText:= (Sender as TSpeedButton).Caption; end; |
mflyy
初階會員 發表:3 回覆:44 積分:44 註冊:2002-08-13 發送簡訊給我 |
To:Hagar 版大,非常謝謝你的回應
我先說明,我的意思不是說版大的程式有問題,可能我解釋的不清楚,不過這不是討論的重點 ===================引 用 hagar 文 章=================== 2.不知您說的由 A 欄位移到 B 欄位則 A 欄位的內容會被複製到 B 欄位是怎麼發生的? 是以程式由 A 欄位移到 B 欄位還是使用者按鍵盤來移? ===================引 用 hagar 文 章=================== Ans: 是用鍵盤或滑鼠 ,移至右邊欄位時,再按一次Button TInPlaceEdit 不是每次Edit 時都會重新Create ? 目前看來 確不是這樣,不知道該如何!! |
mflyy
初階會員 發表:3 回覆:44 積分:44 註冊:2002-08-13 發送簡訊給我 |
剛剛試了 wameng 版大的方法,發現之前的問題都解決了
好像一定要Create 另一個Class(DBGrids.TDBGrid); 這應該是最佳解法了,Thanks!! 只是為何會如此?,有點困惑? ===================引 用 wameng 文 章=================== Type THackDBGrid = Class(DBGrids.TDBGrid); procedure TForm1.SpeedButton1Click(Sender: TObject); begin DBGrid1.EditorMode := True; THackDBGrid(DBGrid1).Datalink.Edit; THackDBGrid(DBGrid1).DataLink.Modified; TInplaceEdit(DBGrid1.Controls[0]).SelText:= (Sender as TSpeedButton).Caption; end; |
wameng
版主 發表:31 回覆:1336 積分:1188 註冊:2004-09-16 發送簡訊給我 |
不清楚你哪裡有困惑?
Type THackDBGrid = Class(DBGrids.TDBGrid); 用意讓 DataLink 原本不公開的屬性公開出來。 TInplaceEdit 當編輯完畢後,會引發DataLink.UpdateData procedure TGridDataLink.UpdateData; begin FInUpdateData := True; try if FModified then FGrid.UpdateData; FModified := False; finally FInUpdateData := False; end; end; 這裡可以很清楚看到由於為自行更改 Text 故FModified為False 就不寫入。當切換其他欄導致原來所更改的被還原。 因此透過 THackDBGrid(DBGrid1).DataLink.Modified; 讓 FModified為True 一切引發一連串的問題,就會被解決。 ===================引 用 mflyy 文 章=================== 剛剛試了 wameng 版大的方法,發現之前的問題都解決了 好像一定要Create 另一個Class(DBGrids.TDBGrid); 這應該是最佳解法了,Thanks!! 只是為何會如此?,有點困惑? ===================引 用 wameng 文 章=================== Type THackDBGrid = Class(DBGrids.TDBGrid); procedure TForm1.SpeedButton1Click(Sender: TObject); begin DBGrid1.EditorMode := True; THackDBGrid(DBGrid1).Datalink.Edit; THackDBGrid(DBGrid1).DataLink.Modified; TInplaceEdit(DBGrid1.Controls[0]).SelText:= (Sender as TSpeedButton).Caption; end; |
mflyy
初階會員 發表:3 回覆:44 積分:44 註冊:2002-08-13 發送簡訊給我 |
wameng 版大 你一語道破我多日的疑惑
看了Source Code 才發現這個 DataLink 就是KeyPoint,難怪Help 找不到,謝了!! 另外也非常謝謝 hager 版大 ,多日來提供相當多的資訊 ,再次謝謝你!! 最後我把Source 整理一下,供有需要的人參考 type THackDBGrid = Class(DBGrids.TDBGrid); THackDBGridEh = Class(DBGridEh.TDBGridEh); procedure PutCap(Sender: TObject;wc: TWinControl); var DBGEh : TDBGridEh; DBG : TDBGrid; begin if (wc is TDBGridEh) then begin DBGEh:= TDBGridEh(wc); DBGEh.EditorMode := True; THackDBGridEh(DBGEh).Datalink.Edit; THackDBGridEh(DBGEh).DataLink.Modified; TInplaceEdit(DBGEh.Controls[0]).SelText:= (Sender as TSpeedButton).Caption; end; if (wc is TDBGrid) then begin DBG:= TDBGrid(wc); DBG.EditorMode := True; THackDBGrid(DBG).Datalink.Edit; THackDBGrid(DBG).DataLink.Modified; TInplaceEdit(DBG.Controls[0]).SelText:= (Sender as TSpeedButton).Caption; end else begin SendMessage(wc.Handle,EM_REPLACESEL,0,LPARAM(LPCSTR((Sender as TSpeedButton).Caption))); end; end; ============================================ procedure TForm1.SpeedButton1Click(Sender: TObject); begin PutCap(Sender,ActiveControl); end; ===================引 用 wameng 文 章=================== 不清楚你哪裡有困惑? Type THackDBGrid = Class(DBGrids.TDBGrid); 用意讓 DataLink 原本不公開的屬性公開出來。 TInplaceEdit 當編輯完畢後,會引發DataLink.UpdateData procedure TGridDataLink.UpdateData; begin FInUpdateData := True; try if FModified then FGrid.UpdateData; FModified := False; finally FInUpdateData := False; end; end; 這裡可以很清楚看到由於為自行更改 Text 故FModified為False 就不寫入。當切換其他欄導致原來所更改的被還原。 因此透過 THackDBGrid(DBGrid1).DataLink.Modified; 讓 FModified為True 一切引發一連串的問題,就會被解決。
編輯記錄
mflyy 重新編輯於 2007-09-11 13:36:51, 註解 無‧
|
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |