Modal windows..

I want to show a form with which the user is to type in some details and click a save button, like a dialog box. I need this form modal towards its parent form (which btw. is a TFrame). The TForm.ShowModal function disables ALL of the windows in the entire application, but I only want to disable the parent, as told. I also need the parentform (frame) to stop processing any further code until the dialog box is closed.

How can this be done?
LVL 1
sagerydAsked:
Who is Participating?
 
AvonWyssCommented:
sageryd, as explained beforer, code executeion CANNOT and MUST NOT be halted. However, the form will be locked until the other form has been closed. make a OnClose function for the "child" form which does this:

procedure TMyForm.FormClose(Sender: TObject; var Action: TCloseAction);
    MyDataSet.FieldByName('whatever').AsString := Edit1.Text;
    Action:=caFree;
end;

After this code is executed, the parent form will "unlock" itself.
0
 
AvonWyssCommented:
You could add a window message handler to the parent form which ignores (or beeps) on any message when a certain condition (e.g. client window is open) is true.
0
 
sagerydAuthor Commented:
But I need it to halt code execution until the client window has closed.
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
AvonWyssCommented:
You cannot halt code execution and at the same time have only the parent window being locked. However, if you make sure that the parent window will not process messages, it will pretty much behave as if it was "dead" and not execute and OnClick etc. code.
0
 
sagerydAuthor Commented:
My code looks like:

var
  MyForm: TMyForm;
begin
  MyForm := TMyForm.Create(nil);
  MyForm.Edit1.Text := MyDataSet.FieldByName('whatever').AsString;
[...]
  MyForm.Show; //(modal)

  //after close:
  MyDataSet.Edit;
  MyDataSet.FieldByName('whatever').AsString := MyForm.Edit1.Text;
  MyDataSet.Post;
end;
0
 
oraelbisCommented:
It seems that you can't access to MyForm.Edit1.Text after MyForm closing. If it is, try this code:


type
  TMyForm = class(TForm)
    ...
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    whateverText:String;
  end;

var
  MyForm: TMyForm;

...
try
  MyForm := TMyForm.Create(nil);
  MyForm.Edit1.Text := MyDataSet.FieldByName('whatever').AsString;
  if MyForm.ShowModal=mrOk then
     MyDataSet.FieldByName('whatever').AsString:=MyForm.whateverText;
finally
  MyForm.Free;
end;

procedure TMyForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Self.whateverText:=MyForm.Edit1.Text;
end;
0
 
sagerydAuthor Commented:
Please read the question before answering :)  That was not the problem.
0
 
sagerydAuthor Commented:
Of course I would put a FreeAndNil(MyForm) after the DataSet posting, but I thought it was irrelevant to this question.
0
 
sagerydAuthor Commented:
And, oraelbis,

try
 MyForm := TMyForm.Create(nil);
  MyForm.SHow;
etc..
finally
  MyForm.Free;

...is incorrect as if the form would fail to create the form in the first place, it would try to free itself and thus generate an error. Just a little hint. No offense.. I bet it was just a typing mistake. :)

Cheers!
0
 
sagerydAuthor Commented:
Sorry for that word error. "form" doesn't create, "it" does. Nevermind. Give me an answer..! *dying*
0
 
AvonWyssCommented:
I thought of something like this (Form1 is the pseudo-modal form):

     TForm1 = class(TForm)
     private
          FLockingForm: TForm;
          FLockingCloseEvent: TCloseEvent;
          procedure ShowClientAndLock(AForm: TForm);
          procedure LockingCloseNotify(Sender: TObject; var Action: TCloseAction);
     protected
          procedure WndProc(var Message: TMessage); override;


var
     MyForm: TMyForm;
begin
     MyForm := TMyForm.Create(nil);
     MyForm.Edit1.Text := MyDataSet.FieldByName('whatever').AsString;
[...]
     ShowClientAndLock(MyForm);
end;

procedure TForm1.ShowClientAndLock(AForm: TForm);
begin
     Assert(not Assigned(FLockingForm)); //only one lock allowed
     Assert(Assigned(AForm)); //must not be NIL
     FLockingForm:=AForm;
     FLockingCloseEvent:=FLockingForm.OnClose;
     FLockingForm.OnClose:=LockingCloseNotify;
end;

procedure TForm1.LockingCloseNotify(Sender: TObject; var Action: TCloseAction);
begin
     Assert(Sender=FLockingForm);
     if Assigned(FLockingCloseEvent) then
          FLockingCloseEvent(Sender,Action);
     if Action in [caHide,caFree] then begin
          FLockingForm.OnClose:=FLockingCloseEvent;
          FLockingForm:=nil;
     end;
end;

procedure TForm1.WndProc(var Message: TMessage);
begin
     if Assigned(FLockingForm) then
          if Message.Msg=WM_PAINT then
               inherited
          else if Message.Msg=WM_SETFOCUS then begin
               MessageBeep(0);
               FLockingForm.SetFocus;
          end;
end;


When the client window is successfully closed (I'd suggest to use an OnClose event and to set the Action variable to caFree for automatic freeing of the window), the "parent" will become active again.
0
 
sagerydAuthor Commented:
Cool. I'll try that, tomorrow.
0
 
kretzschmarCommented:
why not simple disable the tframe?
0
 
sagerydAuthor Commented:
Because I need to halt code execution.
0
 
sagerydAuthor Commented:
Avon, your code doesn't halt the code execution in the parent form, does it?


var
    MyForm: TMyForm;
begin
    MyForm := TMyForm.Create(nil);
    MyForm.Edit1.Text := MyDataSet.FieldByName('whatever').AsString;
[...]
    ShowClientAndLock(MyForm);
  MyDataSet.FieldByName('whatever').AsString := MyForm.Edit1.Text;
  FreeAndNil(MyForm);
end;


...wouldn't work would it?
0
 
MoondancerCommented:
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.11756739.html
http://www.experts-exchange.com/questions/Q.20012038.html
http://www.experts-exchange.com/questions/Q.20015074.html
http://www.experts-exchange.com/questions/Q.20071728.html
http://www.experts-exchange.com/questions/Q.20075938.html
http://www.experts-exchange.com/questions/Q.20086694.html
http://www.experts-exchange.com/questions/Q.20091498.html
http://www.experts-exchange.com/questions/Q.20110041.html
http://www.experts-exchange.com/questions/Q.20114710.html
http://www.experts-exchange.com/questions/Q.20127095.html
http://www.experts-exchange.com/questions/Q.11360917.html
http://www.experts-exchange.com/questions/Q.20136586.html
http://www.experts-exchange.com/questions/Q.20161465.html
http://www.experts-exchange.com/questions/Q.20170435.html
http://www.experts-exchange.com/questions/Q.20177789.html
http://www.experts-exchange.com/questions/Q.20251794.html
http://www.experts-exchange.com/questions/Q.20197428.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20090412.html
http://www.experts-exchange.com/questions/Q.20109565.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
0
 
AvonWyssCommented:
I think I provided a suitable answer to the question asked.
0
 
sagerydAuthor Commented:
Sorry. Forgot all about this questions existance.

Happy easter
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.