Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error on cancel in Dialogs (Delphi 7) 2

Status
Not open for further replies.

ug505

Programmer
Jan 2, 2010
52
US
There's gotta be something I'm missing but I don't know what! It works fine when opening files but if you ever hit cancel it will show a error. What is wrong with this code?

Code:
OpenDialog1.Execute;
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
Form1.Caption:=OpenDialog1.FileName;
Run1.Enabled:=True;
 
OpenDialog1.Execute is a function which returns a boolean value indicating whether a file was selected or not.

Code:
if OpenDialog1.Execute then
  bla bla bla

I'm waiting for the white paper entitled "Finding Employment in the Era of Occupational Irrelevancy
 
The Execute method of TOpenDialog returns a boolean value. If the cancel button was pressed, false is returned and there is no filename, therefore the TMemo has no file to open. I am confident the TMemo is raising the exception (error) with which you are dealing. All the code following the "OpenDialog1.Execute;" statement needs to be contained in an IF statement.

Code:
if OpenDialog1.Execute then
Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
Form1.Caption:=OpenDialog1.FileName;
Run1.Enabled:=True;

Steve.
 
Steve - minor omission...
Code:
  if OpenDialog1.Execute then begin
    Memo1.Lines.LoadFromFile(OpenDialog1.FileName);
    Form1.Caption:=OpenDialog1.FileName;
    Run1.Enabled:=True;
  end;
:)

Roo
Delphi Rules!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top