Solved

problem with messagebox

Posted on 1999-01-24
18
326 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:mathes
  • 6
  • 4
  • 3
  • +2
18 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1363033
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1363034
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
 
LVL 20

Expert Comment

by:Madshi
ID: 1363035
Hi meikl, didn't see your comment...  :-)
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 1363036
No prob, madshi, this happens me sometimes too ;-)

meikl
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1363037
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
 
LVL 17

Expert Comment

by:inthe
ID: 1363038
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
 

Expert Comment

by:dr_gonzo
ID: 1363039
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
 
LVL 17

Expert Comment

by:inthe
ID: 1363040
oh i say Tally ho Chum Tally ho
you seen the dialog in those swedish films we get    hehe

0
 

Expert Comment

by:dr_gonzo
ID: 1363041
What?
Could you try to write a correct english sentence?

regards,
Dr. Gonzo
0
 

Author Comment

by:mathes
ID: 1363042
dr_gonzo made a joke, whereas the other answer is correct.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1363043
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
 

Author Comment

by:mathes
ID: 1363044
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
 

Expert Comment

by:dr_gonzo
ID: 1363045
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1363046
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
 

Author Comment

by:mathes
ID: 1363047
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 20 total points
ID: 1363048
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 1363049
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
 

Author Comment

by:mathes
ID: 1363050

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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

837 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