| View previous topic :: View next topic |
| Author |
Message |
DevilGilad Grandmaster Cheater
Reputation: 0
Joined: 10 May 2007 Posts: 624 Location: Delete C:\WINDOWS folder and you'll be able to see me.
|
Posted: Sat Jul 14, 2007 1:46 pm Post subject: [Delphi question] Saving function? |
|
|
How do I use the saving function?
For example, I want to save a text the user's wrote on a textbox to textfile. How can I do that? You all know how to do it, and I... don't :/
Thanks alot...
... Uhh, forgot... I got another question, if that's not annying you.. I don't want to open new topic...
Let's take an example that I got two forms, on one of them there's TextBox and the other has Button. How can I tell the button to write on the textbox something, while it's in another form?
_________________
|
|
| Back to top |
|
 |
Kevin Grandmaster Cheater Supreme
Reputation: 0
Joined: 07 Mar 2007 Posts: 1139 Location: Spiderman-World
|
Posted: Sat Jul 14, 2007 2:09 pm Post subject: |
|
|
uhm, use the dialog "SaveDialog" from the dialogs tab. then check out www.riddlersoft.com for the code.
uhm... to the second... i think you can just use something like form1.memo1 to refer to the memo/richedit or whatever you use on the first form, just use form1. infront of it to refer to form 1...
|
|
| Back to top |
|
 |
DevilGilad Grandmaster Cheater
Reputation: 0
Joined: 10 May 2007 Posts: 624 Location: Delete C:\WINDOWS folder and you'll be able to see me.
|
Posted: Sat Jul 14, 2007 2:10 pm Post subject: |
|
|
| Kevinnn wrote: | uhm, use the dialog "SaveDialog" from the dialogs tab. then check out www.riddlersoft.com for the code.
uhm... to the second... i think you can just use something like form1.memo1 to refer to the memo/richedit or whatever you use on the first form, just use form1. infront of it to refer to form 1... |
If so, I have to write somethin' like Form1.Memo1.Text? O_o"
_________________
|
|
| Back to top |
|
 |
Kevin Grandmaster Cheater Supreme
Reputation: 0
Joined: 07 Mar 2007 Posts: 1139 Location: Spiderman-World
|
Posted: Sat Jul 14, 2007 2:13 pm Post subject: |
|
|
you want to click a button , then it says something in the text box?
like when you hit the button, it writes "Hi im Devil" or whatever in the memo?
edit: or are you talking about saving function?
|
|
| Back to top |
|
 |
DevilGilad Grandmaster Cheater
Reputation: 0
Joined: 10 May 2007 Posts: 624 Location: Delete C:\WINDOWS folder and you'll be able to see me.
|
Posted: Sat Jul 14, 2007 2:17 pm Post subject: |
|
|
| Kevinnn wrote: | you want to click a button , then it says something in the text box?
like when you hit the button, it writes "Hi im Devil" or whatever in the memo?
edit: or are you talking about saving function? |
My questions are not connected each to other...
Anyways - yeah. I want to do something like when I click a button on form2 it'll show a text on the textbox on form1...
_________________
|
|
| Back to top |
|
 |
magicalimp Expert Cheater
Reputation: 0
Joined: 03 Dec 2006 Posts: 105
|
Posted: Sat Jul 14, 2007 5:40 pm Post subject: |
|
|
Saving to a text file...
Under var, create a variable with variable type TextFile.
Assign your variable a text file path using AssignFile() i.e.
AssignFile(passwords, 'passwords.txt');
Use Reset() to open the file and place the cursor at the start of the file. (You cannot write to a file if you use Reset)
Use Append() to open the file and place the cursor at the end of a file. (You cannot read from a file if you use Append)
Use Rewrite() to overwrite the file and create a new, blank document. (You cannot read from a file if you use Rewrite)
Use Write() or Writeln() to write to the text file. Write writes the string and places the cursor at the end of the line. Writeln writes the string and places the cursor on the next line. The syntax is Write(FileHandle, string)
Use Read() or Readln() to read from a file. Read reads the current line that the cursor is on and leaves the cursor at the end of a line. Readln reads the current line that the cursor is on and moves the cursor to the next line. The syntax is Read(FileHandle, string)
Finally, use CloseFile() to close the file (durr.)
So, if you were writing to a file, your code would look something like this.
Procedure FileWrite(Sender: TObject);
var
MyFile: TextFile;
begin
AssignFile(MyFile, 'MyFile.txt');
if FileExists('MyFile.txt') then
begin
Append(MyFile);
Writeln(Edit1.text);
end
else
begin
Rewrite(MyFile);
Writeln(Edit1.text);
end;
end;
Now, if you wanted to write your file contents to a memo, your code would have to read from the text file, then either directly transfer the data to your memo, or store it in a temporary variable, then transfer it. I opted to store it to a temporary variable and then append it to the memo.
Procedure DisplayText(Sender: TObject);
var
MyFile: TextFile;
Temp: String;
begin
TMemo1.Lines.Clear;
AssignFile(MyFile, 'MyFile.txt');
if FileExists('MyFile.txt') then
begin
Reset(MyFile);
while NOT EOF(MyFile) do
begin
Readln(MyFile, Temp);
TMemo1.Lines.Add(Temp);
end;
end
else
begin
ShowMessage('Error: Text File could not be found);
end;
end;
|
|
| Back to top |
|
 |
|