PDA

View Full Version : Save and Open commands.



jhildy
12-19-2007, 12:18 AM
Anyone who has made a form recently knows about the Save and Open dialogs. They make it easy to open files and modify or whatever.
I am assuming all of you know how to make a basic form. Make a form with a button name it btnOpen. make the text say Open. Now put an edit box next to it and call it edOpen. This is how i designed mine.
http://img210.imageshack.us/img210/1752/02bg5.png (http://imageshack.us)
Now go to the tab dialogs on the form editor and click the first icon it looks like this.
http://img407.imageshack.us/img407/4199/02gx6.png (http://imageshack.us)
Now put that anywhere on your form (it isn't visible so it wont matter).
Now add a label below the open button and name it lblFile.
Save the form and add all your vars/ initform/ ect. procedures to make it work. Then go to the opendialog in your initform procedure and it should look like this.
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Position := poDesktopCenter;
frmDesign.BorderStyle := bsSingle;
frmDesign.Width := 365;
frmDesign.Height := 123;
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.PixelsPerInch := 96;
Button1 := TButton.Create(frmDesign);
Button1.Parent := frmDesign;
Button1.Left := 32;
Button1.Top := 24;
Button1.Width := 81;
Button1.Height := 25;
Button1.Caption := 'Open';
Button1.TabOrder := 8;
edOpen := TEdit.Create(frmDesign);
edOpen.Parent := frmDesign;
edOpen.Left := 120;
edOpen.Top := 24;
edOpen.Width := 201;
edOpen.Height := 25;
edOpen.TabOrder := 9;
OpenDialog1 := TOpenDialog.Create(frmDesign);
OpenDialog1.Parent := frmDesign;
OpenDialog1.Left := 200;
OpenDialog1.Top := 8;
lblFile := TLabel.Create(frmDesign);
lblFile.Parent := frmDesign;
lblFile.Left := 0;
lblFile.Top := 0;
lblFile.Width := 65;
lblFile.Height := 17;
end;


Now for me here i get an unknow identifier for Parent, Left, and Top in the opendialog but i dont know if everyone else does so i delete those three so it looks like this.
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Position := poDesktopCenter;
frmDesign.BorderStyle := bsSingle;
frmDesign.Width := 365;
frmDesign.Height := 123;
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.PixelsPerInch := 96;
Button1 := TButton.Create(frmDesign);
Button1.Parent := frmDesign;
Button1.Left := 32;
Button1.Top := 24;
Button1.Width := 81;
Button1.Height := 25;
Button1.Caption := 'Open';
Button1.TabOrder := 8;
edOpen := TEdit.Create(frmDesign);
edOpen.Parent := frmDesign;
edOpen.Left := 120;
edOpen.Top := 24;
edOpen.Width := 201;
edOpen.Height := 25;
edOpen.TabOrder := 9;
OpenDialog1 := TOpenDialog.Create(frmDesign);
lblFile := TLabel.Create(frmDesign);
lblFile.Parent := frmDesign;
lblFile.Left := 0;
lblFile.Top := 0;
lblFile.Width := 65;
lblFile.Height := 17;
end;

It doesn't make a difference either.
Now add a OnClick event to the btnOpen button.
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Position := poDesktopCenter;
frmDesign.BorderStyle := bsSingle;
frmDesign.Width := 365;
frmDesign.Height := 123;
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.PixelsPerInch := 96;
btnOpen := TButton.Create(frmDesign);
btnOpen.Parent := frmDesign;
btnOpen.Left := 32;
btnOpen.Top := 24;
btnOpen.Width := 81;
btnOpen.Height := 25;
btnOpen.Caption := 'Open';
btnOpen.TabOrder := 8;
btnOpen.OnClick := @FileOpen;
edOpen := TEdit.Create(frmDesign);
edOpen.Parent := frmDesign;
edOpen.Left := 120;
edOpen.Top := 24;
edOpen.Width := 201;
edOpen.Height := 25;
edOpen.TabOrder := 9;
OpenDialog1 := TOpenDialog.Create(frmDesign);
OpenDialog1.Parent := frmDesign;
OpenDialog1.Left := 200;
OpenDialog1.Top := 8;
lblFile := TLabel.Create(frmDesign);
lblFile.Parent := frmDesign;
lblFile.Left := 0;
lblFile.Top := 0;
lblFile.Width := 65;
lblFile.Height := 17;
end;


Now to make the FileOpen procedure.

Procedure FileOpen(Sender: TObject);
begin
opendialog1.Execute
edOpen.text := opendialog1.filename
end;

opendialog1.Execute opens the window to open files. Then when you pick the file it makes the editbox have the filelocation displayed in it. Try picking a few files with this just to see what it does.

Now to display the file. In the procedure FileOpen add this.
Procedure FileOpen(Sender: TObject);
var i: integer;
var s: string;
begin
opendialog1.Execute;
edOpen.text := opendialog1.filename;
I := OpenFile(Opendialog1.filename, false);
ReadFileString(I, s, filesize(i));
lblFile.caption := s;
closefile(i);
end;

This will read the exact text in the file and write it to the label you created. Then it closes the file so there are no memory leaks. Try this with a small text file you create. You can also do it with memos similar to the form parser by ron. you just set memo1.text := s insted of lblfile.caption. You can also edit the file in any way you want. Heres the finished script.
program New;

var
// InitForm variables.
frmDesign : TForm;
BtnOpen : TButton;
edOpen : TEdit;
OpenDialog1 : TOpenDialog;
lblFile : TLabel;

Procedure FileOpen(Sender: TObject);
var i: integer;
var s: string;
begin
opendialog1.Execute;
edOpen.text := opendialog1.filename;
I := OpenFile(Opendialog1.filename, false);
ReadFileString(I, s, filesize(i));
lblFile.caption := s;
closefile(i);
end;

// This form was parsed using: DFM Form Parser v.23 by Ron.
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Position := poDesktopCenter;
frmDesign.BorderStyle := bsSingle;
frmDesign.Width := 365;
frmDesign.Height := 123;
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.PixelsPerInch := 96;
BtnOpen := TButton.Create(frmDesign);
BtnOpen.Parent := frmDesign;
BtnOpen.Left := 32;
BtnOpen.Top := 24;
BtnOpen.Width := 81;
BtnOpen.Height := 25;
BtnOpen.Caption := 'Open';
BtnOpen.TabOrder := 8;
BtnOpen.OnClick := @FileOpen;
edOpen := TEdit.Create(frmDesign);
edOpen.Parent := frmDesign;
edOpen.Left := 120;
edOpen.Top := 24;
edOpen.Width := 201;
edOpen.Height := 25;
edOpen.TabOrder := 9;
OpenDialog1 := TOpenDialog.Create(frmDesign);
lblFile := TLabel.Create(frmDesign);
lblFile.Parent := frmDesign;
lblFile.Left := 0;
lblFile.Top := 0;
lblFile.Width := 65;
lblFile.Height := 17;
end;

procedure SafeInitForm;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('InitForm', V);
end;

procedure ShowInitFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowInitFormModal;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ShowInitFormModal', V);
end;

procedure MainInitForm;
begin
try
SafeInitForm;
SafeShowInitFormModal;
finally
FreeForm(frmDesign);
except
WriteLn('An error seems to have occurred in: InitForm');
end;
end;

begin
ClearDebug;
MainInitForm;
end.


You can mess around with these in any way you want and are helpful with getting chattext or whatever you want. Post if you want anything else explained here.

Now for saving. I changed the form around so the output of the file goes into the textbox and i got rif of the label. I also added the save dialog. It is right next to the open dialog when you are in the form editor. You can just copy my script for right now.
program New;

var
frmDesign : TForm;
btnOpen : TButton;
edFiles : TEdit;
btnSave : TButton;
OpenDialog1 : TOpenDialog;
SaveDialog1 : TSaveDialog;

Procedure FileOpen(Sender: TObject);
var i: integer;
var s: string;
begin
opendialog1.Execute;
I := OpenFile(Opendialog1.filename, false);
ReadFileString(I, s, filesize(i));
edFiles.text := s;
closefile(i);
end;

// This form was parsed using: DFM Form Parser v.23 by Ron.
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 315;
frmDesign.Top := 129;
frmDesign.Width := 321;
frmDesign.Height := 166;
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
btnOpen := TButton.Create(frmDesign);
btnOpen.Parent := frmDesign;
btnOpen.Left := 40;
btnOpen.Top := 48;
btnOpen.Width := 49;
btnOpen.Height := 25;
btnOpen.Caption := 'Open';
btnOpen.TabOrder := 8;
btnOpen.Onclick := @fileopen
edFiles := TEdit.Create(frmDesign);
edFiles.Parent := frmDesign;
edFiles.Left := 96;
edFiles.Top := 48;
edFiles.Width := 145;
edFiles.Height := 25;
edFiles.TabOrder := 9;
btnSave := TButton.Create(frmDesign);
btnSave.Parent := frmDesign;
btnSave.Left := 40;
btnSave.Top := 80;
btnSave.Width := 201;
btnSave.Height := 25;
btnSave.Caption := 'Save';
btnSave.TabOrder := 10;
OpenDialog1 := TOpenDialog.Create(frmDesign);
SaveDialog1 := TSaveDialog.Create(frmDesign);
end;

procedure SafeInitForm;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('InitForm', V);
end;

procedure ShowInitFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowInitFormModal;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ShowInitFormModal', V);
end;

procedure MainInitForm;
begin
try
SafeInitForm;
SafeShowInitFormModal;
finally
FreeForm(frmDesign);
except
WriteLn('An error seems to have occurred in: InitForm');
end;
end;

begin
ClearDebug;
MainInitForm;
end.

as you can see i added the save dialog and removed the parent and top and left vars because they don't work either. Now go down to the btnSave and add an onclick event and call it SaveFile. then go create that procedure below the openfile procedure. now your script should look like this.
program New;

var
frmDesign : TForm;
btnOpen : TButton;
edFiles : TEdit;
btnSave : TButton;
OpenDialog1 : TOpenDialog;
SaveDialog1 : TSaveDialog;

Procedure FileOpen(Sender: TObject);
var i: integer;
var s: string;
begin
opendialog1.Execute;
I := OpenFile(Opendialog1.filename, false);
ReadFileString(I, s, filesize(i));
edFiles.text := s;
closefile(i);
end;

Procedure SaveFile(Sender :TObject);
begin
end;

// This form was parsed using: DFM Form Parser v.23 by Ron.
procedure InitForm;
begin
frmDesign := CreateForm;
frmDesign.Left := 315;
frmDesign.Top := 129;
frmDesign.Width := 321;
frmDesign.Height := 166;
frmDesign.Caption := 'frmDesign';
frmDesign.Color := clBtnFace;
frmDesign.Font.Color := clWindowText;
frmDesign.Font.Height := -11;
frmDesign.Font.Name := 'MS Sans Serif';
frmDesign.Font.Style := [];
frmDesign.Visible := False;
frmDesign.PixelsPerInch := 96;
btnOpen := TButton.Create(frmDesign);
btnOpen.Parent := frmDesign;
btnOpen.Left := 40;
btnOpen.Top := 48;
btnOpen.Width := 49;
btnOpen.Height := 25;
btnOpen.Caption := 'Open';
btnOpen.TabOrder := 8;
btnOpen.Onclick := @fileopen
edFiles := TEdit.Create(frmDesign);
edFiles.Parent := frmDesign;
edFiles.Left := 96;
edFiles.Top := 48;
edFiles.Width := 145;
edFiles.Height := 25;
edFiles.TabOrder := 9;
btnSave := TButton.Create(frmDesign);
btnSave.Parent := frmDesign;
btnSave.Left := 40;
btnSave.Top := 80;
btnSave.Width := 201;
btnSave.Height := 25;
btnSave.Caption := 'Save';
btnSave.TabOrder := 10;
btnSave.OnClick := @SaveFile;
OpenDialog1 := TOpenDialog.Create(frmDesign);
SaveDialog1 := TSaveDialog.Create(frmDesign);
end;

procedure SafeInitForm;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('InitForm', V);
end;

procedure ShowInitFormModal;
begin
frmDesign.ShowModal;
end;

procedure SafeShowInitFormModal;
var
V : TVariantArray;
begin
SetArrayLength(V, 0);
ThreadSafeCall('ShowInitFormModal', V);
end;

procedure MainInitForm;
begin
try
SafeInitForm;
SafeShowInitFormModal;
finally
FreeForm(frmDesign);
except
WriteLn('An error seems to have occurred in: InitForm');
end;
end;

begin
ClearDebug;
MainInitForm;
end.


ok now we are going to take the text in the edFiles edit box and save it to a file. Add this to your SaveFile procedure. Procedure SaveFile(Sender :TObject);
var I: integer;
var S: string;
begin
Savedialog1.Filter := '.txt'
Savedialog1.Execute;
if pos(Savedialog1.Filter,savedialog1.FileName)<>0 then
begin
I := Rewritefile(savedialog1.filename, false);
if writefilestring(i , edFiles.Text) then
writeln('Success');
closefile(i);
end else;
savedialog1.filename := savedialog1.filename + Savedialog1.filter;
I := Rewritefile(savedialog1.filename, false);
if writefilestring(i , edFiles.Text) then
writeln('Success');
closefile(i);
end;

Now to explain it. Savedialog.filter is the extension that you allow. This helps later in the procedure to make sure its written with that extension. Then all i do is open the save dialog. The next part checks if the extension is in the filename. If its not it adds it. Then for saving you have to use rewrite file instead of open because open only reads the file and rewrite allows to write. The next part writes the string in the edit box to the file and if it does it successfully it writes success. then it closes the file to prevent leaking. The next part is if the savedialog1.filter wasnt in the filename. It then adds it so that it will save as that type of file. Then it repeats the steps above it. So try writing something in a text box and saving it to a text file. See what happens. You can change the filter to whatever you want .scar or whatever it will work fine.

Thanks for reading this tutorial if you have any questions post and i will address them.

Santa_Clause
12-19-2007, 12:31 AM
Very nice tutorial...! Haven't seen you around in a while either ;)

jhildy
12-19-2007, 12:40 AM
Yea i decided i havn't done much with scar (started learning java and started scripting for nexus) so i decided to get back into + i released something in the members section and helped dan cardin out recently and it inspired me lol.

Dan Cardin
12-19-2007, 12:53 AM
nice tut :)
trying to think of a way this could be implimented into my project that inpired u ;)

jhildy
12-19-2007, 12:56 AM
haha thanks. Even though you dont really know what the project is for lol.

jhildy
12-26-2007, 05:44 PM
sorry for double post but UPDATED for save!!!!!

R0b0t1
12-28-2007, 11:58 PM
You don't script for neXus... You type in five lines, and, poof!

Santa_Clause
12-29-2007, 12:03 AM
Haha to R0bot's post! What does this have to do with Nexus?

jhildy
12-29-2007, 12:49 AM
Yea i decided i havn't done much with scar (started learning java and started scripting for nexus) so i decided to get back into + i released something in the members section and helped dan cardin out recently and it inspired me lol.


and it may not be as many lines as scar but i did still script as if it was scar. MAAAAAAAD failsafes. It pretty much never messed up to the point i got to. Never really released any scripts because never finished. Oh well what do you think about this.

R0b0t1
12-29-2007, 01:02 AM
I think its... nice? But I pretty much read BenLand's mind to find this stuff before.


I tried NeXus... Boring as hell...

jhildy
12-29-2007, 08:08 PM
i just wanted it to learn the techniques of java mostly but im done with it i dont care anymore.