Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 10-31-2017, 05:19 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 254
Default Delphi5: Delphi Calling Form moves behind while executing TOpenDialog

Dear Expert,

While executing TOpenDialog in Delphi5, the calling Form moves behind. The code is as below:

HTML Code:
procedure TfrmChqRelacc.btnBrowseClick(Sender: TObject);
begin
try
        OpenDialog1.InitialDir := GetSharedDir;
        Application.ProcessMessages;
        if OpenDialog1.Execute then
        begin
                Application.BringToFront;
                EditRTM.Text := OpenDialog1.FileName;
        end;
finally
EditRTM.SetFocus;
end;
end;
Kindly suggest how shall I bring the Calling Form once TOpenDialog is executed.

Thanks.
__________________
Rmt
Reply With Quote
  #2  
Old 10-31-2017, 07:08 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 254
Default

Dear Expert,

I need your suggestion. I modified the previous code as below and it seems its working. Kindly suggest if it is ok.

HTML Code:
procedure TfrmChqRelacc.btnBrowseClick(Sender: TObject);
var
frmChqRelacc:TfrmChqRelacc;
begin
try
        frmChqRelacc:=TfrmChqRelacc.Create(nil);
        frmChqRelacc.visible:=False;
        OpenDialog1.InitialDir := GetSharedDir;
        if OpenDialog1.Execute then
        begin
                EditRTM.Text := OpenDialog1.FileName;
                frmChqRelacc.visible:=True;
        end;
finally
EditRTM.SetFocus;
freeandnil(frmChqRelacc);
end;
end;
Thanks in advance.
__________________
Rmt
Reply With Quote
  #3  
Old 10-31-2017, 08:56 AM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,218
Default

Not completely, normally you create your items before the try and free them in the finally. FreeAndNil is not needed, object.Free() is enough.

Only thing I'm curious about is what that frmChqRelacc is. It hasn't got enough time to show, just a quick flicker or something, since it's not shown Modal (assuming it's a form based on naming convention).
I'm personally not a big fan of the .visible on forms, it has .Show (or .ShowModal) and .Hide for this purpose.
And you create something that you only need when your OpenDialog is executed. Then create it at that point, not sooner
Reply With Quote
  #4  
Old 10-31-2017, 09:14 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 254
Default

yes it is a form. And I am using Visible:=true instead of showmodal since there are certain codes that get executed on Form's OnShow event. Hope it wont create any problem though.
__________________
Rmt
Reply With Quote
  #5  
Old 10-31-2017, 03:20 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,218
Default

I don't have Delphi at the moment, but normally a .ShowModal will trigger the OnShow, so that argument is invalid.

But is that .visible false/true construction a ShowModal trick? Because your form gets created and if the dialog executes it gets shown. But directly after it shows you FreeAndNil it, so therefor in my perspective it would not show at all (or just breafly as a flicker)???

If it's not a Modal form (and it is still shown) this will most likely run into a Memory Leak. This is easily solvable by adding an OnDestroy with Action := caFree in it's implementation. But then your FreeAndNil is obsolete, since it does nothing at that point.

I'm not sure which of the implementations goes by your code sample and I cannot verify at the moment, but this will hopefully give you some pointers in the right direction.

But back to my original reply, I would only create the form when it's needed, so inside the if Execute construction.
Reply With Quote
  #6  
Old 10-31-2017, 07:10 PM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 173
Default

Can you please explain again what the problem is and what you are attempting to do. At best, it seems you are attempting to execute OpenDialog1, so what issue are you having?

Also, the code you provided has many issues, so I would definitely say that it not OK.
Reply With Quote
  #7  
Old 11-01-2017, 04:52 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 254
Default

Dear Rojam,

Actually I have a Delphi5 application in which I am calling Form2 from MainForm. From Form2, I am trying to execute TOpenDialog which when gets executed and files selected and once the Open Button in TopenDialog windows is clicked, the calling Form2 moves behind. This is the main problem. But when I again open Form2 and execute TOpenDialog and select the file and clicked Open Button, this problem does not occur. The original code while calling TOpenDialog is as follows:

HTML Code:
procedure TfrmChqRelacc.btnBrowseClick(Sender: TObject);
begin
try
        OpenDialog1.InitialDir := GetSharedDir;
        Application.ProcessMessages;
        if OpenDialog1.Execute then
        begin
                //Application.BringToFront;
                EditRTM.Text := OpenDialog1.FileName;

        end;
 
finally
EditRTM.SetFocus;
end;
end;
__________________
Rmt
Reply With Quote
  #8  
Old 11-01-2017, 02:33 PM
rojam rojam is offline
Senior Member
 
Join Date: Jun 2015
Posts: 173
Default

OpenDialog has an overloaded method, not sure it is included in Delphi5, but you can give it a try.

call OpenDialog1.Execute with the Handle of the form you want to stay up front.
Code:
if OpenDialog1.Execute(Form2.Handle) then
begin
  //
end;
Reply With Quote
  #9  
Old 11-02-2017, 03:56 AM
rmt rmt is offline
Senior Member
 
Join Date: Jun 2013
Posts: 254
Default

Dear Rojam,

It is not included in Delphi5. After applying the code, I got an error "Too many actual parameters" while compiling.
__________________
Rmt
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:10 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.