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

自定義元件datasource的notification

尚未結案
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2015-11-10 10:45:26 IP:211.75.xxx.xxx 未訂閱
大家好,

我在自定義元件的Notification處理一段, 當DataSource屬性值移掉時, 會出ShowMessage。
結果當我把元件放在表單上DataSource沒有設置, 在執行時滑鼠在DBNavigator處移動, 就會觸發該notification, 而秀出'Data source was already nil'
因為我datasource沒有設值, 這時候的notification並不需要去處理這個訊息, 請問有經驗的前輩, 如何在判斷式裡加條件避開ShowMessage ('Data source was already nil')?

謝謝
[code delphi]
procedure TMdDbProgress.Notification (AComponent: TComponent;
Operation: Toperation);
begin
inherited Notification (AComponent, Operation);
if (Operation = opRemove) and (FDataLink <> nil) and
(AComponent = FDataLink.DataSource) then
begin
FDataLink.DataSource := nil;
ShowMessage ('Data source set to nil');
end
else if (Operation = opRemove) and (FDataLink <> nil) and
(FDataLink.DataSource = nil) then
begin
ShowMessage ('Data source was already nil');
end;
end;
[/code]

附件是測試小程式, 可以看到問題現象。
leveon
資深會員


發表:30
回覆:389
積分:303
註冊:2012-02-12

發送簡訊給我
#2 引用回覆 回覆 發表時間:2015-11-10 14:50:45 IP:1.163.xxx.xxx 訂閱
看不懂你最終想要的目的?

你把程式放在 Notification
又依你寫的程式碼邏輯來看 'Data source was already nil' 肯定是狂跳

如果你只是想處理元件的某個屬性發生改變 應該用這樣的寫法

http://stackoverflow.com/questions/9945989/supervise-changes-in-object-property-values


===================引 用 pedro 文 章===================
大家好,

我在自定義元件的Notification處理一段, 當DataSource屬性值移掉時, 會出ShowMessage。
結果當我把元件放在表單上DataSource沒有設置, 在執行時滑鼠在DBNavigator處移動, 就會觸發該notification, 而秀出'Data source was already nil'
因為我datasource沒有設值, 這時候的notification並不需要去處理這個訊息, 請問有經驗的前輩, 如何在判斷式裡加條件避開ShowMessage ('Data source was already nil')?

謝謝
[code delphi]
procedure TMdDbProgress.Notification (AComponent: TComponent;
Operation: Toperation);
begin
inherited Notification (AComponent, Operation);
if (Operation = opRemove) and (FDataLink <> nil) and
(AComponent = FDataLink.DataSource) then
begin
FDataLink.DataSource := nil;
ShowMessage ('Data source set to nil');
end
else if (Operation = opRemove) and (FDataLink <> nil) and
(FDataLink.DataSource = nil) then
begin
ShowMessage ('Data source was already nil');
end;
end;
[/code]

附件是測試小程式, 可以看到問題現象。
pedro
尊榮會員


發表:152
回覆:1187
積分:892
註冊:2002-06-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2015-11-10 17:35:43 IP:211.75.xxx.xxx 未訂閱
leveron大

謝謝你的回應
不明白為什麼在DBNavigator移動滑鼠, 自定元件Notification 就會收到FDataLink.DataSource為nil的訊息?
是不是delphi會對表單上所有資料感知元件都送這個訊息?

===================引 用 leveon 文 章===================
看不懂你最終想要的目的?

你把程式放在 Notification
又依你寫的程式碼邏輯來看 'Data source was already nil' 肯定是狂跳

如果你只是想處理元件的某個屬性發生改變 應該用這樣的寫法

http://stackoverflow.com/questions/9945989/supervise-changes-in-object-property-values


===================引 用 pedro 文 章===================
大家好,

我在自定義元件的Notification處理一段, 當DataSource屬性值移掉時, 會出ShowMessage。
結果當我把元件放在表單上DataSource沒有設置, 在執行時滑鼠在DBNavigator處移動, 就會觸發該notification, 而秀出'Data source was already nil'
因為我datasource沒有設值, 這時候的notification並不需要去處理這個訊息, 請問有經驗的前輩, 如何在判斷式裡加條件避開ShowMessage ('Data source was already nil')?

