Einzelnen Beitrag anzeigen

Satty67

Registriert seit: 24. Feb 2007
Ort: Baden
1.566 Beiträge
 
Delphi 2007 Professional
 
#5

Re: Werte aus einer Form sichern und zurückschreiben

  Alt 21. Mai 2009, 11:12
Also ich hatte da mal früher einen Weg, den ich heute nicht mehr gehen würde. Aber trifft Deine Aufgabe wohl so in etwa:
Delphi-Quellcode:
function CtrlState_To_Ini(IniFile : TIniFile; Section : String; Form : TForm): Boolean;
var
  x : Integer;
begin
  Result := FALSE;
  if (assigned(Form)) and (assigned(IniFile)) then begin

    for x := 1 to Form.ComponentCount do begin
      with Form do begin
        // TEdit
        if Components[x-1].ClassName = 'TEditthen begin
          iniFile.WriteString(Section,Components[x-1].Name,(Form.Components[x-1]as TEdit).text);
        // TCheckBox1
        end else if Components[x-1].ClassName = 'TCheckBoxthen begin
          iniFile.WriteBool(Section,Components[x-1].Name,(Form.Components[x-1]as TCheckBox).Checked);
        // TRadioButtion
        end else if Components[x-1].ClassName = 'TRadioButtonthen begin
          iniFile.WriteBool(Section,Components[x-1].Name,(Form.Components[x-1]as TRadioButton).Checked);
        // TRadioGroup
        end else if Components[x-1].ClassName = 'TRadioGroupthen begin
          iniFile.WriteInteger(Section,Components[x-1].Name,(Form.Components[x-1]as TRadioGroup).ItemIndex);
        // TTrackBar
        end else if Components[x-1].ClassName = 'TTrackBarthen begin
          iniFile.WriteInteger(Section,Components[x-1].Name,(Form.Components[x-1]as TTrackBar).Position);
        // THotKey
        end else if Components[x-1].ClassName = 'THotKeythen begin
          iniFile.WriteInteger(Section,Components[x-1].Name,(Form.Components[x-1]as THotKey).HotKey);
        // TSpinEdit
        end else if Components[x-1].ClassName = 'TSpinEditthen begin
          iniFile.WriteInteger(Section,Components[x-1].Name,(Form.Components[x-1]as TSpinEdit).Value);
        // TComboBox
        end else if Components[x-1].ClassName = 'TComboBoxthen begin
          iniFile.WriteInteger(Section,Components[x-1].Name,(Form.Components[x-1]as TComboBox).ItemIndex);
        end;
      end;
    end;

    Result := TRUE;
  end;
end;

function Ini_To_CtrlState(IniFile : TIniFile; Section : String; Form : TForm): Boolean;
var
  x : Integer;
  TempComponent : TComponent;
  sl : TStringList;
begin
  Result := FALSE;
  if (assigned(Form)) and (assigned(IniFile)) then begin

    // Alle Item-Namen einlesen
    sl := TStringList.Create;
    try
      iniFile.ReadSection(Section,sl);

      for x := 1 to sl.Count do begin

        TempComponent := Form.FindComponent(sl[x-1]);
        if assigned(TempComponent) then begin

          // TEdit
          if TempComponent.ClassName = 'TEditthen begin
            TEdit(TempComponent).Text := IniFile.ReadString(Section,TempComponent.Name,'');
          // TCheckBox
          end else if TempComponent.ClassName = 'TCheckBoxthen begin
            TCheckBox(TempComponent).Checked := IniFile.ReadBool(Section,TempComponent.Name,FALSE);
          // TRadioButtion
          end else if TempComponent.ClassName = 'TRadioButtonthen begin
            TRadioButton(TempComponent).Checked := IniFile.ReadBool(Section,TempComponent.Name,FALSE);
          // TRadioGroup
          end else if TempComponent.ClassName = 'TRadioGroupthen begin
            TRadioGroup(TempComponent).ItemIndex := IniFile.ReadInteger(Section,TempComponent.Name,0);
          // TTrackBar
          end else if TempComponent.ClassName = 'TTrackBarthen begin
            TTrackBar(TempComponent).Position := IniFile.ReadInteger(Section,TempComponent.Name,0);
          // THotKey (Hier Eigenwert als Default)
          end else if TempComponent.ClassName = 'THotKeythen begin
            THotKey(TempComponent).HotKey := IniFile.ReadInteger(Section,TempComponent.Name,THotKey(TempComponent).HotKey);
          // TSpinEdit
          end else if TempComponent.ClassName = 'TSpinEditthen begin
            TSpinEdit(TempComponent).Value := IniFile.ReadInteger(Section,TempComponent.Name,TSpinEdit(TempComponent).Value);
          // TComboBox
          end else if TempComponent.ClassName = 'TComboBoxthen begin
            TComboBox(TempComponent).ItemIndex := IniFile.ReadInteger(Section,TempComponent.Name,TComboBox(TempComponent).ItemIndex);
          end;

        end;
      end;
    finally
      sl.Free;
    end;

    Result := TRUE;
  end;
end;
  Mit Zitat antworten Zitat