Howto Use TOpenDialog: what changed
Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 1 | + | Simple and short guidelines: | |
| 4 | 2 | ||
| 5 | - | ||
| 6 | - | ||
| 3 | + | 1\. Place a [[TOpenDialog]] widget [](</File: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. | |
| 7 | 4 | ``` | |
| 8 | - | ||
| 9 | - | ||
| 5 | + | [](</File:Component_Palette_Dialogs.png>) | |
| 6 | + | ``` | |
| 10 | 7 | ||
| 11 | - | ||
| 8 | + | It is located in the [[Dialogs tab]] of the [[Component Palette|component palette]] and is the leftmost component. | |
| 12 | 9 | ||
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 10 | + | 2\. In your code write something similar to: | |
| 21 | 11 | ``` | |
| 12 | + | procedure TForm1.FormCreate(Sender: TObject); | |
| 13 | + | begin | |
| 14 | + | OpenDialog1.InitialDir := 'C:\'; | |
| 15 | + | OpenDialog1.Filter := 'Binary files (*.bin;*.hex)|*.bin;*.hex|Text Files (*.txt)|*.txt|All Files (*.*)|*.*'; | |
| 16 | + | end; | |
| 17 | + | ||
| 18 | + | procedure TForm1.Button1Click(Sender: TObject); | |
| 19 | + | begin | |
| 20 | + | if OpenDialog1.Execute then | |
| 21 | + | begin | |
| 22 | + | if fileExists(OpenDialog1.Filename) then | |
| 23 | + | ShowMessage(OpenDialog1.Filename); | |
| 24 | + | end | |
| 25 | + | else | |
| 26 | + | ShowMessage('No file selected'); | |
| 27 | + | end; | |
| 28 | + | ``` | |
| 22 | 29 | ||
| 23 | - | ||
| 30 | + | The dialog [ Execute](<http://lazarus-ccr.sourceforge.net/docs/lcl/dialogs/tcommondialog.execute.html> "doc:lcl/dialogs/tcommondialog.execute.html") [[Method|method]] displays the file open dialog. It returns [[True|true]] when user has selected a file, [[False|false]] when user has aborted. | |
| 24 | 31 | ||
| 25 | - | ||
| 32 | + | The dialog [ Filename](<http://lazarus-ccr.sourceforge.net/docs/lcl/dialogs/tfiledialog.filename.html> "doc:lcl/dialogs/tfiledialog.filename.html") [[Property|property]] returns the full filename including drive and path. | |
| 26 | 33 | ||
| 27 | - | ||
| 34 | + |  **Note:** This control only collects the filename. It does not actually open the file. Your code must do that. | |
| 28 | 35 | ||
| 29 | 36 | ## See also | |
| 30 | 37 | ||
| 31 | - | ||
| 32 | - | ||
| 38 | + | * [[Howto Use TSaveDialog]] | |
| 39 | + | * [[CopyFile]] | |
| 33 | 40 | ||
| 34 | 41 | --- | |
| 35 | - | ||
| 42 | + | *From the Free Pascal wiki, page "Howto Use TOpenDialog", as the Internet Archive kept it on 2025-06-16, last changed on 2025-03-19. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
