Solved

Create Open Dialog

Posted on 2007-11-16
8
289 Views
Last Modified: 2010-04-04
When I try to create a TOpenDialog I get an EAccessViolation Error. Here is my code where the error occurs:

var
  OpenDlg: TOpenDialog;
  filename: string;

procedure TForm1.FormCreate(Sender: TObject);
begin
  OpenDlg.Create(OpenDlg); //Access Violation
  if OpenDlg.Execute then
  begin
    filename := OpenDlg.FileName;
  end;

Could anyone tell me how to fix this?
0
Comment
Question by:ding-dong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 19

Accepted Solution

by:
MerijnB earned 125 total points
ID: 20300348
procedure TForm1.FormCreate(Sender: TObject);
begin
  OpenDlg := TOpenDialog.Create(OpenDlg); //Access Violation gone
  try
    if OpenDlg.Execute then
    begin
      filename := OpenDlg.FileName;
    end;
  finally
    OpenDlg.Free();
  end;
end;
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20300369
Try this...

procedure TForm1.Button1Click(Sender: TObject);
begin
  OpenDlg := TOpenDialog.Create(self); //Access Violation
  if OpenDlg.Execute then
  begin
    Edit1.Text := OpenDlg.FileName;
  end;  
end;

John
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20300373
Woops... remove the comment //Access violation as it is not there.

John
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 19

Expert Comment

by:MerijnB
ID: 20300379
always refresh before you post a comment :)
0
 
LVL 18

Expert Comment

by:Johnjces
ID: 20300411
MerijnB

WoopS! Alright I see the problem!!!! In just before me!

Sorry about that! I see that happen every so often... first time for me!

;-) John

0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 20301264
The following code:

  OpenDlg:=TOpenDialog.Create(OpenDlg);

Is going to be prone to problems, and should not be used. If OpenDlg is not cleared (left pointing to an already freed object; aka stale pointer), then an access violation WILL occur. Not a problem for the first time use when the var is global in scope, but the second time is an issue. This is also an issue if the var is local in scope (not guaranteed to be nil). In short, poor practice.

If you don't need an owner then use var:=TOpenDialog.Create(nil), or as John pointed out, use Self, which in this case refers to the object who's method your calling (TForm1).

Russell


0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20301279
oops, sorry, that was a very big typo, should of course be: OpenDlg := TOpenDialog.Create(self);
0
 
LVL 19

Expert Comment

by:MerijnB
ID: 20301280
(glad you are paying attention :))
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question