What is the correct way to work with multiple form ?

Hello guys,

My main form will call the salesform, but it can be called several times, then, what is the correct way to work with multiple form?

procedure TFmenu.ConsultadeVendas1Click(Sender: TObject);
Var
  NewForm : TFSalesForm;
begin
  SMerc      := 'I';
  NewForm := TFSalesForm.Create(Self);
  NewForm.Show;
End

procedure TFmenu.ConsultadeVendas1Click(Sender: TObject);
Var
  NewForm : TFSalesForm;
begin
  SMerc      := 'I';
  FSalesForm:= TFSalesForm.Create(Self);
  FSalesForm.Show;
End;

Or other way...

Thanks


LVL 1
hidrauAsked:
Who is Participating?
 
slado2Connect With a Mentor Commented:
You should consider using MDI interface. Set the main form's FormStyle property to fsMDIForm and all your sales or other forms fsMDIChild. Then you can open as many forms as you want and the user will be able to work simultaneosly on them.
0
 
peterkiersConnect With a Mentor Commented:
Code for opening multiple forms:

procedure TForm.CreateMDIChild(const Name: string);
var
  Child: TMDIChild;
begin
  Child := TMDIChild.Create(Application);
  Child.Caption := Name;
end;
0
 
TheRealLokiSenior DeveloperCommented:
if you do not need to keep track of the form, then you do not even need the variable
you can just do

procedure TFmenu.ConsultadeVendas1Click(Sender: TObject);
begin
  SMerc      := 'I';
  With TFSalesForm.Create(Self) do Show;
End;
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
hidrauAuthor Commented:
Hello Guys

See the problem that I have and help me to understand it .

SaleForm := TSaleForm.Create(self);
SaleForm.Show

After call 5 times the SaleForm, when I close any windows, any reference in my windows SaleForm gives me an error, when I say refence, like this:

In the form saleForm

With SaleForm do
begin
   Top := 0;
   Width := 600  
End;

Why? How to avoid this?
0
 
slado2Connect With a Mentor Commented:
There are 2 design concepts of windows applications: SDI (Single Document Interface) form and MDI (Multiple Document Interface).
SDI means that the user has just one form open and he works only with one instance of "document" (in your case SalesForm). He may not open another SalesForm until the current one is closed <=> he can work only with one form at the time. Typical application is for example Notepad = you can edit only one document (document editor = form)
MDI means that the user open many "documents" (SalesForms) and that the forms are independent. So he may freely open another SalesForm even he has some SalesForms open already. If you know Microsoft Word than you can see that word can open many documents (=forms) at the same time and user can freely work with the document he wants. User can access the documents by Window menu (in standard application menu bar) where he can cascade, tile, minimize windows (MDIChilds) etc.

You have to choose what you want to provide to your users. If you decide for SDI then do not allow opening another SalesForm if one is already displayed.
If you want to use MDI then you will have to store your forms in ObjectList or another control to keep the track of all open forms.
0
 
hidrauAuthor Commented:
slado2, when you say MDI application , you mean that all my form will work only inside of my main form, and when I minimize it, it will be inside of my main form ?

is it?
0
 
slado2Commented:
yes. Your main form has formstyle fsMDIForm and all your child forms have fsMDIChild.
0
 
TheRealLokiConnect With a Mentor Senior DeveloperCommented:
If you want to have 5 salesforms, you should *not* use teh variable name "salesform"
otherwise, you will be overwriting it each time you create a new form
in the salesformpas code wehre you have
>With SaleForm do
>begin
>   Top := 0;
>   Width := 600  
>End;

change it to
With (self as TSalesForm) do
begin
   Top := 0;
   Width := 600  
End;
0
 
hidrauAuthor Commented:
TheRealLoki:
From my saleform sometimes I need to call other form and there some part in these forms that I make reference the saleform due to those my queries, like this example:

  sLinha     :=  Fsaleform .QConsultaEstoque_ICns_LINH.AsString;
  sProduto   := Fsaleform .QConsultaEstoque_ICns_CodProd.AsString;
  sDescricao := Fsaleform .QConsultaEstoque_ICns_Descricao_Produto.AsString;
  sCodDePara := Fsaleform .QConsultaEstoque_Icns_Codigo_DePara.AsString;

Then, I got problem here when I do this:

I called my saleform this way:

Var
  NewForm : TFsaleform ;
begin
  SMerc      := 'I';
  NewForm := TFsaleform .Create(Self);
  NewForm.Show;
End

How to work around ?
0
 
slado2Connect With a Mentor Commented:
Use ObjectList to store all your SaleForm:
var Forms: TObjectList;

...

Forms := TObjectList.Create(true);
 SMerc      := 'I';
  NewForm := TFsaleform .Create(Self);
  Forms.Add(NewForm);
  NewForm.Show;


Then use Forms to search for appropriate form.
0
 
hidrauAuthor Commented:
Must forms be declared in Public variable?
How can I call my form with this objectlist ?
I didn't understand very well
0
 
hidrauAuthor Commented:
Let me explain better what I need and what I am having problem:

Imagine a new project:

I have the main form named Form1, on my main form there is a button that call form2, this form2 can be called several times, there is a query on form2 and a button that call form3, on the form3 there is a button that can close adoquery from my form2.

Now you have the process, Imagine that I called 2 or 3 times form2 and form3 from each form2, when I click on the button on form3 that must close the adoquery in form2, it's ok, but when I try in other form3 to do the same thing, I don't have the success,

did you get it?

0
 
hidrauAuthor Commented:
I am gonna open another thread
0
 
hidrauAuthor Commented:
part of the problem was solved, then I prefered to open another thread
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.