0

I am not sure how to explain this, but here goes: I have a form with a grid with some comboboxes. From this form I create another to edit data. The edit form has also some of the comboboxes like the grid. The values in these comboboxes can be edited from a third form. If they are edited I send a broadcast like message to all open forms to update the comboboxes.

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

On each form where updates should be performed I have:

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

This is like using a shootgun when a riffle would be enough. It should be enough to send the message to the form where the editform was created from

I am not sure if it would give any performance boost but I would like to try.

My question: How can I instead of using HsBrodcastUpdate that sends to all forms just send the message to the form that created the form that sends the message.

4

3 回答 3

0

我在您的帖子中没有看到任何问题,因此这甚至可能不是您想要的。听起来像实现观察者模式可能是您正在寻找的。

您可以查看以下两篇文章作为示例,但还有更多。 http://tdelphihobbyist.blogspot.com/2009/10/observer-design-pattern-in-delphi-push.html http://sourcemaking.com/design_patterns/observer/delph

于 2012-05-04T16:17:51.337 回答
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;
于 2012-05-12T09:58:07.873 回答
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;
于 2012-05-04T14:25:55.073 回答