Ignore:
Timestamp:
Aug 7, 2019, 12:04:53 AM (5 years ago)
Author:
chronos
Message:
  • Added: Allow to add, edit and delete registry values.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Registry/Demo/UFormMain.pas

    r538 r539  
    3131    MenuItem5: TMenuItem;
    3232    MenuItem6: TMenuItem;
    33     PopupMenu1: TPopupMenu;
     33    PopupMenuValue: TPopupMenu;
    3434    Splitter1: TSplitter;
    3535    StatusBar1: TStatusBar;
    3636    TreeView1: TTreeView;
     37    procedure AConnectionAddExecute(Sender: TObject);
    3738    procedure AValueAddExecute(Sender: TObject);
     39    procedure AValueDeleteExecute(Sender: TObject);
     40    procedure AValueEditExecute(Sender: TObject);
    3841    procedure FormShow(Sender: TObject);
    3942    procedure ListView1Data(Sender: TObject; Item: TListItem);
     43    procedure ListView1DblClick(Sender: TObject);
    4044  private
    41     function RegValueToString(Name: string): string;
     45    function RegValueToString(ValueName: string): string;
    4246    procedure LoadNode(Node: TTreeNode; Key: TRegKey);
    4347    procedure ReloadTreeNode(Node: TTreeNode; Reg: TGeneralRegistry);
     
    5054  FormMain: TFormMain;
    5155
     56
    5257implementation
    5358
    5459uses
    55   UCore;
     60  UCore, UFormValue;
    5661
    5762{$R *.lfm}
     
    8085end;
    8186
     87procedure TFormMain.ListView1DblClick(Sender: TObject);
     88begin
     89  AValueEdit.Execute;
     90end;
     91
    8292procedure TFormMain.FormShow(Sender: TObject);
    8393begin
     
    8696end;
    8797
     98
     99procedure TFormMain.AConnectionAddExecute(Sender: TObject);
     100begin
     101end;
     102
     103procedure TFormMain.AValueDeleteExecute(Sender: TObject);
     104var
     105  I: Integer;
     106begin
     107  if MessageDlg('Delete value', 'Do you want to delete value?', mtConfirmation,
     108    [mbCancel, mbOk], 0) = mrOk then begin
     109      for I := ListView1.Items.Count - 1 downto 0 do
     110      if ListView1.Items[I].Selected then begin
     111        ListView1.Items[I].Selected := False;
     112        Core.ActiveRegistry.DeleteValue(ListView1.Items[I].Caption);
     113      end;
     114      ReloadValues;
     115    end;
     116end;
     117
    88118procedure TFormMain.AValueAddExecute(Sender: TObject);
    89 begin
    90 //  Core.ActiveRegistry.Write;
    91 end;
    92 
    93 function TFormMain.RegValueToString(Name: string): string;
     119var
     120  FormValue: TFormValue;
     121begin
     122  FormValue := TFormValue.Create(nil);
     123  FormValue.EditName.Text := 'New value';
     124  FormValue.EditName.Enabled := True;
     125  FormValue.SetValueType(vtString);
     126  if FormValue.ShowModal = mrOk then begin
     127    if FormValue.GetValueType = vtString then
     128      Core.ActiveRegistry.WriteString(FormValue.EditName.Text, FormValue.EditValue.Text)
     129    else if FormValue.GetValueType = vtInteger then
     130      Core.ActiveRegistry.WriteInteger(FormValue.EditName.Text, FormValue.SpinEditValue.Value)
     131    else if FormValue.GetValueType = vtBoolean then
     132      Core.ActiveRegistry.WriteBool(FormValue.EditName.Text, FormValue.CheckBoxValue.Checked)
     133    else raise Exception.Create('Unsupported type ' + RegValueTypeName[TRegValueType(FormValue.ComboBoxType.ItemIndex)]);
     134    ReloadValues;
     135  end;
     136  FormValue.Free;
     137end;
     138
     139procedure TFormMain.AValueEditExecute(Sender: TObject);
     140var
     141  FormValue: TFormValue;
     142  ValueInfo: TRegValueInfo;
     143  PrevValueType: TRegValueType;
     144begin
     145  FormValue := TFormValue.Create(nil);
     146  FormValue.EditName.Text := ListView1.Selected.Caption;
     147  FormValue.EditName.Enabled := False;
     148  Core.ActiveRegistry.GetValueInfo(ListView1.Selected.Caption, ValueInfo);
     149  PrevValueType := ValueInfo.ValueType;
     150  FormValue.SetValueType(ValueInfo.ValueType);
     151  if PrevValueType = vtString then
     152    FormValue.EditValue.Text := Core.ActiveRegistry.ReadString(ListView1.Selected.Caption)
     153  else if PrevValueType = vtInteger then
     154    FormValue.SpinEditValue.Value := Core.ActiveRegistry.ReadInteger(ListView1.Selected.Caption)
     155  else if PrevValueType = vtBoolean then
     156    FormValue.CheckBoxValue.Checked := Core.ActiveRegistry.ReadBool(ListView1.Selected.Caption);
     157  if FormValue.ShowModal = mrOk then begin
     158    if FormValue.GetValueType <> PrevValueType then
     159      Core.ActiveRegistry.DeleteValue(FormValue.EditName.Text);
     160    if FormValue.GetValueType = vtString then
     161      Core.ActiveRegistry.WriteString(FormValue.EditName.Text, FormValue.EditValue.Text)
     162    else if FormValue.GetValueType = vtInteger then
     163      Core.ActiveRegistry.WriteInteger(FormValue.EditName.Text, FormValue.SpinEditValue.Value)
     164    else if FormValue.GetValueType = vtBoolean then
     165      Core.ActiveRegistry.WriteBool(FormValue.EditName.Text, FormValue.CheckBoxValue.Checked)
     166    else raise Exception.Create('Unsupported type ' + RegValueTypeName[TRegValueType(FormValue.ComboBoxType.ItemIndex)]);
     167    ReloadValues;
     168  end;
     169  FormValue.Free;
     170end;
     171
     172function TFormMain.RegValueToString(ValueName: string): string;
    94173var
    95174  ValueType: TRegValueType;
     
    98177begin
    99178  with Core.ActiveRegistry do begin
    100     ValueType := GetValueType(Name);
     179    ValueType := GetValueType(ValueName);
    101180    case ValueType of
    102       vtBoolean: if ReadBool(Name) then Result := 'True' else Result := 'False';
    103       vtInteger: Result := IntToStr(ReadInteger(Name));
    104       vtString: Result := ReadString(Name);
    105       vtFloat: Result := FloatToStr(ReadFloat(Name));
     181      vtBoolean: if ReadBool(ValueName) then Result := 'True' else Result := 'False';
     182      vtInteger: Result := IntToStr(ReadInteger(ValueName));
     183      vtString: Result := ReadString(ValueName);
     184      vtFloat: Result := FloatToStr(ReadFloat(ValueName));
    106185      vtUnknown: Result := '?';
    107       vtText: Result := ReadString(Name);
     186      vtText: Result := ReadString(ValueName);
    108187      vtBinary: begin
    109         SetLength(Buffer, GetValueSize(Name));
    110         ReadBinaryData(Name, PByte(Buffer)^, Length(Buffer));
     188        SetLength(Buffer, GetValueSize(ValueName));
     189        ReadBinaryData(ValueName, PByte(Buffer)^, Length(Buffer));
    111190        Result := '';
    112191        for I := 0 to Length(Buffer) - 1 do begin
Note: See TracChangeset for help on using the changeset viewer.