[ Pobierz całość w formacie PDF ] .4 Double-click the ActionManager to open it.4-16 Qui ck St ar tWr i t i n g e v e n t h a n d l e r s5 In the Action Manager editor, select the File category and then double-click theNew action.Tip You can also double-click the File|FileNew action in the Object TreeView.The Code editor opens with the cursor inside the event handler.Double-click the action tocreate an empty eventhandler where you canspecify what will happen whenusers execute the command.6 Right where the cursor is positioned in the Code editor (between begin and end),type the following lines:RichEdit1.Clear;FileName := 'untitled.txt';StatusBar1.Panels[0].Text := FileName;Your event handler should look like this when you re done:This line clears the text areawhen you create a new file.This line calls the new file untitled.txt.This line puts the file nameinto the status bar.Save your work and that s it for the File|New command.Note You can resize the code portion of the window to reduce horizontal scrolling.Cr eat i ng a t ex t edi t or a t ut or i al 4-17Wr i t i n g e v e n t h a n d l e r sCreating an event handler for the Open commandTo open a file in the text editor, you want a standard Windows Open dialog box toappear.You ve already added a standard File|Open command to the ActionManager editor, which automatically includes the dialog box.However, you stillneed to customize the event handler for the command.1 Press F12 to locate the main form (or select View|Forms and choose Form1).2 Double-click the Action Manager editor to open it.Select the File|Open action.3 In the Object Inspector, click the plus sign to the left of the Dialog property toexpand its properties.Delphi names the dialog box FileOpen1.OpenDialog bydefault.When OpenDialog1 s Execute method is called, it invokes the standarddialog box for opening files.4 Set the following properties of FileOpen1.Dialog:" Set DefaultExt to txt." Double-click the text area next to Filter to display the Filter editor.In the firstrow under the Filter Name column, type Text files (*.txt).In the Filtercolumn, type *.txt.In the second row under the Filter Name column, type Allfiles (*.*) and in the Filter column, type *.*.Click OK.Use the Filter editor to definefilters for theFileOpen1.Dialog andFileSaveAs1.Dialog actions." After Title, type Open file.These words will appear at the top of the Open dialogbox.5 Click the Events tab.Double-click the OnAccept event so that FileOpen1Acceptappears.6 The Code editor opens with the cursor inside the event handler.7 Right where the cursor is positioned in the Code editor (between begin and end),type the following lines:RichEdit1.Lines.LoadFromFile(FileOpen1.Dialog.FileName);FileName := FileOpen1.Dialog.FileName;StatusBar1.Panels[0].Text := FileName;4-18 Qui ck St ar tWr i t i n g e v e n t h a n d l e r sYour FileOpen event handler should look like this when you re done:This line inserts the textfrom the specified file.This line sets the file nameto the one in the Opendialog box.This line puts the file nameinto the status bar.That s it for the File|Open command and the Open dialog box.Creating an event handler for the Save commandTo create an event handler for the Save command:1 Press F12 to display the form.Double-click the ActionManager component to openit.2 Double-click the File|Save action.The Code editor opens with the cursor inside the event handler.Tip You can also double-click the File|FileSave action in the Object TreeView.With the Object TreeView,you can access the eventhandlers for each action.Double-click the action toopen the Code editor andwrite a new event handler.3 Right where the cursor is positioned in the Code editor (between begin and end),type the following lines:if (FileName = 'untitled.txt') thenFileSaveAs1.ExecuteelseRichEdit1.Lines.SaveToFile(FileName);This code tells the text editor to display the SaveAs dialog box if the file isn tnamed yet so the user can assign a name to it.Otherwise, save the file using itscurrent name.The SaveAs dialog box is defined in the event handler for the SaveAs command on page 4-20.FileSaveAs1BeforeExecute is the automatically generatedname for the Save As command
[ Pobierz całość w formacie PDF ] zanotowane.pldoc.pisz.plpdf.pisz.plmikr.xlx.pl
|