Jump to content
Ian Branch

Prevent a sub form being reopened..

Recommended Posts

Hi Team,

D10.4.1, Win 10, 32bit App.

I need to open a sub form, StockPositionForm, from the MainForm, non modal as it is used in conjunction with other forms in the App.

At the same time I need to prevent StockPositionForm being reopened again from the MainForm whilst StockPositionForm is already open.

StockPositionForm  is currently opened with..

procedure TMainForm.StockPosition1Click(Sender: TObject);
begin
  //
  TStockPositionForm.Create(Self).Show;
  //
end;

Thoughts/suggestions, polite :-), appreciated.

 

Regards & TIA,

Ian

Share this post


Link to post

what a coincidence, if you look at Remy's answer, you will get the idea.

 

 

Edited by Attila Kovacs

Share this post


Link to post
Guest
6 hours ago, Ian Branch said:

At the same time I need to prevent StockPositionForm being reopened again from the MainForm whilst StockPositionForm is already open.

that way is more easy and can be used in another forms. using "Create(Application)" the application end and release it!

  • the "StockPositionForm" will be release only when app end, else, same clicking on "closse buttton" it will be "hided" - not closed!

 

image.thumb.png.f17bf7d4f88c17a0286a2146d66d5e83.png

 

FormMain:

type
  TFormMain = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormMain: TFormMain;

procedure prcShowStockPositionForm; // exported for another units!

implementation

{$R *.dfm}

uses
  uStockPositionForm,
  uFormSecond;

procedure prcShowStockPositionForm; // for easy use! but you can put it in a unit generic for general use!!!
begin
  // use same var "StockPositionForm : TStockPositionForm" definided on "uStockPositionForm.pas"
  //
  // always verifying if "it" was not created... then, create it!
  if (StockPositionForm = nil) then
    StockPositionForm := TStockPositionForm.Create(Application);
  //
  // if it is not showing... (if hide for example)
  if not StockPositionForm.Showing then
    StockPositionForm.Show
  else
    StockPositionForm.BringToFront;
end;

procedure TFormMain.Button1Click(Sender: TObject);
begin
  prcShowStockPositionForm;
end;

procedure TFormMain.Button2Click(Sender: TObject);
begin
  FormSecond := TFormSecond.Create(nil);
  try
    FormSecond.ShowModal;
  finally
    FreeAndNil(FormSecond);
  end;
end;

procedure TFormMain.FormCreate(Sender: TObject);
begin
  Self.Position := TPosition.poScreenCenter;
end;

initialization

ReportMemoryLeaksOnShutdown := true;

finalization

end.

 

FormSecond

type
  TFormSecond = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FormSecond: TFormSecond;

implementation

{$R *.dfm}

uses
  uFormMain; // necessary to use "prcShowStockPositionForm;" but you can put this procedure in a unit for general use (not in FormMain)

procedure TFormSecond.Button1Click(Sender: TObject);
begin
  prcShowStockPositionForm;
end;

procedure TFormSecond.FormCreate(Sender: TObject);
begin
  Self.Height   := 200;
  Self.Width    := 600;
  Self.Position := TPosition.poScreenCenter;
end;

end.

 

StockPositionForm

type
  TStockPositionForm = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  StockPositionForm: TStockPositionForm;

implementation

{$R *.dfm}

uses
  uFormMain; // not necessary! here only because below: FormMain.Left ... and .Top

procedure TStockPositionForm.FormCreate(Sender: TObject);
begin
  Self.Left := FormMain.Left + FormMain.Width + 50;
  Self.Top  := FormMain.Top;
end;

end.

 

hug

Edited by Guest

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×