Solved

problem with messagebox

Posted on 1999-01-24
18
322 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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi meikl, didn't see your comment...  :-)
0
 
LVL 27

Expert Comment

by:kretzschmar
Comment Utility
No prob, madshi, this happens me sometimes too ;-)

meikl
0
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
What?
Could you try to write a correct english sentence?

regards,
Dr. Gonzo
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

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

Expert Comment

by:kretzschmar
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now