How to 'parent' a form? (Controls won't accept focus)

大憨熊 提交于 2019-11-30 09:23:58

问题


I have a child form 'frmTest' and a main form 'TfrmMain'. I set the main form as parent for frmTest like this:

unit Main;

INTERFACE
USES
  System.SysUtils, System.Classes, Vcl.Forms, Test, Vcl.StdCtrls, Vcl.Controls;

type
  TfrmMain = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
  public
  end;

IMPLEMENTATION  {$R *.dfm}

procedure TfrmMain.Button1Click(Sender: TObject);
VAR frmTest: TChildForm;
begin
  Application.CreateForm(TChildForm, frmTest);
  //frmTest:= TForm1.Create(Self);
  frmTest.Parent:= Self;
  frmTest.Show;
  frmTest.SetFocus;
end;

unit test;  { THIS IS THE CHILD }

INTERFACE

USES
  System.SysUtils,  System.Classes, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin;

TYPE
  TChildForm = class(TForm)
    Edit1: TEdit;
    SpinEdit1: TSpinEdit;
  private
  public
  end;


IMPLEMENTATION {$R *.dfm}

end.

Code as ZIP

But controls (edit box, spin edit, etc) in frmInsertImg will not accept focus from mouse but can be focused with Tab.

What am I doing wrong?


回答1:


I suggest that you set BorderStyle to bsNone for the child form. I'm not sure of the exact reasons why this works, but it has the desired effect.

If you need to add a visual frame for your child form then that is best done with explicit UI for the contained of your child form.

Forms aren't really intended to be used in this way, in my opinion. You can make things mostly work, but it's not terribly robust. Putting the UI into a frame and then hosting that should lead to better behaviour.



来源:https://stackoverflow.com/questions/42065369/how-to-parent-a-form-controls-wont-accept-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!