Your SlideShare is downloading. ×
Delphi L05 Files and Dialogs
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Delphi L05 Files and Dialogs

446
views

Published on

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
446
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Intro to Event-driven Programming and Forms with Delphi L05 – Files and Dialogs Mohammad Shaker mohammadshakergtr.wordpress.com Intro to Event-driven Programming and Forms with Delphi @ZGTRShaker 2010, 2011, 2012
  • 2. Small Tip • StrToFloat(); StrToInt(); IntToStr(); FloatToStr();
  • 3. What’s for today? • FILES Processing () • MediaPlayer • Dialogs: – OpenDialog – SaveDialog – MsgDlg
  • 4. Files • “Pascal Code” file processing, like the following: – – – – Assign > AssignFile ReSet (Reading from “file”) ReWrite (Writing to “file”) Close > CloseFile • File Handlers: Windows • File Streams: Look like “Binary File”s
  • 5. File Processing - Input & Output routines Procedures & Functions Append AssignFile BlockRead BlockWrite ChDir CloseFile Eof Eoln Erase FilePos FileSize IOResult Description Opens an existing text file for appending. Assigns the name of an external file to a file variable. Reads one or more records from an untyped file. Writes one or more records into an untyped file. Changes the current directory. Closes an open file. Returns the end-of-file status of a file. Returns the end-of-line status of a text file. Erases an external file. Returns the current file position of a typed or untyped file. Returns the current size of a file; not used for text files. Returns an integer value that is the status of the last I/O function performed.
  • 6. File Processing - Input & Output routines Procedures & Functions Read Readln Rename Reset Rewrite Seek SeekEof SeekEoln Truncate Write Writeln Description Reads one or more values from a file into one or more variables. Does what Read does and then skips to beginning of next line in the text file. Renames an external file. Opens an existing file. Creates and opens a new file. Moves the current position of a typed or untyped file to a specified component. Not used with text files. Returns the end-of-file status of a text file. Returns the end-of-line status of a text file. Truncates a typed or untyped file at the current file position. Writes one or more values to a file. Does the same as Write, and then writes an end-of-line marker to the text file.
  • 7. File Processing • File Coping: procedure TForm1.Button1Click(Sender: TObject); begin CopyFile('D:Sour.jpg','D:TempFolderDest.jpg',False); end; Source Destination “Boolean” indicate that the source file will overwrite the targeted file “IF FOUND”
  • 8. File Processing • File Moving: procedure TForm1.Button1Click(Sender: TObject); begin MoveFile('D:PlayerSour.jpg','D:CrierDest.jpg‘); end; Source Destination No “Boolean” Indicator for overwriting
  • 9. File Processing • File Moving: procedure TForm1.Button1Click(Sender: TObject); begin MoveFile('D:PlayerSour.jpg','D:CrierDest.jpg‘); end;  Note that:  the “Source” file will be renamed to be the “Destination” file name in Destination path.  Watch out  The function “MoveFile” will fail if:  The “Source” file is not exist.  The “Destination” file existed.
  • 10. File Processing • File Deleting: – Delete a “specific” file. procedure TForm1.Button1Click(Sender: TObject); begin Delete('D:FlierSour.jpg’); end;
  • 11. File Processing • “2 Be Or Not 2 Be” Files Prop. (Boolean): – FileExists > Boolean procedure TForm1.Button1Click(Sender: TObject); begin if (FileExists('D:Dest.jpg')=true)then Edit1.Text:='The file exists.' else Edit1.Text:='The file does not exist.'; end;  Watch out for “V AR” in the procedure’s header when calling a “file” as a “variables”.
  • 12. File Processing test is live!
  • 13. File Features • Types: – Text, Typed, UnTyped • Text files: – opened with ReSet are read-only. – opened with ReWrite and Append are write-only. • Typed & UnTyped files: – always allow both reading and writing, regardless whether they are opened with ReSet or ReWrite.
  • 14. File Features • Caution: – The type “Text” is distinct from the typed file “ file of Char ”. • Text files: – there are special forms of Read and Write that let you read and write values that are not of type Char. Such values are automatically translated to and from their character representation. For example, Read(F, I), where I is a type Integer variable, reads a sequence of digits, interprets that sequence as a decimal integer, and stores it in I. – “file of Char” don’t – UnTyped files: • are low-level I/O channels used primarily for direct access to disk files regardless of type and structuring. An UnTyped file is declared with the word file and nothing more. // we declare the file like any other variables Var FUnTyped:file; // UnTyped File
  • 15. File features • Types: – Text, Typed, UnTyped // Text file declaration Var FText: text; // Typed file declaration Var FReal: file of Real; Var FTyped: file of Real; Var FRecd: file of Recd; Var FTyped: file of Recd; Var FInt: file of integer; Var FTyped: file of integer; // Un Typed file declaration Var FUnTyped: file;
  • 16. Delphi code area unit Unit1; interface Name of the “unit” we are working in Libraries we can take functions & procedures from it uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} end. Types & classes Public Variables Here begins the “unit” implementation
  • 17. File • Files containing types: – Every file is a “linear sequence” of components, each of which has the component type (or record type) of the file. – The components are numbered starting with zero. – Files are normally accessed sequentially. That is, when a component is: • READ : using the standard procedure Read • WRITTEN : using the standard procedure Write • the current file position moves to the next numerically ordered file component.
  • 18. File • Typed files and UnTyped files can also be accessed randomly through the standard procedure Seek, which moves the current file position to a “specified” component. • We can use the following functions: – FilePos – FileSize : determines the current file position : determines the current file size
  • 19. File • Like Pascal: – Before a file variable can be used: • it must be associated with an “External File” through a call to the AssignFile procedure. – “External File”: • Stores the information written to it (Input). • Supplies the information read it (Output). – Once the association with an “External File” is established, the file variable must be "opened" to prepare it for input or output. • ReWrite (Input). • ReSet(Output).
  • 20. File • Existence & Creation: – An “Existing file” can be opened via the Reset procedure – A “New file” can be created and opened via the Rewrite procedure. • Caution: – When a program completes processing a file, the file must be closed using the standard procedure CloseFile (As Usual). – After a file is closed, its associated “External File” is updated. – The file variable can then be associated with another “External File”.
  • 21. File Processing Example • Let’s have a “FInteger= file of integer” Type that is “Type of integer file” and have the following “form” style • Now, what we need is that we must enter “integer” values in “Input to file” in association with the “Write” button. • Then, we want to see each “Entered” value when pressing “Read” button each time. • Note that: – The 2nd edit “Edit2” is “ReadOnly”’ – When pressing the “Read” button, the “Write” button becomes unEnabled. – We consider the “ReSet” & “ReWrite” Procedures’ “restrictions”. – The initial case for edits controls are “-1” that is all the entered values afterward should be “positive” [0,+∞[.
  • 22. File Processing Example tik-tok
  • 23. File Processing Example’s solution • First of all we make the “Edit 2” is “ReadOnly: – ”Edit2 > Properties > ReadOnly > True Type FInteger= file of integer; Var Form1: TForm1; F:FInteger;
  • 24. File Processing Example’s solution // initializing the file for the 1st time procedure TForm1.FormCreate(Sender: TObject); begin AssignFile(F,'D:IntegerFile.txt'); Rewrite(F); end; // (Write) button procedure TForm1.Button1Click(Sender: TObject); var x:integer; begin x:=strtoint(Edit1.Text); write(F,x); end;
  • 25. File Processing Example’s solution // (Read) button procedure TForm1.Button2Click(Sender: TObject); var x:integer; Begin if (Edit2.Text=‘-1’) then begin Button1.Enabled:=false; // disabling Button1 for 1st time CloseFile(F); ReSet(F); end; if (not(eof(f))=true) then begin read(F,x); Edit2.Text:=inttostr(x); end; end;
  • 26. File Run
  • 27. File Run • First of all, we should include the “ExtActns” unit to our project. Uses ExtActns; • We declare the “File” like any other “Variable” Var FRun: TFileRun;
  • 28. Delphi code area unit Unit1; interface Name of the “unit” we are working in Libraries we can take functions & procedures from it uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} end. Types & classes Public Variables Here begins the “unit” implementation
  • 29. File Run • 1st Code Sample (No browse dialog needed): Frun:= TFileRun.Create(nil); FRun.FileName:= ‘D:kekoke.exe’; FRun.Execute; // here the specified // file will be opened // and executed • 2nd Code Sample (Browse dialog showed & needed): FRun:= TFileRun.Create(nil); FRun.browse:= true; FRun.Execute; // // // // here a browse dialog will be opened to let the user choose the file sheshe wants:D
  • 30. File Run • Note that when you execute the following code the player you have will be opened and that’s inconvenient • So.. What to do? Frun:= TFileRun.Create(nil); FRun.FileName:= 'D:SthInMind.mp3'; FRun.Execute();
  • 31. Mediaplayer, Component
  • 32. Mediaplayer • Here nothing will be displayed.. And also u can control how to play your music throw MediaPlayer procedure TForm2.Button1Click(Sender: TObject); begin // FileName can be changed at RunDesign Time mediaplayer1.filename:= 'D:SthInMind.mp3’; mediaplayer1.open; mediaplayer1.play; end;
  • 33. File Run Test Live
  • 34. OpenDialog
  • 35. OpenDialog • It won’t “open” anything • It’s just an “OpenDialog”
  • 36. OpenDialog Prop. • Properties: – InitialDir* – File name: • The “Default” directory of the targeted “file” • This would maybe change at Runtime as it’s up for user to choose. – Filter: • Filter Name | Filter • “Text” files | *.txt – Filterindex: • determines which of the file types in Filter is selected by default when the dialog opens. Set FilterIndex to 1 to choose the first file type in the list as the default, or set FilterIndex to 2 to choose the second file type as the default, and so forth. If the value of FilterIndex is out or range, the first file type listed in Filter is the default. • ‫ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ‬ – *InitialDir: After some slides.. So don’t worry:D
  • 37. OpenDialog • “OpenDialog1.Execute()” Procedure: – Execute opens the file-selection dialog, returning: • true when the user selects a file and clicks Open. • If the user clicks Cancel, Execute returns false. • Code example: procedure TForm2.Button1Click(Sender: TObject); begin OpenDialog1.Execute(); End;
  • 38. OpenDialog • “OpenDialog1.Execute()” Procedure: – Execute opens the file-selection dialog, returning: • true when the user selects a file and clicks Open. • If the user clicks Cancel, Execute returns false. • Code example: procedure TForm2.Button1Click(Sender: TObject); begin If (OpenDialog1.Execute()) then Begin Label1.caption := OpenDialog1.filename; Edit1.text := OpenDialog1.filename; Memo1.lines.Add(OpenDialog1.filename); End; End;
  • 39. OpenDialog • Code example: // OpenDialog with instant running file sample procedure TForm1.Button1Click(Sender: TObject); begin If (OpenDialog1.Execute()) then Begin FRun:= TFileRun.Create(nil); FRun.FileName:= OpenDialog1.FileName; FRun.Execute(); End; end;
  • 40. OpenDialog • Code example procedure TForm1.Button1Click(Sender: TObject); begin If (OpenDialog1.Execute()) then Begin FRun:=TFileRun.Create(nil); FRun.Browse:= true; FRun.Execute(); End; end;
  • 41. OpenDialog • Code example: – So we’ll have 2 “OpenDialog”s to browse from them. – The Question now is: • Which of the selected “files” will run? procedure TForm1.Button1Click(Sender: TObject); begin If (OpenDialog1.Execute()) then Begin FRun:= TFileRun.Create(nil); FRun.browse:= true; FRun.Execute(); End; end;
  • 42. OpenDialog procedure TForm1.Button1Click(Sender: TObject); begin if(OpenDialog1.Execute()) then Begin Image1.Picture.LoadFromFile(OpenDialog1.FileName); End; end;
  • 43. OpenDialog Design Time Runtime
  • 44. TOpenDialog.InitialDir Property Delphi Help • InitialDir: – determines the default directory displayed in the file-selection dialog when it opens. – For example, to point the dialog at the (WINDOWSSYSTEM) directory, set the value of InitialDir to (C:WINDOWSSYSTEM). – If no value is assigned to InitialDir, or if the specified directory does not exist. • the initial directory is controlled by the global ForceCurrentDirectory variable. If ForceCurrentDirectory is true, the dialog opens with the current working directory displayed. Otherwise, the dialog opens with either the current working directory or the “My Documents” directory, depending on the version of your Windows.
  • 45. SaveDialog
  • 46. SaveDialog • Exactly like OpenDialog • It won’t “save” anything • It’s just an “SaveDialog”
  • 47. SaveDialog Prop. • Properties: – InitialDir* – File name: The directory of the targeted “file” – Filter: • Filter Name | Filter • “Text” files | *.txt – Filterindex
  • 48. SaveDialog • “SaveDialog1.Execute” Procedure: – Execute opens the file-selection dialog, returning: • true when the user selects a file and clicks Save. • If the user clicks Cancel, Execute returns false. • Code example: procedure TForm2.Button2Click(Sender: TObject); begin If (SaveDialog1.Execute()) then Begin Label1.caption := SaveDialog1.filename; Edit1.text := SaveDialog1.filename; Memo1.lines[0] := SaveDialog1.filename; End; End;
  • 49. SaveDialog procedure TForm2.Button2Click(Sender: TObject); begin If (SaveDialog1.Execute()) then Begin // Save file by the dialog’s name End; End;
  • 50. MessageDlg
  • 51. MessageDlg
  • 52. MessageDlg • • • • • function MessageDlg (const Msg: string; DlgType: TMsgDlgType; Buttons: TMsgDlgButtons; HelpCtx: Longint
  • 53. MessageDlg procedure TForm1.Button1Click(Sender: TObject); begin MessageDlg(‘Hello lk where are u going, jd bdak troo7?’ , mtConfirmation, [mbYes, mbNo, mbCancel],0); end;
  • 54. MessageDlg procedure TForm1.Button1Click(Sender: TObject); begin MessageDlg(‘Hello lk where are u going, jd bdak troo7?’ , mtWarning, [mbYes, mbNo, mbCancel],0); end;
  • 55. MessageDlg • TMsgDlgBtn
  • 56. MessageDlg • Dealing With MsgDlg // Show a custom dialog buttonSelected:= MessageDlg('Custom dialog',mtCustom, [mbYes,mbAll,mbCancel], 0); // if if if Show the button type selected buttonSelected = mrYes then ShowMessage('Yes pressed'); buttonSelected = mrAll then ShowMessage('All pressed'); buttonSelected = mrCancel then ShowMessage('Cancel pressed');
  • 57. MessageDlg • Dealing With MsgDlg procedure TForm1.Button1Click(Sender: TObject); begin if (MessageDlg('Hello lk where are u going, jd bdak troo7?' ,mtWarning, [mbYes, mbNo, mbCancel],0) = mrYes) then begin // Form1.Close(); // Not True, all the time Application.Terminate(); end; end;
  • 58. MessageDlg • Consider we have the following procedure TForm1.Button1Click(Sender: TObject); begin if (MessageDlg('Hello lk where are u going, jd bdak troo7?' ,mtWarning, [mbYes, mbNo, mbCancel],0) = mrYes) then begin Form1.hide(); Form2.Close(); // Not True, all the time //Application.Terminate(); end; end;
  • 59. MessageDlg
  • 60. MessageDlg Note that The Application is still Running
  • 61. MessageDlg • Dealing With MsgDlg procedure TForm1.Button1Click(Sender: TObject); begin if (MessageDlg('Hello lk where are u going, jd bdak troo7?' ,mtWarning, [mbYes, mbNo, mbCancel],0) = mrYes) then begin // Form1.hide(); // Form2.Close(); // Not True, all the time Application.Terminate(); end; end; This is the proper thing
  • 62. See you!