problem with messagebox

Hi experts,

With the help of a entry form I give the user the possibility to input some data. After editing is finished, the
user is prompted for a filename where the input data will be stored. After this the user is asked, if he wants to
create another input file.
If he answers with "yes", the entry form is displayed again on the screen, and the input szenario starts again.
As soon as the question is answered with "no", the specified filenames are passed to notepad.exe as command line
parameters.

My problem is that I have difficulties to seperate the prompting for a filename
from launching notepad.exe.

What I want is a program like this

save file nr. 1
save file nr. 2
save file nr. 3

load file nr. 1
load file nr. 2
load file nr. 3


But in my source it happens this:


save file nr. 1
load file nr. 1
save file nr. 2
load file nr. 2
save file nr. 3
load file nr. 3

Although I separated the part with the messagebox and the part with the for loop these parts are executed
not separately. Can you please tell me what I am doing wrong here?


Here is my code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs,  StdCtrls,ShellApi;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    SaveDialog1: TSaveDialog;
   
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  f: textfile;
  sl: tstringlist;
  i: word;
begin
sl := tstringlist.create;
  if savedialog1.execute then
  begin
    assignfile(f, savedialog1.filename);
    rewrite(f);
    writeln(f, edit1.text);
    writeln(f, edit2.text);
    closefile(f);
    ShowMessage('Your data were saved to a file.');
    sl.add(savedialog1.filename);
  end;
  if (messagebox(0, 'Create one more text file ?', 'Question', 36) <> 6) = true
    then form1.close;
  for i := 0 to sl.count - 1 do
  begin
    ShellExecute(Self.Handle, 'open', PChar('C:\Windows\Notepad.exe '), PChar(sl.strings[i]), nil,
      SW_NORMAL);
  end;
  sl.free;
end;

end.


With kind regards

Mathes
mathesAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
kretzschmarConnect With a Mentor Commented:
Hi Mathes,

i will favorit this solution, if this not solve your problem then reject my answer,

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs,  StdCtrls,ShellApi;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    SaveDialog1: TSaveDialog;
    
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

var sl : tstringlist;

procedure TForm1.Show(Sender: TObject);
begin
  sl := tstringlist.create;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  f: textfile;
  i: word;
begin
  if savedialog1.execute then
  begin
    assignfile(f, savedialog1.filename);
    rewrite(f);
    writeln(f, edit1.text);
    writeln(f, edit2.text);
    closefile(f);
    ShowMessage('Your data were saved to a file.');
    sl.add(savedialog1.filename);
  end;
  if (messagebox(0, 'Create one more text file ?', 'Question', 36) <> 6) = true
    then
    form1.close;
end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    for i := 0 to sl.count - 1 do
    begin
      ShellExecute(Self.Handle, 'open', PChar('C:\Windows\Notepad.exe '), PChar(sl.strings[i]), nil,
      SW_NORMAL);
    sl.free;
end;
end.

meikl
0
 
kretzschmarCommented:
Hi mathes,

try this litlle changes



var sl : tstringlist;

procedure TForm1.Show(Sender: TObject);
begin
  sl := tstringlist.create;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  f: textfile;
  {sl: tstringlist;  -> should moved to global of the form }
  i: word;
begin
{ sl := tstringlist.create;  -> should be moved to formshow}
  if savedialog1.execute then
  begin
    assignfile(f, savedialog1.filename);
    rewrite(f);
    writeln(f, edit1.text);
    writeln(f, edit2.text);
    closefile(f);
    ShowMessage('Your data were saved to a file.');
    sl.add(savedialog1.filename);
  end;
  if (messagebox(0, 'Create one more text file ?', 'Question', 36) <> 6) = true
    then
  begin
    for i := 0 to sl.count - 1 do
    begin
      ShellExecute(Self.Handle, 'open', PChar('C:\Windows\Notepad.exe '), PChar(sl.strings[i]), nil,
      SW_NORMAL);
    end;
    form1.close;
    {sl.free;  should be move to FormClose}
  end;
end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    sl.free;
end;


