1

My problem is that Form2 won't show inside Form1 panel in the code below:

procedure TForm1.WebButton1Click(Sender: TObject);
var
  LResult: integer;
  newform: TForm2;

begin
  newform := TForm2.Create(Self);
  newform.Caption := 'test form';

  newform.Parent :=  WebPanel1; // this is the problem it doesn't work

  newform.Popup := false;

  // used to manage Back button handling to close subform
  window.location.hash := 'main';

  // load file HTML template + controls
  await(TForm2, newform.Load());

  // init controls values after loading
  newform.WebLabel1.caption := newform.WebLabel1.caption + ' is working!';

  try
    // excute form and wait for close
    LResult := await(TModalResult, newform.Execute);

    WebLabel1.Caption := WebLabel1.Caption + newform.ModalResult.ToString();

    ShowMessage('Form 2 is closed!');
  finally
    newform.Free;
  end;

end;

I'm trying to use VCL way to achieve this so far but the form is not displaying inside the panel!

newform.Parent :=  WebPanel1; // this is the problem it doesn't work no error messages

another approach is:

frm :=  TForm2.CreateNew(WebPanel1.ElementID, nil);

Which is working perfectly however it will not act as a modal like my code above to give back a result.

My goal is to display Form2 inside a panel in Form1 and wait for the user to close it so it can return a result to Form1 (a simple modal form way).

any way to do this please?

11
  • Have you set Visible property of your second form to True? By default when you create additional forms in Delphi their Visibility property is always set to False. So either set your second form's the Visible property to True at design-time or call newForm.Show;. But definitely don't call newForm.ShowModal when this form is shown within other form as you will block receiving any windows messages to it since they need to go through parent form message que which is currently being blocked by modality of your second form. Aug 27 at 14:33
  • Any way nesting forms within other forms can lead to some other problems so it is better to use frames for this instead. As for returning message from this "second form" to the main form there are plenty other ways you can achieve that. Main question here would be how are you waiting for the received message in your main form. Aug 27 at 14:35
  • I don't have Delphi 11 (I'm on 10.4), neither WebCore, but it seems you are missing a newform.show; after setting the forms parent. I also do not undestand why you need to call 'newform.Popup := false;' Aug 27 at 14:51
  • @SilverWarior This code LResult := await(TModalResult, newform.Execute); as mentioned above waits for the form till closes then save the return message in a LResult. also setting for Visibility doesn't affect anything here
    – Dreamer64
    Aug 27 at 17:57
  • @Tom Brunberg no need for newform.show as again `LResult := await(TModalResult, newform.Execute);' will display the form but the problem it is not inside the panel, it is covering the whole page!
    – Dreamer64
    Aug 27 at 18:02

1 Answer 1

0

Upon request I'm answering my own question with code comments, Thanks for your interest.

  public
    { Public declarations }
    newform: TWebform; // I moved it here just to reuse it anywhere
  end;    

    procedure TForm1.WebLabel1Click(Sender: TObject);
    
      procedure AfterCreate(AForm: TObject);
        begin
          { Do What you want to that form after creation }
        end;
    
    begin
      // check if it's there then clear it that is useful for multiple time use 
      if Assigned(newform) then
      begin
        newform.Close;
        newform := nil;
      end;

      // Just a double check if its cleared for testing purposes
      if newform <> nil then
         begin
           showMessage('Form Still Exsists!');
           exit;
         end;

      // the creation starts here
      newform := TForm2.CreateNew(WebPanel1.ElementID, @AfterCreate); //<-- this line did the trick!

      newform.Caption := 'test form';
      newform.Popup := false;
    
      // used to manage Back button handling to close subform
      window.location.hash := 'main';
    end;

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.