Author: Maarten de Haan
How to access menuitems like an array and how to write just one onclick procedure
for all of them?
Answer:
1
2 // Suppose you have an application with a TMainMenu.
3 // Under the MenuItem, with caption "File" of the MainMenu, you can
4 // choose the "normal" things, like:
5 // "Open", "New", "Close", Save", "Print", "Printer Setup", "Exit" and
6 // so on.
7 // At the end of the "File" menu there is a recent file list with all
8 // the files in it which you recently opened with this application.
9 // (Just like in Word.) Suppose the names of these MenuItems are:
10 //
11 // FileLastFile1 : TMenuItem;
12 // FileLastFile2 : TMenuItem;
13 // FileLastFile3 : TMenuItem;
14 // FileLastFile4 : TMenuItem;
15 // FileLastFile5 : TMenuItem;
16 // FileLastFile6 : TMenuItem;
17 // FileLastFile7 : TMenuItem;
18 // FileLastFile8 : TMenuItem;
19 // FileLastFile9 : TMenuItem;
20 //
21 // When the application starts, it opens a config file, where the drive,
22 // path and filenames of the recent file list are read. You can set the
23 // captions of the MenuItems with the following procedure:
24 //----------------------------------------------------------------------
25
26 procedure ReadConfigFile;
27
28 var
29 // It is better to make this a global TMenuItem array so you only
30 // have to assign once.
31 aMenuItem: array[1..9] of TMenuItem
32 f: TextFile;
33 I: Integer;
34 S: string;
35
36 begin
37 // If this array is global, you only have to assign once. F.i. in the
38 // procedure OnFormCreate. After that you can always access the
39 // MenuItems like an array, with: aMenuItem[Index].
40 aMenuItem[1] := FileLastFile1;
41 aMenuItem[2] := FileLastFile2;
42 aMenuItem[3] := FileLastFile3;
43 aMenuItem[4] := FileLastFile4;
44 aMenuItem[5] := FileLastFile5;
45 aMenuItem[6] := FileLastFile6;
46 aMenuItem[7] := FileLastFile7;
47 aMenuItem[8] := FileLastFile8;
48 aMenuItem[9] := FileLastFile9;
49
50 // Now the MenuItems are in an TMenuItem array, we can get easy access
51 // to the individual MenuItems. Like this:
52 for I := 1 to 9 do
53 aMenuItem[I].Caption := ''; // Make the caption empty.
54
55 // Open the config file with the recent file list. Example:
56 AssignFile(f, 'c:\my_ini_file.ini');
57 Reset(f);
58
59 // Read out the recent file names and put them in a global string array.
60 // Example:
61 for I := 1 to 9 do
62 if not Eof(f) then
63 ReadLn(f, aRecentfile[I]);
64 System.CloseFile(f);
65 //
66 // Of course you can also do it the "windows" way with:
67 // MyIniFile := TIniFile.Create('c:\my_ini_file.ini');
68 // For I := 1 to 9 do
69 // aRecentFile[I] := MyIniFile.ReadString('Section','RecentFile' +
70 // Chr(I + Ord('0')), '');
71 // MyIniFile.Free;
72 //
73 // If the content of the IniFile looks like this:
74 //
75 // [Section]
76 // RecentFile1=c:\firstfile.bmp
77 // RecentFile2=c:\secondfile.txt
78 // ...
79 //
80 // Now you can assign the filenames to the caption of the TMenuItems.
81 // Example:
82 for I := 1 to 9 do
83 aMenuItem[I].Caption := ExtractFileName(aRecentFile[I]);
84 end;
85 //----------------------------------------------------------------------
86 // You can also point ALL the OnClick events of the TMenuItems to just
87 // one procedure. F.i. the procedure below.
88 // The procedure finds out on which MenuItem is clicked, and opens the
89 // right file from the array with the recent filenames.
90 //----------------------------------------------------------------------
91
92 procedure TMainForm.FileLastFileClick(Sender: TObject);
93
94 var
95 sFilename: string;
96 I: Integer;
97 sS: string;
98 iErr: Integer;
99
100 begin
101 // Assumes that the global string array aRecentFile[1..9] holds the
102 // filenames of the recent file list, read during startup of the app.
103
104 // Find out which one is clicked.
105 // Get the name of the sender: FileLastFile1..FileLastFile9
106 sS := (Sender as TComponent).Name;
107 // Get the number at the end of the name of the sender, so you know
108 // which one is clicked. Example:
109 Val(sS[13], I, iErr);
110 if iErr <> 0 then
111 begin
112 // sS[13] is an illegal character.
113 Exit;
114 end;
115
116 // Now the variable "I" holds the number of the MenuItem, which was
117 // clicked on.
118
119 // Get the filename from the array of filenames and do something with
120 // it. Example:
121 sFileName := aRecentFile[I];
122 if sFileName <> '' then
123 begin
124
125 if FileExists(sFileName) then
126 begin
127 // Open the file
128 // Do your stuff
129 end
130 else
131 begin
132 // Something wrong! The file cannot be found.
133 // Send a message. Example:
134 // ShowMessage('Cannot find the requested file: ' + sFilename + '.');
135 // Or grayout this MenuItem in the "File" menu. Example:
136 // (Sender as TComponent).Enabled := False;
137 // Or hide this MenuItem in the "File" menu. Example:
138 // (Sender as TComponent).Hide;
139 end;
140 end;
141 end;
142 //----------------------------------------------------------------------
|