2012-05-04 51 views
0

我不知道如何解釋這一點,但這裏有: 我有一個窗體與一些組合框的網格。從這個表格我創建另一個編輯數據。編輯表單也有一些組合框,如網格。 這些組合框中的值可以從第三種形式進行編輯。 如果編輯它們,我會向所有打開的表單發送類似廣播的消息來更新組合框。避免消息廣播

procedure HsBrodcastUpdate; 
var 
    i: integer; 
begin 
    for i := 1 to Screen.FormCount - 1 do 
    SendMessage(Screen.Forms[i].Handle, WM_FORMUPDATES, 0, 0); 
end; 

在哪裏更新應執行我有各種形式:

procedure FormUpdate(var aMessage: TMessage); message WM_FORMUPDATES; 

這就像使用shootgun當淺灘就足夠了。 它應該足以將消息發送到創建編輯窗體的窗體

我不確定它是否會提高性能,但我想嘗試。

我的問題:我怎麼能代替使用發送到所有窗體的HsBrodcastUpdate發送消息到創建發送消息的窗體的窗體。

+1

你無法想象有多少消息是每秒發送一個工作的Windows系統上:)。消除你的消息將不會影響性能。 – kludg

+1

在編輯表單中使用類似'OnEditionDone'的事件並以'owner'形式創建事件處理程序。或者只發送/發佈消息給所有者表單。 – teran

+1

..或致電業主控制執行()方法 –

回答

0

我會用一個類的方法來完成這項

Form1 = class(TForm1) 
    ... 
    private 
    ... 
    public 
    Class procedure UpdateComboBox; 
    ... 
    end; 

那麼程序看起來像這樣

class procudure TForm1.UpdateComboBox; 
    var 
    F: TForm2;//This is the target form 
    I: Integer; 
    begin 
    F := nil; 
    for i := Screen.FormCount - 1 DownTo 0 do 
     if (Screen.Forms[i].Name = '<The Form Name Here>') then 
     F := Screen.Forms[I] As TForm2; 
    if F <> nil then 
    begin 
     //update your form's F.comboBox here 
    end; 
    end; 
0

您可以構建一連串的事件,在您的形式暴露事件​​處理程序。 研究這個例子:

{- Main Form } 

Type 

TFormMain = Class(TForm) 
    private 
    procedure UpdateCombo(Sender : TObject); 
    procedure InvokeOtherForm; 
end; 

TFormMain.InvokeOtherForm; 
var 
    OtherForm : TOtherForm; 
begin 
    OtherForm := TOtherForm.Create(Nil); 
    try 
    OtherForm.OnUpdateCombo := Self.UpdateCombo; // Link update event ! 
    OtherForm.ShowModal; 
    finally 
    OtherForm.Free; 
    end; 
end; 

TFormMain.UpdateCombo(Sender : TObject); 
begin 
    {- Update the combos ... } 
    ... 
end; 

{- OtherForm } 
Type 

TOtherForm = Class(TForm) 
    private 
    FOnUpdateCombo : TNotifyEvent; 
    procedure InvokeThirdForm; 
    procedure UpdateCombo(Sender : TObject); 
    public 
    OnUpdateCombo : TNotifyEvent read FOnUpdateCombo write FOnUpdateCombo; 
    end; 

TOtherForm.InvokeThirdForm; 
var 
    ThirdForm : TThirdForm; 
begin 
    ThirdForm := TThirdForm.Create(Nil); 
    try 
    ThirdForm.OnUpdateCombo := Self.UpdateCombo; // Link update event ! 
    ThirdForm.ShowModal; 
    finally 
    ThirdForm.Free; 
    end; 
end; 

TOtherForm.UpdateCombo(Sender : TObject); 
begin 
    {- Do some internal updates } 
    ... 
    {- Pass on update information event } 
    if Assigned(FOnUpdateCombo) then 
    FOnUpdateCombo(Sender); 
end; 

{- ThirdForm } 
Type 

TThirdForm = Class(TForm) 
    private 
    FOnUpdateCombo : TNotifyEvent; 
    procedure UpdateCombo; 
    public 
    OnUpdateCombo : TNotifyEvent read FOnUpdateCombo write FOnUpdateCombo; 
    end; 

TThirdForm.UpdateCombo; 
begin 
    {- Do some internal updates } 
    ... 
    {- Pass on update information event } 
    if Assigned(FOnUpdateCombo) then 
    FOnUpdateCombo(Self); 
end;