Difference between revisions of "Howto Use TSaveDialog"

From Lazarus wiki
Jump to navigationJump to search
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 
{{Howto Use TSaveDialog}}
 
{{Howto Use TSaveDialog}}
 
Howto Use TSaveDialog
 
  
 
[[Image:tsavedialog.png]]
 
[[Image:tsavedialog.png]]
Line 7: Line 5:
 
Simple guideline:
 
Simple guideline:
  
1. Place the SaveDialog widget on your form (anyplace, since it will be not visible).
+
# Place the [[TSaveDialog|SaveDialog]] widget [[Image:tsavedialog.png]] on your [[TForm|form]] (anyplace, since it will be not visible). <br /> [[Image:Component_Palette_Dialogs.png]] <br /> (It is the second left dialog under [[Dialogs tab]])
 
+
# Add a [[TMemo|memo]] [[Image:tmemo.png]] in the form.
  [[Image:Component_Palette_Dialogs.png]]
+
# Add a [[TButton|button]] [[Image:tbutton.png]] in the form.
  (It is the second left dialog under [[Dialogs tab]])
 
 
 
2. Add a memo in the form.
 
 
 
[[Image:tmemo.png]]
 
 
 
  
3. Add a button in the form.
 
  
[[Image:tbutton.png]]
 
  
 
The [[IDE_Window:_Object_Inspector|Object Inspector]] will display the properties of the object Button1.
 
The [[IDE_Window:_Object_Inspector|Object Inspector]] will display the properties of the object Button1.
Change a property named 'Caption', with the displayed value 'Button1' to 'Save'.
+
Change a [[Property|property]] named 'Caption', with the displayed value 'Button1' to 'Save'.
Click on the Events tab on the Object Inspector.Select the box to the right of OnClick:
+
Click on the Events tab on the Object Inspector. Select the box to the right of OnClick:
 
a smaller box with three dots (... ellipsis) appears. Click on this,
 
a smaller box with three dots (... ellipsis) appears. Click on this,
 
you are taken automatically into the Source Editor and your cursor will be
 
you are taken automatically into the Source Editor and your cursor will be
 
placed in a piece of code starting. Completion code:
 
placed in a piece of code starting. Completion code:
 
+
<syntaxhighlight lang="pascal">
<syntaxhighlight>
+
   
  procedure TForm1.Button1Click( Sender: TObject );
+
procedure TForm1.Button1Click( Sender: TObject );
 
  begin
 
  begin
 
   if SaveDialog1.Execute then
 
   if SaveDialog1.Execute then
Line 36: Line 26:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The [[doc:lcl/dialogs/tcommondialog.execute.html | Execute]] method displays the file save dialog. It returns [[True|true]] when user has selected a file, [[False|false]] when user has aborted.
+
The [[doc:lcl/dialogs/tcommondialog.execute.html | Execute]] [[Method|method]] displays the file save dialog. It returns [[True|true]] when user has selected a file, [[False|false]] when user has aborted.
  
 
The [[doc:lcl/dialogs/tfiledialog.filename.html | Filename]] property returns the full filename including drive and path.
 
The [[doc:lcl/dialogs/tfiledialog.filename.html | Filename]] property returns the full filename including drive and path.
  
'''Note''': This control only collects the filename. It does not actually open the file for writing. Your code must do that.  It also does not verify if they want to overwrite a file with the same name. You must check to see if the file already exists before overwriting an existing file.
+
{{Note| This control only collects the filename. It does not actually open the file for writing. Your code must do that.}}  
 
 
 
== See also ==
 
== See also ==
  
 
* [[Howto Use TOpenDialog]]
 
* [[Howto Use TOpenDialog]]
* [[Dialogs_tab| Dialogs tab]]
+
* [[CopyFile]]
* [[Standard_tab#TButton| TButton]]
 
* [[Standard_tab#TMemo| TMemo]]
 
 
 
[[Category:Lazarus]]
 
[[Category:LCL]]
 
[[Category:Components]]
 
[[Category:Tutorials]]
 

Latest revision as of 07:53, 8 September 2019

Deutsch (de) English (en) español (es) suomi (fi) français (fr) 日本語 (ja) polski (pl) русский (ru) slovenčina (sk)

tsavedialog.png

Simple guideline:

  1. Place the SaveDialog widget tsavedialog.png on your form (anyplace, since it will be not visible).
    Component Palette Dialogs.png
    (It is the second left dialog under Dialogs tab)
  2. Add a memo tmemo.png in the form.
  3. Add a button tbutton.png in the form.


The Object Inspector will display the properties of the object Button1. Change a property named 'Caption', with the displayed value 'Button1' to 'Save'. Click on the Events tab on the Object Inspector. Select the box to the right of OnClick: a smaller box with three dots (... ellipsis) appears. Click on this, you are taken automatically into the Source Editor and your cursor will be placed in a piece of code starting. Completion code:

 
procedure TForm1.Button1Click( Sender: TObject );
 begin
   if SaveDialog1.Execute then
    Memo1.Lines.SaveToFile( SaveDialog1.Filename );
 end;

The Execute method displays the file save dialog. It returns true when user has selected a file, false when user has aborted.

The Filename property returns the full filename including drive and path.

Light bulb  Note: This control only collects the filename. It does not actually open the file for writing. Your code must do that.

See also