meikl
0
 
MadshiCommented:
Hi mathes,

I'm not sure if I understood you right. Please check this one:

procedure TForm1.Button1Click(Sender: TObject);
var
  f: textfile;
  sl: tstringlist;
  i: word;
begin
  sl := tstringlist.create;
  repeat
    if savedialog1.execute then begin
      assignfile(f, savedialog1.filename);
      rewrite(f);
      writeln(f, edit1.text);
      writeln(f, edit2.text);
      closefile(f);
      ShowMessage('Your data were saved to a file.');
      sl.add(savedialog1.filename);
    end;
  until messagebox(0, 'Create one more text file ?', 'Question', 36) <> 6;
  for i := 0 to sl.count - 1 do begin
    ShellExecute(Self.Handle, 'open', PChar('C:\Windows\Notepad.exe '), PChar(sl.strings[i]), nil,
      SW_NORMAL);
  end;
  sl.free;
  Close;
end;

And perhaps two syntax tips:
(1) Your can write
       "if blabla <>5 then"
    instead of
       "if (blabla <> 5) = true then"
(2) In a object function like "TForm1.blabla" you don't need to use "self.handle", you can directly write "handle".
Perhaps you understand that if you image that Delphi does always this for you:

procedure TForm1.blabla;
begin
  with self do begin  // This is what Delphi does automatically
    ...
  end;
end;

Hope this helps...  :-)

Regards, Madshi.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
MadshiCommented:
Hi meikl, didn't see your comment...  :-)
0
 
kretzschmarCommented:
No prob, madshi, this happens me sometimes too ;-)

meikl
0
 
MadshiCommented:
I guess, you wrote your comment 0.3 seconds after I loaded the page and 4 mintes 59.7 seconds before I posted my comment...   :-)
0
 
intheCommented:
madshi,
Your can write
       "if blabla <>5 then"
    instead of
       "if (blabla <> 5) = true then"
this was'nt his idea it was mine(sorry)
this is what i told him  in another question ,he was having trouble with the form ,using a until loop to ask for input ,it kept looping back to the dialog all the time without waiting for input.
btw  what is pst time
Regards Barry

0
 
dr_gonzoCommented:
It should look like this:


procedure TForm1.Button1Click(Sender: TObject);
var
      f            : TextFile;
  sl      : TStringList;
  i            : Word;
begin
      sl := TStringList.Create;
  repeat
        SaveDialog1.Execute;
        AssignFile(f, SaveDialog1.Filename);
            reWrite(f);
    Writeln(f, edit1.text);
    Writeln(f, edit2.text);
    CloseFile(f);
    ShowMessage('Your data were saved to a file.');
    sl.add(SaveDialog1.Filename);
  until Windows.MessageBox(Handle, 'Create one more text file ?', 'Question', 36) <> 6;
  for i := 0 to sl.count - 1 do begin
        ShellExecute(Self.Handle, 'open', PChar('C:\Windows\Notepad.exe '), PChar(sl.strings[i]), nil,
                                             SW_NORMAL);
  end;
  sl.free;
end;

To all you american and english guys out there.
This have nothing to do with this problem but
you would proberly like to know it anyway.
Don't use MessageBox.
Use Windows.MessageBox.
The difference?
Messagebox always uses english Ok/Cancel-buttons.

Windows.MessageBox uses the international settings on
the users machine(which in my case(sweden) would be
Ok/Avbryt)
The worst I know is to get program that uses american style
dialog-boxes.

regards,
Dr. Gonzo
0
 
intheCommented:
oh i say Tally ho Chum Tally ho
you seen the dialog in those swedish films we get    hehe

0
 
dr_gonzoCommented:
What?
Could you try to write a correct english sentence?

regards,
Dr. Gonzo
0
 
mathesAuthor Commented:
dr_gonzo made a joke, whereas the other answer is correct.
0
 
kretzschmarCommented:
Hi dr_gonzo,

fast with answers? i think this solves not mathes problem! Mathes User has no chance to place other entrys in the edit-components! See also Madshis comment, just the same except the DialogBox, which is not the problem of this question. But thanks for the tip about to localize a dialogbox in the specific language.