謝謝
[code delphi]
procedure TMdDbProgress.Notification (AComponent: TComponent;
Operation: Toperation);
begin
inherited Notification (AComponent, Operation);
if (Operation = opRemove) and (FDataLink <> nil) and
(AComponent = FDataLink.DataSource) then
begin
FDataLink.DataSource := nil;
ShowMessage ('Data source set to nil');
end
else if (Operation = opRemove) and (FDataLink <> nil) and
(FDataLink.DataSource = nil) then
begin
ShowMessage ('Data source was already nil');
end;
end;
[/code]

附件是測試小程式, 可以看到問題現象。
GrandRURU
站務副站長


發表:240
回覆:1680
積分:1874
註冊:2005-06-21

發送簡訊給我
#4 引用回覆 回覆 發表時間:2015-11-10 17:47:26 IP:59.120.xxx.xxx 未訂閱
把順序對調一下就可以了吧?

===================引 用 pedro 文 章===================
大家好,

我在自定義元件的Notification處理一段, 當DataSource屬性值移掉時, 會出ShowMessage。
結果當我把元件放在表單上DataSource沒有設置, 在執行時滑鼠在DBNavigator處移動, 就會觸發該notification, 而秀出'Data source was already nil'
因為我datasource沒有設值, 這時候的notification並不需要去處理這個訊息, 請問有經驗的前輩, 如何在判斷式裡加條件避開ShowMessage ('Data source was already nil')?

謝謝
[code delphi]
procedure TMdDbProgress.Notification (AComponent: TComponent;
Operation: Toperation);
begin
inherited Notification (AComponent, Operation);
if (Operation = opRemove) and (FDataLink <> nil) and
(AComponent = FDataLink.DataSource) then
begin
FDataLink.DataSource := nil;
ShowMessage ('Data source set to nil');
end
else if (Operation = opRemove) and (FDataLink <> nil) and
(FDataLink.DataSource = nil) then
begin
ShowMessage ('Data source was already nil');
end;
end;
[/code]

附件是測試小程式, 可以看到問題現象。
leveon
資深會員


發表:30
回覆:389
積分:303
註冊:2012-02-12

發送簡訊給我
#5 引用回覆 回覆 發表時間:2015-11-10 21:24:02 IP:220.132.xxx.xxx 訂閱
Notification  是VCL裡面的一個通知機制  

你可以在Form 裡 呼叫
Self.Notification(Self,opRemove); 'Data source was already nil' 應該也是會狂跳


你可以去追追
TComponent.Notification 中的Source 
procedure TComponent.Notification(AComponent: TComponent;
  Operation: TOperation);
var
  I: Integer;
begin        for I := 0 to FComponents.Count - 1 do
      TComponent(FComponents[I]).Notification(AComponent, Operation);
end;    這篇有 Notification  的介紹
http://mc-computing.com/languages/delphi/ComponentNotification.html

===================引 用 pedro 文 章===================
leveron大

謝謝你的回應
不明白為什麼在DBNavigator移動滑鼠, 自定元件Notification 就會收到FDataLink.DataSource為nil的訊息?
是不是delphi會對表單上所有資料感知元件都送這個訊息?

===================引 用 leveon 文 章===================
看不懂你最終想要的目的?

你把程式放在 Notification
又依你寫的程式碼邏輯來看 'Data source was already nil' 肯定是狂跳

如果你只是想處理元件的某個屬性發生改變 應該用這樣的寫法

http://stackoverflow.com/questions/9945989/supervise-changes-in-object-property-values


===================引 用 pedro 文 章===================
大家好,

我在自定義元件的Notification處理一段, 當DataSource屬性值移掉時, 會出ShowMessage。
結果當我把元件放在表單上DataSource沒有設置, 在執行時滑鼠在DBNavigator處移動, 就會觸發該notification, 而秀出'Data source was already nil'
因為我datasource沒有設值, 這時候的notification並不需要去處理這個訊息, 請問有經驗的前輩, 如何在判斷式裡加條件避開ShowMessage ('Data source was already nil')?

謝謝
[code delphi]
procedure TMdDbProgress.Notification (AComponent: TComponent;
Operation: Toperation);
begin
inherited Notification (AComponent, Operation);
if (Operation = opRemove) and (FDataLink <> nil) and
(AComponent = FDataLink.DataSource) then
begin
FDataLink.DataSource := nil;
ShowMessage ('Data source set to nil');
end
else if (Operation = opRemove) and (FDataLink <> nil) and
(FDataLink.DataSource = nil) then
begin
ShowMessage ('Data source was already nil');
end;
end;
[/code]

附件是測試小程式, 可以看到問題現象。
系統時間:2024-04-20 13:43:58
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!