Difference between revisions of "Howto Use TOpenDialog"

From Lazarus wiki
Jump to navigationJump to search
(Improve code example - check file exists, check success of execute, remove redundant var)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
 
{{Howto Use TOpenDialog}}
 
{{Howto Use TOpenDialog}}
  
'''Howto Use TOpenDialog'''
+
Simple and short guidelines:
  
Simple and short guideline:
+
1. Place a [[TOpenDialog]] widget [[image:topendialog.png]] on your [[TForm|form]]. It can be placed anywhere on your form as it is not visible during program [[runtime|run time]] but only during design time.
# Place a [[TOpenDialog]] widget [[image:topendialog.png]] on your form (anyplace, since it will be not visible).
 
# In your code write something similar to:
 
  
<syntaxhighlight>
+
[[Image:Component_Palette_Dialogs.png]]
var
 
  filename: string;
 
  
 +
It is located in the [[Dialogs tab]] of the [[Component Palette|component palette]] and is the leftmost component.
 +
 +
2. In your code write something similar to:
 +
 +
<syntaxhighlight lang="pascal">
 
if OpenDialog1.Execute then
 
if OpenDialog1.Execute then
begin
+
  begin
  filename := OpenDialog1.Filename;
+
    if fileExists(OpenDialog1.Filename) then
  ShowMessage(filename);
+
      ShowMessage(OpenDialog1.Filename);
end;
+
  end
 +
else
 +
  ShowMessage('No file selected');
 
</syntaxhighlight>
 
</syntaxhighlight>
  
The [[doc:lcl/dialogs/tcommondialog.execute.html | Execute]] method displays the file open dialog. It returns [[True|true]] when user has selected a file, [[False|false]] when user has aborted.
+
The dialog [[doc:lcl/dialogs/tcommondialog.execute.html | Execute]] [[Method|method]] displays the file open 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 dialog [[doc:lcl/dialogs/tfiledialog.filename.html | Filename]] [[Property|property]] returns the full filename including drive and path.
  
'''Note''': This control only collects the filename. It does not actually open the file. Your code must do that. It also does not check to see if the file actually exists.
+
{{Note|This control only collects the filename. It does not actually open the file. Your code must do that.}}
  
 
== See also ==
 
== See also ==
 +
 
* [[Howto Use TSaveDialog]]
 
* [[Howto Use TSaveDialog]]
* [[Dialogs tab]]
+
* [[CopyFile]]
* [[TOpenDialog]]
 
 
 
[[Category:Lazarus]]
 
[[Category:Tutorials]]
 

Latest revision as of 07:07, 27 March 2021

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

Simple and short guidelines:

1. Place a TOpenDialog widget topendialog.png on your form. It can be placed anywhere on your form as it is not visible during program run time but only during design time.

Component Palette Dialogs.png 

It is located in the Dialogs tab of the component palette and is the leftmost component.

2. In your code write something similar to:

if OpenDialog1.Execute then
  begin
    if fileExists(OpenDialog1.Filename) then
      ShowMessage(OpenDialog1.Filename);
  end
else
  ShowMessage('No file selected');

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

The dialog 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. Your code must do that.

See also