meikl
0
 
mathesAuthor Commented:
how can I display the correct answer on my screen ? I can nothing see except
commets. Actually I would like to rate the answer now.


With kind regards

mathes

0
 
dr_gonzoCommented:
kretzschmar:
If the user shall be able to input things between them.
Why are the writing and reading positioned in the same
procedure? In that case half the code should be positioned
in FormClose or something. Or I'm I missing something here?

mathes:
the answer is always positioned on top of comments.
You'll find it right under your question.

regards,
Dr. Gonzo
0
 
kretzschmarCommented:
well dr_gonzo,

it can also shown so:


procedure TForm1.Button1Click(Sender: TObject);
var
  f: textfile;
  {sl: tstringlist;  -> should moved to global of the form }
  i: word;
begin
{ sl := tstringlist.create;  -> should be moved to formshow}
  if savedialog1.execute then
  begin
    assignfile(f, savedialog1.filename);
    rewrite(f);
    writeln(f, edit1.text);
    writeln(f, edit2.text);
    closefile(f);
    ShowMessage('Your data were saved to a file.');
    sl.add(savedialog1.filename);
  end;
  if (messagebox(0, 'Create one more text file ?', 'Question', 36) <> 6) = true
    then
    form1.close;
end;


procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
    for i := 0 to sl.count - 1 do
    begin
      ShellExecute(Self.Handle, 'open', PChar('C:\Windows\Notepad.exe '), PChar(sl.strings[i]), nil,
      SW_NORMAL);
    sl.free;
end;

meikl
0
 
mathesAuthor Commented:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  Dialogs,  StdCtrls,ShellApi;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    SaveDialog1: TSaveDialog;
   
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
      var
        f: textfile;
        sl: tstringlist;
        i: word;
      begin
        sl := tstringlist.create;
        repeat
          if savedialog1.execute then begin
            assignfile(f, savedialog1.filename);
            rewrite(f);
            writeln(f, edit1.text);
            writeln(f, edit2.text);
            closefile(f);
            ShowMessage('Your data were saved to a file.');
            sl.add(savedialog1.filename);
          end;
        until messagebox(0, 'Create one more text file ?', 'Question', 36) <> 6;
        for i := 0 to sl.count - 1 do begin
          ShellExecute(Self.Handle, 'open', PChar('C:\Windows\Notepad.exe '), PChar(sl.strings[i]), nil,
            SW_NORMAL);
        end;
        sl.free;
        Close;
      end;

end.
0
 
kretzschmarCommented:
Hi mathes,

there som bugs in the answer above (i've pasted something wrong
)
this now been tested

unit Unit1;
interface
uses
Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,
Dialogs,StdCtrls,ShellApi;
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    SaveDialog1: TSaveDialog;
    procedure Button1Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
      { Private-Deklarationen }
  public
      { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

var sl : tstringlist;

procedure TForm1.Button1Click(Sender: TObject);
var
  f: textfile;
begin
  if savedialog1.execute then
  begin
    assignfile(f, savedialog1.filename);
    rewrite(f);
    writeln(f, edit1.text);
    writeln(f, edit2.text);
    closefile(f);
    ShowMessage('Your data were saved to a file.');
    sl.add(savedialog1.filename);
  end;
  if (messagebox(0, 'Create one more text file ?', 'Question', 36) <> 6) = true then
    form1.close;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var i : Integer;
begin
  for i := 0 to sl.count - 1 do
    ShellExecute(Self.Handle, 'open', PChar('C:\Windows\Notepad.exe '), PChar(sl.strings[i]), nil,SW_NORMAL);
  sl.free;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  sl := tstringlist.create;
end;

end.


meikl
0
 
mathesAuthor Commented:

Hi experts,

thank you for your help.

I like the idea of kretzschmar to split the whole source and to make use of several events in my form.
This improves the readability of my sources.

And now of course the source does exactly what I want to be done.

With kind regards

Mathes

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.