- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


Technical Information Document (TI3336)

Surfacing an Event on an ActiveForm to a Container

  • Product: Delphi 3
  • Version: All
  • Platform: Windows 95, Windows NT Q: How do I surface an event on an ActiveForm to a container?
    A: Here's a demo on how to do this:

    Let's create an ActiveForm with a button on it. Now we want to surface the OnClick event of this button to any container using our ActiveForm. What we need to do is this:

    Start a brand new ActiveForm project, by selecting File | New... | ActiveX | ActiveForm. Change the ActiveX Name, Implementation Unit, and Project Name if you want. For instance, TestX, TestImpl.pas, and Test.dpr respectivelly.

    Drop a TButton on the TestX form. Double click on the button and make the event handle say:

      procedure TTestX.Button1Click(Sender: TObject);
      var
        OldCaption : String;
      begin
        with Button1 do begin
          OldCaption := Caption;
          Caption := 'Auch!';
          Sleep(1000);
          Caption := OldCaption;
        end;
      end;
    This is just to demonstrate that we can handle the button's click event in both the ActiveForm and in the container. What we just implemented was of course the ActiveForm side. Now, let's take care of the container side...

    Do a View | Type Library. In ITestXEvents we need to add a new event just below OnPaint. Let's call it OnButton1Click. When you hit Refresh you should see that the ID (dispid) of this event is set to 9.

    Now go back to the OnClick event handler of Button1 on the ActiveForm. Add one line to make it look like this:

    procedure TTestX.Button1Click(Sender: TObject);
    var
      OldCaption : String;
    begin
      with Button1 do begin
        OldCaption := Caption;
        Caption := 'Auch!';
        Sleep(1000);
        Caption := OldCaption;
      end;
      if FEvents <> nil then FEvents.OnButton1Click;
    end;
    This will make the container see things after you've handled the OnClick event on the ActiveForm side. If you want to let the container do its thing first, just put the line at the top instead.

    Save the ActiveForm project somewhere on disk, e.g. in C:\Test.

    Do a Project | Build All.

    Do a Run | Register ActiveX Server.

    Do a Component | Import ActiveX Control. Find Test Library in the list of registered controls. Select it, and hit Install. Pu it in dclusr30.dpk (the default). After hitting OK a couple of times the control should pop up on your ActiveX tab on the component palette. Close the package editor and save the changes to the package.

    Do a File | New Application. Drop down a TestX (our ActiveForm) on the form. Double click on the OnButton1Click event handler on the Events tab in the Object Inspector and make it say:

      procedure TForm1.TestX1Button1Click(Sender: TObject);
      begin
        ShowMessage('You hit me!');
      end;
    Run the application, and hit Button1 in the TestX component. You should see the Caption change for a second and then you should see your 'You hit me!' message popping up on the screen.



    * #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



    - - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
    PFPhotoFile