Question : Wor with ini file


With :
with TINIFile.Create(ExtractPath(Form1.caption)+'.ini')
i would like to work in:
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
procedure TForm1.FormCreate(Sender: TObject);

i need to get path where initialization of my form is stored. And i get it from the Form caption.

Example:  myform      The archive is located in: C:\myform
1:
2:
3:
4:
5:
6:
7:
Function TForm1.ExtractPath(Str:String):String;
Var
 P:Integer;
begin
 P:=Pos(':\',Str);
 if P>0 Then Result:=Copy(Str,P-1,Length(Str)) Else Result:='';
end;

Answer : Wor with ini file

I don't understand much of what you want to do, but I have an extensive tool unit to load/save components & forms properties (captions, checked status, etc...) in Ini files.

Even if you don't want to use it as it is, I'm sure you will find lots of ideas here.
When you include this unit in a project, it automatically initialize an Ini file named after the application or dll name that use it.

All you have to do is to call
IniSaveComponnents
and
IniLoadComponnents

with an array of components or forms that you want to manage :

procedure TForm1.FormCreate(Sender:TObject);
begin
 IniLoadComponnents([ Self, edtParam1, chkOption1 ]); // => save the form, and 2 components
end;

procedure TForm1.FormDestroy(Sender:TObject);
begin
 IniSaveComponnents([ Self, edtParam1, chkOption1 ]); // => save the form, and 2 components
end;


You can add an additional section parameter that will be added to the automatic Ini section created. That is to help regroup a bit the key/values in the Ini, if used in many forms, but that does not impact what is really stored. You can also add a TextOnly boolean parameter to only load/save the captions, not the values (for a simple language management)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
307:
308:
309:
310:
311:
312:
313:
314:
315:
316:
317:
318:
319:
320:
321:
322:
323:
324:
325:
326:
327:
328:
329:
330:
331:
332:
333:
334:
335:
336:
337:
338:
339:
340:
341:
342:
343:
344:
345:
346:
347:
348:
349:
350:
351:
352:
353:
354:
355:
356:
357:
358:
359:
360:
361:
362:
363:
364:
365:
366:
367:
368:
369:
370:
371:
372:
373:
374:
375:
376:
377:
378:
379:
380:
381:
382:
383:
384:
385:
386:
387:
388:
389:
390:
391:
392:
393:
394:
395:
396:
397:
398:
399:
400:
401:
402:
403:
404:
405:
406:
407:
408:
409:
410:
411:
412:
413:
414:
415:
416:
417:
418:
419:
420:
421:
422:
423:
424:
425:
426:
427:
428:
429:
430:
431:
432:
433:
434:
435:
436:
437:
438:
439:
440:
441:
442:
443:
444:
445:
446:
447:
448:
449:
450:
451:
452:
453:
454:
455:
456:
457:
458:
459:
460:
461:
462:
463:
464:
465:
466:
467:
468:
469:
470:
471:
472:
473:
474:
475:
476:
477:
478:
479:
480:
481:
482:
483:
484:
485:
486:
487:
488:
489:
490:
491:
492:
493:
494:
495:
496:
497:
498:
499:
500:
501:
502:
503:
504:
505:
506:
507:
508:
509:
510:
511:
512:
513:
514:
515:
516:
517:
518:
519:
520:
521:
522:
523:
524:
525:
526:
527:
528:
529:
530:
531:
532:
533:
534:
535:
536:
537:
538:
539:
540:
541:
542:
543:
544:
545:
546:
547:
548:
549:
550:
551:
552:
553:
554:
555:
556:
557:
558:
559:
560:
561:
562:
563:
564:
565:
566:
567:
568:
569:
570:
571:
572:
573:
574:
575:
576:
577:
578:
579:
580:
581:
582:
583:
584:
585:
586:
587:
588:
589:
590:
591:
592:
593:
594:
595:
596:
597:
598:
599:
600:
601:
602:
603:
604:
605:
606:
607:
608:
609:
610:
611:
612:
613:
614:
615:
616:
617:
618:
619:
620:
621:
622:
623:
624:
625:
626:
627:
628:
629:
630:
631:
632:
633:
634:
635:
636:
637:
638:
639:
640:
641:
642:
643:
644:
645:
646:
647:
648:
649:
650:
651:
652:
653:
654:
655:
656:
657:
658:
659:
660:
661:
662:
663:
664:
665:
666:
667:
668:
669:
670:
671:
672:
673:
674:
675:
676:
677:
678:
679:
680:
681:
682:
683:
684:
685:
686:
687:
688:
689:
690:
691:
692:
unit IniOptions;

interface

Uses Forms,classes,IniFiles,StdCtrls,ExtCtrls,Buttons,Grids,Controls,Dialogs,Graphics,ComCtrls,Menus, Spin;

Type
 TStringListOption=(slUpperCase);
 TStringListOptions=set of TStringListOption;

procedure IniOpenFile(FileName:String='';FilePath:String=''); overload;
procedure IniOpenFile(Instance:Integer;FilePath:String=''); overload;
procedure IniCloseFile;

procedure IniSaveQuotedCaption(Section,Param,Default:String);
function IniLoadQuotedCaption(Section,Param,Default:String):String;

function IniLoadSaveDefaultString(Section,Param,Default:String):String;
function IniLoadSaveDefault(Section,Param,Default:String):String; overload;
function IniLoadSaveDefault(Section,Param:String;Default:Integer):Integer; overload;
function IniLoadSaveDefault(Section,Param:String;Default:Boolean):Boolean; overload;
procedure IniLoadSaveDefaultStringList(Section,Param:String;List:TStringList;Opt:TStringListOptions=[]);

procedure IniLoadMenu(M:TMenu);
procedure IniSaveMenu(M:TMenu);
procedure IniLoadListView(L:TListView);
procedure IniSaveListView(L:TListView);
procedure IniLoadFormPosAndSize(frm:TCustomForm);
procedure IniSaveFormPosAndSize(frm:TCustomForm);
procedure IniLoadCheckBox(chk:TCheckBox);
procedure IniSaveCheckBox(chk:TCheckBox);
procedure IniLoadRadioButton(rb:TRadioButton);
procedure IniSaveRadioButton(rb:TRadioButton);
procedure IniLoadCaption(lbl:TLabel);
procedure IniSaveCaption(lbl:TLabel);
procedure IniLoadText(edt:TCustomEdit);
procedure IniSaveText(edt:TCustomEdit);
procedure IniLoadRadioGroup(rg:TRadioGroup);
procedure IniSaveRadioGroup(rg:TRadioGroup);
procedure IniLoadGroupBox(gb:TGroupBox);
procedure IniSaveGroupBox(gb:TGroupBox);
procedure IniLoadComboBox(cb:TComboBox);
procedure IniSaveComboBox(cb:TComboBox);
{
procedure IniLoadSpinEdit(edt:TSpinEdit);
procedure IniSaveSpinEdit(edt:TSpinEdit);
}
procedure IniLoadButton(btn:TButton);
procedure IniSaveButton(btn:TButton);
procedure IniLoadSpeedButton(btn:TSpeedButton);
procedure IniSaveSpeedButton(btn:TSpeedButton);
procedure IniLoadFormCaption(frm:TForm);
procedure IniSaveFormCaption(frm:TForm);
procedure IniSavePanel(pnl:TPanel);
procedure IniLoadPanel(pnl:TPanel);
procedure IniSaveFrame(pnl:TFrame);
procedure IniLoadFrame(pnl:TFrame);
procedure IniLoadStringGrid(grid:TStringGrid);
procedure IniSaveStringGrid(grid:TStringGrid);
procedure IniLoadShape(shp:TShape);
procedure IniLoadImage(img:TImage);
procedure IniSaveShape(shp:TShape);
procedure IniSaveImage(img:TImage);
procedure IniSaveControl(c:TControl);
procedure IniLoadControl(c:TControl);
procedure IniLoadOpenDialog(dlg:TOpenDialog);
procedure IniSaveOpenDialog(dlg:TOpenDialog);
procedure IniLoadMemo(memo:TMemo);
procedure IniSaveMemo(memo:TMemo);

procedure SetExtendedSection(S:String);
function  GetExtendedSection:String;
function  GetTextOnly:Boolean;
procedure SetTextOnly(b:Boolean);
function TranslateFormat2Text(S:String):String;
function TranslateText2Format(S:String):String;

procedure IniSaveComponnents(List:Array of TObject;Ext_Section:String='';TextOnly:Boolean=False);
procedure IniLoadComponnents(List:Array of TObject;Ext_Section:String='';TextOnly:Boolean=False);

Var
 IniFile:TIniFile;

implementation

Uses SysUtils,untUtils,FileCtrl;

Var
 _ExtSection:String='';
 _TextOnly:Boolean=False;

procedure SetExtendedSection(S:String);
begin
 _ExtSection:=S;
end;

function  GetExtendedSection:String;
begin
 Result:=_ExtSection;
end;

function  GetTextOnly:Boolean;
begin
 Result:=_TextOnly;
end;

procedure SetTextOnly(b:Boolean);
begin
 _TextOnly:=b;
end;

function TranslateFormat2Text(S:String):String;
begin
 Result:=StringReplace(S,'%\',#13#10,[rfReplaceAll]);
end;

function TranslateText2Format(S:String):String;
begin
 Result:=StringReplace(S,#13#10,'%\',[rfReplaceAll]);
end;

procedure IniOpenFile(Instance:Integer;FilePath:String='');
begin
 IniOpenFile(ChangeFileExt(GetModuleName(hInstance),'.INI'),FilePath);
end;

procedure IniOpenFile(FileName:String='';FilePath:String='');
begin
 IniCloseFile;
 if FileName='' Then FileName:=ChangeFileExt(GetModuleName(hInstance),'.INI');// ChangeFileExt(ParamStr(0),'.INI');
 if (FilePath<>'') Then
  begin
   FilePath:=IncludeTrailingBackslash(FilePath);
   if DirectoryExists(FilePath) Then FileName:=FilePath+FileName;
  end;
 IniFile:=TIniFile.Create(FileName);
end;

procedure IniCloseFile;
begin
 FreeAndNil(IniFile);
end;

function IniLoadSaveDefaultString(Section,Param,Default:String):String;
begin
 Result:=IniLoadSaveDefault(Section,Param,Default);
end;

procedure IniLoadSaveDefaultStringList(Section,Param:String;List:TStringList;Opt:TStringListOptions=[]);
Var
 i:integer;
begin
 Section:=_ExtSection+Section;
 List.CommaText:=IniFile.ReadString(Section,Param,List.CommaText);
 if slUpperCase In Opt Then
  begin
   List.Sorted:=False;
   for i:=0 to List.Count-1 do List[i]:=UpperCase(List[i]);
   List.Sorted:=True;
  end;
 IniFile.WriteString(Section,Param,List.CommaText);
end;

function IniLoadSaveDefault(Section,Param,Default:String):String; overload;
begin
 Section:=_ExtSection+Section;
 Result:=IniFile.ReadString(Section,Param,Default);
 IniFile.WriteString(Section,Param,Result);
end;

function IniLoadSaveDefault(Section,Param:String;Default:Integer):Integer; overload;
begin
 Section:=_ExtSection+Section;
 Result:=IniFile.ReadInteger(Section,Param,Default);
 IniFile.WriteInteger(Section,Param,Result);
end;

function IniLoadSaveDefault(Section,Param:String;Default:Boolean):Boolean; overload;
begin
 Section:=_ExtSection+Section;
 Result:=IniFile.ReadBool(Section,Param,Default);
 IniFile.WriteBool(Section,Param,Result);
end;

procedure IniSaveQuotedCaption(Section,Param,Default:String);
begin
 if Trim(Default)<>Default Then Default:=AnsiQuotedStr(Default,'"');
 IniFile.WriteString(_ExtSection+Section,Param,Default);
end;

function IniLoadQuotedCaption(Section,Param,Default:String):String;
Var
 P:PChar;
begin
 Result:=IniFile.ReadString(_ExtSection+Section,Param,Default);
 if (Length(Result)>=2) And (Result[1]='"') Then
  begin
   P:=PChar(Result);
   Result:=AnsiExtractQuotedStr(P,'"');
  end;
end;

procedure IniLoadFormPosAndSize(frm:TCustomForm);
Var
 Section:String;
begin
 Section:=_ExtSection+frm.Name;
 frm.Caption:=IniFile.ReadString(Section,'CAPTION',frm.Caption);
 if Not _TextOnly Then
  begin
   if frm Is TForm Then TForm(frm).Position:=poDesigned;
   frm.Width:=IniFile.ReadInteger(Section,'WIDTH',frm.Width);
   frm.Height:=IniFile.ReadInteger(Section,'HEIGHT',frm.Height);
   frm.Top:=IniFile.ReadInteger(Section,'TOP',frm.Top);
   frm.Left:=IniFile.ReadInteger(Section,'LEFT',frm.Left);
   frm.WindowState:=TWindowState(IniFile.ReadInteger(Section,'STATE',Integer(frm.WindowState)));
  end;
end;

procedure IniSaveFormPosAndSize(frm:TCustomForm);
Var
 Section:String;
begin
 Section:=_ExtSection+frm.Name;
 IniFile.WriteString(Section,'CAPTION',frm.Caption);
 if Not _TextOnly Then
  begin
   IniFile.WriteInteger(Section,'STATE',Integer(frm.WindowState));
   IniFile.WriteInteger(Section,'WIDTH',frm.Width);
   IniFile.WriteInteger(Section,'HEIGHT',frm.Height);
   IniFile.WriteInteger(Section,'TOP',frm.Top);
   IniFile.WriteInteger(Section,'LEFT',frm.Left);
  end;
end;

procedure IniLoadMemo(memo:TMemo);
begin
 memo.Text:=TranslateFormat2Text(IniFile.ReadString(_ExtSection+memo.Name,'TEXT',TranslateText2Format(memo.Text)));
end;

procedure IniSaveMemo(memo:TMemo);
begin
 IniFile.WriteString(_ExtSection+memo.Name,'TEXT',TranslateText2Format(memo.Text));
end;

procedure IniLoadCheckBox(chk:TCheckBox);
begin
 chk.Caption:=IniLoadQuotedCaption(chk.Name,'CAPTION',chk.Caption);
 if Not _TextOnly Then chk.Checked:=IniFile.ReadBool(_ExtSection+chk.Name,'CHECKED',chk.Checked);
end;

procedure IniSaveCheckBox(chk:TCheckBox);
begin
 IniSaveQuotedCaption(chk.Name,'CAPTION',chk.Caption);
 if Not _TextOnly Then IniFile.WriteBool(_ExtSection+chk.Name,'CHECKED',chk.Checked);
end;

procedure IniLoadShape(shp:TShape);
Var
 Section:String;
begin
 with shp do
  begin
   Section:=_ExtSection+Name;
   Visible:=IniFile.ReadBool(Section,'VISIBLE',Visible);
   Pen.Color:=StringToColor(IniFile.ReadString(Section,'PEN_COLOR',ColorToString(Pen.Color)));
   Brush.Color:=StringToColor(IniFile.ReadString(Section,'BRUSH_COLOR',ColorToString(Brush.Color)));
  end;
end;

procedure IniLoadImage(img:TImage);
Var
 F:String;
 Section:String;
begin
 With img do
  try
   Section:=_ExtSection+Name;
   Visible:=IniFile.ReadBool(Section,'VISIBLE',Visible);
   Transparent:=IniFile.ReadBool(Section,'TRANSPARENT',Transparent);
   Stretch:=IniFile.ReadBool(Section,'STRETCH',Stretch);
   F:=IniFile.ReadString(Section,'FILE','');
   if FileExists(F) Then Picture.LoadFromFile(F);
  except
  end;
end;

procedure IniSaveShape(shp:TShape);
Var
 Section:String;
begin
 with shp do
  begin
   Section:=_ExtSection+Name;
   IniFile.WriteBool(Section,'VISIBLE',Visible);
   IniFile.WriteString(Section,'PEN_COLOR',ColorToString(Pen.Color));
   IniFile.WriteString(Section,'BRUSH_COLOR',ColorToString(Brush.Color));
  end;
end;

procedure IniSaveImage(img:TImage);
Var
 Section:String;
begin
 With img do
  begin
   Section:=_ExtSection+Name;
   IniFile.WriteBool(Section,'VISIBLE',Visible);
   IniFile.WriteBool(Section,'TRANSPARENT',Transparent);
   IniFile.WriteBool(Section,'STRETCH',Stretch);
  end;
end;

procedure IniLoadRadioButton(rb:TRadioButton);
begin
 rb.Caption:=IniLoadQuotedCaption(rb.Name,'CAPTION',rb.Caption);
 if Not _TextOnly Then rb.Checked:=IniFile.ReadBool(_ExtSection+rb.Name,'CHECKED',rb.Checked);
end;

procedure IniSaveRadioButton(rb:TRadioButton);
begin
 IniSaveQuotedCaption(rb.Name,'CAPTION',rb.Caption);
 if Not _TextOnly Then IniFile.WriteBool(_ExtSection+rb.Name,'CHECKED',rb.Checked);
end;

procedure IniLoadText(edt:TCustomEdit);
begin
 edt.Text:=IniLoadQuotedCaption(edt.Name,'TEXT',edt.Text);
end;

procedure IniSaveText(edt:TCustomEdit);
begin
 IniSaveQuotedCaption(edt.Name,'TEXT',edt.Text);
end;

procedure IniLoadCaption(lbl:TLabel);
begin
 lbl.Caption:=IniLoadQuotedCaption(lbl.Name,'CAPTION',lbl.Caption);
end;

procedure IniSaveCaption(lbl:TLabel);
begin
 IniSaveQuotedCaption(lbl.Name,'CAPTION',lbl.Caption);
end;

procedure IniLoadFormCaption(frm:TForm);
begin
 frm.Caption:=IniLoadQuotedCaption(frm.Name,'CAPTION',frm.Caption);
end;

procedure IniSaveFormCaption(frm:TForm);
begin
 IniSaveQuotedCaption(frm.Name,'CAPTION',frm.Caption);
end;

procedure IniLoadButton(btn:TButton);
begin
 btn.Caption:=IniLoadQuotedCaption(btn.Name,'CAPTION',btn.Caption);
end;

procedure IniSaveButton(btn:TButton);
begin
 IniSaveQuotedCaption(btn.Name,'CAPTION',btn.Caption);
end;

procedure IniLoadBitButton(btn:TBitBtn);
begin
 btn.Caption:=IniLoadQuotedCaption(btn.Name,'CAPTION',btn.Caption);
end;

procedure IniSaveBitButton(btn:TBitBtn);
begin
 IniSaveQuotedCaption(btn.Name,'CAPTION',btn.Caption);
end;

procedure IniLoadSpeedButton(btn:TSpeedButton);
begin
 btn.Caption:=IniLoadQuotedCaption(btn.Name,'CAPTION',btn.Caption);
end;

procedure IniSaveSpeedButton(btn:TSpeedButton);
begin
 IniSaveQuotedCaption(btn.Name,'CAPTION',btn.Caption);
end;

procedure IniLoadRadioGroup(rg:TRadioGroup);
begin
 rg.Caption:=IniLoadQuotedCaption(rg.Name,'CAPTION',rg.Caption);
 if Not _TextOnly Then rg.ItemIndex:=IniFile.ReadInteger(_ExtSection+rg.Name,'INDEX',rg.ItemIndex);
end;

procedure IniSaveRadioGroup(rg:TRadioGroup);
begin
 IniSaveQuotedCaption(rg.Name,'CAPTION',rg.Caption);
 if Not _TextOnly Then IniFile.WriteInteger(_ExtSection+rg.Name,'INDEX',rg.ItemIndex);
end;

procedure IniLoadGroupBox(gb:TGroupBox);
begin
 gb.Caption:=IniLoadQuotedCaption(gb.Name,'CAPTION',gb.Caption);
 if Not _TextOnly Then IniLoadControl(gb);
end;

procedure IniSaveGroupBox(gb:TGroupBox);
begin
 IniSaveQuotedCaption(gb.Name,'CAPTION',gb.Caption);
 if Not _TextOnly Then IniSaveControl(gb);
end;

procedure IniLoadComboBox(cb:TComboBox);
Var
 Str:String;
begin
 Str:=IniLoadQuotedCaption(cb.Name,'TEXT',cb.Text);
 if cb.Style=csDropDownList Then
  begin
   if Str<>'' Then cb.ItemIndex:=cb.Items.IndexOf(Str) Else cb.ItemIndex:=IniFile.ReadInteger(_ExtSection+cb.Name,'INDEX',cb.ItemIndex);
  end Else cb.Text:=Str;
end;

procedure IniSaveComboBox(cb:TComboBox);
begin
 IniSaveQuotedCaption(cb.Name,'TEXT',cb.Text);
 IniFile.WriteInteger(_ExtSection+cb.Name,'INDEX',cb.ItemIndex);
end;

{$ifdef USE_SPIN_EDT}
procedure IniLoadSpinEdit(edt:TSpinEdit);
begin
 edt.Value:=IniFile.ReadInteger(_ExtSection+edt.Name,'VALUE',edt.Value);
end;

procedure IniSaveSpinEdit(edt:TSpinEdit);
begin
 IniFile.WriteInteger(_ExtSection+edt.Name,'VALUE',edt.Value);
end;
{$endif}

procedure IniSavePanel(pnl:TPanel);
begin
 IniFile.WriteInteger(_ExtSection+pnl.Name,'WIDTH',pnl.Width);
 IniFile.WriteInteger(_ExtSection+pnl.Name,'HEIGHT',pnl.Height);
end;

procedure IniLoadPanel(pnl:TPanel);
begin
 pnl.Width:=IniFile.ReadInteger(_ExtSection+pnl.Name,'WIDTH',pnl.Width);
 pnl.Height:=IniFile.ReadInteger(_ExtSection+pnl.Name,'HEIGHT',pnl.Height);
end;

procedure IniSaveFrame(pnl:TFrame);
begin
 IniFile.WriteInteger(_ExtSection+pnl.Name,'WIDTH',pnl.Width);
 IniFile.WriteInteger(_ExtSection+pnl.Name,'HEIGHT',pnl.Height);
end;

procedure IniLoadFrame(pnl:TFrame);
begin
 pnl.Width:=IniFile.ReadInteger(_ExtSection+pnl.Name,'WIDTH',pnl.Width);
 pnl.Height:=IniFile.ReadInteger(_ExtSection+pnl.Name,'HEIGHT',pnl.Height);
end;

procedure IniLoadStringGrid(grid:TStringGrid);
Var
 i:integer;
 Section:String;
begin
 With grid do
  begin
   Section:=_ExtSection+Name;
   grid.ColCount:=IniFile.ReadInteger(Section,'COL_COUNT',grid.ColCount);
   for i:=0 to grid.ColCount-1 do
    begin
     grid.ColWidths[i]:=IniFile.ReadInteger(Section,Format('COL_%d_WIDTH',[i]),grid.ColWidths[i]);
     grid.Cells[i,0]:=IniFile.ReadString(Section,Format('COL_%d_TEXT',[i]),grid.Cells[i,0]);
    end;
  end;
end;

procedure IniSaveStringGrid(grid:TStringGrid);
Var
 i:integer;
 Section:String;
begin
 With grid do
  begin
   Section:=_ExtSection+Name;
   IniFile.WriteInteger(Section,'COL_COUNT',grid.ColCount);
   for i:=0 to grid.ColCount-1 do
    begin
     IniFile.WriteInteger(Section,Format('COL_%d_WIDTH',[i]),grid.ColWidths[i]);
     IniFile.WriteString(Section,Format('COL_%d_TEXT',[i]),grid.Cells[i,0]);
    end;
  end;
end;

procedure IniLoadOpenDialog(dlg:TOpenDialog);
begin
 dlg.InitialDir:=IniLoadQuotedCaption(dlg.Name,'INITIAL_DIR',dlg.InitialDir);
end;

procedure IniSaveOpenDialog(dlg:TOpenDialog);
begin
 IniSaveQuotedCaption(dlg.Name,'INITIAL_DIR',dlg.InitialDir);
end;

procedure IniSaveControl(c:TControl);
begin
 IniFile.WriteInteger(_ExtSection+c.Name,'WIDTH',c.Width);
 IniFile.WriteInteger(_ExtSection+c.Name,'HEIGHT',c.Height);
end;

procedure IniLoadControl(c:TControl);
begin
 c.Width:=IniFile.ReadInteger(_ExtSection+c.Name,'WIDTH',c.Width);
 c.Height:=IniFile.ReadInteger(_ExtSection+c.Name,'HEIGHT',c.Height);
end;

procedure IniLoadListView(L:TListView);
Var
 Section:String;
 i:integer;
begin
 With L do
  begin
   Section:=_ExtSection+Name;
   for i := 0 to Columns.Count - 1 do Columns[i].Caption:=IniFile.ReadString(Section,Format('COLUMN_%d',[i]),Columns[i].Caption);
  end;
end;

procedure IniSaveListView(L:TListView);
Var
 Section:String;
 i:integer;
begin
 With L do
  begin
   Section:=_ExtSection+Name;
   for i := 0 to Columns.Count - 1 do IniFile.WriteString(Section,Format('COLUMN_%d',[i]),Columns[i].Caption);
  end;
end;

procedure IniLoadMenu(M:TMenu);
Var
 Section:String;
 i:integer;

 procedure IniLoadMenuItem(M:TMenuItem;Sect:String);
 Var
  i:integer;
 begin
  Sect:=Sect+'.'+M.Name;
  for i := 0 to M.Count - 1 do if M.Items[i].Caption<>'-' Then
   begin
    M.Items[i].Caption:=IniFile.ReadString(Sect,M.Items[i].Name,M.Items[i].Caption);
    if M.Items[i].Count>0 then IniLoadMenuItem(M.Items[i],Sect);
   end;
 end;
begin
 With M do
  begin
   Section:=_ExtSection+Name;
   for i := 0 to M.Items.Count - 1 do if Items[i].Caption<>'-' Then
    begin
     Items[i].Caption:=IniFile.ReadString(Section,Items[i].Name,Items[i].Caption);
    end;
  end;
end;

procedure IniSaveMenu(M:TMenu);
Var
 Section:String;
 i:integer;
 procedure IniSaveMenuItem(M:TMenuItem;Sect:String);
 Var
  i:integer;
 begin
  Sect:=Sect+'.'+M.Name;
  for i := 0 to M.Count - 1 do if M.Items[i].Caption<>'-' Then
   begin
    IniFile.WriteString(Sect,M.Items[i].Name,M.Items[i].Caption);
    if M.Items[i].Count>0 then IniSaveMenuItem(M.Items[i],Sect);
   end;
 end;
begin
 With M do
  begin
   Section:=_ExtSection+Name;
   for i := 0 to M.Items.Count - 1 do if Items[i].Caption<>'-' Then
    begin
     IniFile.WriteString(Section,Items[i].Name,Items[i].Caption);
     if Items[i].Count>0 then IniSaveMenuItem(Items[i],_ExtSection);
    end;
  end;
end;

procedure IniSaveComponnents(List:Array of TObject;Ext_Section:String='';TextOnly:Boolean=False);
var
 i:integer;
 SaveExtSect:String;
 SaveTextOnly:Boolean;
begin
 SaveExtSect:=_ExtSection;
 _ExtSection:=Ext_Section;
 SaveTextOnly:=_TextOnly;
 _TextOnly:=TextOnly;
 for i:=Low(List) to High(List) do if Assigned(List[i]) Then
  try
   if List[i] Is TMemo        then IniSaveMemo          (TMemo        (List[i])) Else
   if List[i] Is TListView    then IniSaveListView      (TListView    (List[i])) Else
   if List[i] Is TMenu        then IniSaveMenu          (TMenu        (List[i])) Else
   if List[i] Is TCustomForm  Then IniSaveFormPosAndSize(TCustomForm  (List[i])) Else
   if List[i] Is TRadioGroup  Then IniSaveRadioGroup    (TRadioGroup  (List[i])) Else
   if List[i] Is TRadioButton Then IniSaveRadioButton   (TRadioButton (List[i])) Else
   if List[i] Is TCustomEdit  Then IniSaveText          (TCustomEdit  (List[i])) Else
   if List[i] Is TCheckBox    Then IniSaveCheckBox      (TCheckBox    (List[i])) Else
   if List[i] Is TComboBox    Then IniSaveComboBox      (TComboBox    (List[i])) Else
{$ifdef USE_SPIN_EDT}
   if List[i] Is TSpinEdit    Then IniSaveSpinEdit  	(TSpinEdit    (List[i])) Else
{$endif}
   if List[i] Is TLabel       Then IniSaveCaption  	(TLabel       (List[i])) Else
   if List[i] Is TBitBtn      Then IniSaveBitButton  	(TBitBtn      (List[i])) Else
   if List[i] Is TButton      Then IniSaveButton  	(TButton      (List[i])) Else
   if List[i] Is TSpeedButton Then IniSaveSpeedButton  	(TSpeedButton (List[i])) Else
   if List[i] Is TPanel       Then IniSavePanel 	(TPanel       (List[i])) Else
   if List[i] Is TFrame       Then IniSaveFrame 	(TFrame       (List[i])) Else
   if List[i] Is TStringGrid  Then IniSaveStringGrid    (TStringGrid  (List[i])) Else
   if List[i] Is TOpenDialog  Then IniSaveOpenDialog    (TOpenDialog  (List[i])) Else
   if List[i] Is TShape       Then IniSaveShape         (TShape       (List[i])) Else
   if List[i] Is TImage       Then IniSaveImage         (TImage       (List[i])) Else
   if List[i] Is TGroupBox    Then IniSaveGroupBox      (TGroupBox    (List[i])) Else
   if List[i] Is TControl     Then IniSaveControl       (TControl     (List[i])) Else
   if List[i] Is TComponent   Then ShowMessage('Erreur Save '+TComponent(List[i]).Name+':'+List[i].ClassName)
    Else ShowMessage('Erreur Save '+List[i].ClassName);
  except
   on E:Exception do ShowMessage(Format('Error Saving %s : %s'#13'%s',[TComponent(List[i]).Name,E.ClassName,E.Message]));
  end;
 _ExtSection:=SaveExtSect;
 _TextOnly:=SaveTextOnly;
end;

procedure IniLoadComponnents(List:Array of TObject;Ext_Section:String='';TextOnly:Boolean=False);
var
 i:integer;
 SaveExtSect:String;
 SaveTextOnly:Boolean;
begin
 SaveExtSect:=_ExtSection;
 _ExtSection:=Ext_Section;
 SaveTextOnly:=_TextOnly;
 _TextOnly:=TextOnly;
 for i:=Low(List) to High(List) do if Assigned(List[i]) Then
  try
   if List[i] Is TMemo        then IniLoadMemo          (TMemo        (List[i])) Else
   if List[i] Is TListView    then IniLoadListView      (TListView    (List[i])) Else
   if List[i] Is TMenu        then IniLoadMenu          (TMenu        (List[i])) Else
   if List[i] Is TCustomForm  Then IniLoadFormPosAndSize(TCustomForm  (List[i])) Else
   if List[i] Is TRadioGroup  Then IniLoadRadioGroup    (TRadioGroup  (List[i])) Else
   if List[i] Is TRadioButton Then IniLoadRadioButton   (TRadioButton (List[i])) Else
   if List[i] Is TCustomEdit  Then IniLoadText          (TCustomEdit  (List[i])) Else
   if List[i] Is TCheckBox    Then IniLoadCheckBox      (TCheckBox    (List[i])) Else
   if List[i] Is TComboBox    Then IniLoadComboBox      (TComboBox    (List[i])) Else
{$ifdef USE_SPIN_EDT}
   if List[i] Is TSpinEdit    Then IniLoadSpinEdit  	(TSpinEdit    (List[i])) Else
{$endif}
   if List[i] Is TLabel       Then IniLoadCaption  	(TLabel       (List[i])) Else
   if List[i] Is TBitBtn      Then IniLoadBitButton  	(TBitBtn      (List[i])) Else
   if List[i] Is TButton      Then IniLoadButton  	(TButton      (List[i])) Else
   if List[i] Is TSpeedButton Then IniLoadSpeedButton  	(TSpeedButton (List[i])) Else
   if List[i] Is TPanel       Then IniLoadPanel 	(TPanel       (List[i])) Else
   if List[i] Is TFrame       Then IniLoadFrame 	(TFrame       (List[i])) Else
   if List[i] Is TStringGrid  Then IniLoadStringGrid    (TStringGrid  (List[i])) Else
   if List[i] Is TOpenDialog  Then IniLoadOpenDialog    (TOpenDialog  (List[i])) Else
   if List[i] Is TShape       Then IniLoadShape         (TShape       (List[i])) Else
   if List[i] Is TImage       Then IniLoadImage         (TImage       (List[i])) Else
   if List[i] Is TGroupBox    Then IniLoadGroupBox      (TGroupBox    (List[i])) Else
   if List[i] Is TControl     Then IniLoadControl       (TControl     (List[i])) Else
   if List[i] Is TComponent   Then ShowMessage('Erreur Load '+TComponent(List[i]).Name+':'+List[i].ClassName) Else
    ShowMessage('Erreur Load '+List[i].ClassName);
  except
   on E:Exception do ShowMessage(Format('Error Loading %s : %s'#13'%s',[TComponent(List[i]).Name,E.ClassName,E.Message]));
  end;
 _ExtSection:=SaveExtSect;
 _TextOnly:=SaveTextOnly;
end;

initialization
 IniFile:=nil;
 IniOpenFile;
finalization
 IniCloseFile;
end.
Random Solutions  
 
programming4us programming4us