1.代码如下。。怎样解决var
i:integer;
begin
for i := 0 to CheckListbox11.Items.Count-1 do
while CheckListbox11.Checked[i]=False do
if MessageDlg('确定要删除吗?',mtConfirmation,[mbYes,mbNo],0)=mrYes then
begin
Reg:=TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',True);
Reg.DeleteValue(CheckListbox11.Items[i]);
Reg.CloseKey;
Reg.Free;
CheckListbox11.Items.Delete(i);
Edit15.Text:='';
Edit16.Text:='';
end
else Exit;
end;
2.点击后这些代码只能在注册表中写入数据值名(ValueName),而不能写入数据,请问如处解决呢??代码如下。。怎样解决var
i:integer;
begin
for i:=CheckListbox11.Items.Count-1 downto 0 do
if CheckListbox11.Checked[i] then
begin
Reg := TRegistry.Create;
Reg1 := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg1.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', True) and Reg1.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run-', True) then
begin
Reg.WriteString(CheckListbox11.Items[i], Reg.ReadString(CheckListbox11.Items[i]));
Reg1.DeleteValue(CheckListbox11.Items[i]);
end;
Reg.CloseKey;
Reg1.CloseKey;
finally
Reg.Free;
Reg1.Free;
end;
Checklistbox11.ItemIndex:=checklistbox11.Items.Count-1;
CheckListBox11.Selected[i]:=True;
end
else
exit;
end;
i:integer;
begin
for i := 0 to CheckListbox11.Items.Count-1 do
while CheckListbox11.Checked[i]=False do
if MessageDlg('确定要删除吗?',mtConfirmation,[mbYes,mbNo],0)=mrYes then
begin
Reg:=TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',True);
Reg.DeleteValue(CheckListbox11.Items[i]);
Reg.CloseKey;
Reg.Free;
CheckListbox11.Items.Delete(i);
Edit15.Text:='';
Edit16.Text:='';
end
else Exit;
end;
2.点击后这些代码只能在注册表中写入数据值名(ValueName),而不能写入数据,请问如处解决呢??代码如下。。怎样解决var
i:integer;
begin
for i:=CheckListbox11.Items.Count-1 downto 0 do
if CheckListbox11.Checked[i] then
begin
Reg := TRegistry.Create;
Reg1 := TRegistry.Create;
try
Reg.RootKey := HKEY_LOCAL_MACHINE;
Reg1.RootKey := HKEY_LOCAL_MACHINE;
if Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', True) and Reg1.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run-', True) then
begin
Reg.WriteString(CheckListbox11.Items[i], Reg.ReadString(CheckListbox11.Items[i]));
Reg1.DeleteValue(CheckListbox11.Items[i]);
end;
Reg.CloseKey;
Reg1.CloseKey;
finally
Reg.Free;
Reg1.Free;
end;
Checklistbox11.ItemIndex:=checklistbox11.Items.Count-1;
CheckListBox11.Selected[i]:=True;
end
else
exit;
end;
改为 for i := CheckListbox11.Items.Count-1 downto 0 do
这是因为你在循环里把Item删除了,后面的Item的ItemIndex都发生变化了,
从后面往前删除就没问题了。2. 把 Reg.WriteString(CheckListbox11.Items[i], Reg.ReadString(CheckListbox11.Items[i]));
改为 Reg.WriteString(CheckListbox11.Items[i], Reg1.ReadString(CheckListbox11.Items[i]));
应该是从Reg1里读取。
for i := CheckListbox11.Items.Count-1 downto 0 do
if CheckListbox11.Checked[i]=False then
procedure TForm1.FormCreate(Sender: TObject);
var
Reg: TRegistry;
str: TStrings;
i: Integer;
begin
str := TStringList.Create;
Reg := TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',True);
Reg.GetValueNames(str);
Reg.CloseKey;
CheckListBox1.Items := str;
for i := 0 to str.Count - 1 do
CheckListBox1.Checked[i] := True;
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run-',True);
Reg.GetValueNames(str);
for i := 0 to str.Count - 1 do
CheckListBox1.Items.Add(str[i]);
Reg.CloseKey;
Reg.Free;
str.Free;
for i := 0 to CheckListBox1.Items.Count - 1 do
CheckListBox1.ItemEnabled[i] := False;
end;
// 启用
procedure TForm1.Button1Click(Sender: TObject);
var
Reg: TRegistry;
s: string;
begin
Reg := TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run-',True);
s := Reg.ReadString(CheckListBox1.Items[CheckListBox1.ItemIndex]);
Reg.DeleteValue(CheckListBox1.Items[CheckListBox1.ItemIndex]);
Reg.CloseKey;
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',True);
Reg.WriteString(CheckListBox1.Items[CheckListBox1.ItemIndex], s);
Reg.CloseKey;
Reg.Free;
CheckListBox1.Checked[CheckListBox1.ItemIndex] := True;
end;
// 禁用
procedure TForm1.Button2Click(Sender: TObject);
var
Reg: TRegistry;
s: string;
begin
Reg := TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',True);
s := Reg.ReadString(CheckListBox1.Items[CheckListBox1.ItemIndex]);
Reg.DeleteValue(CheckListBox1.Items[CheckListBox1.ItemIndex]);
Reg.CloseKey;
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run-',True);
Reg.WriteString(CheckListBox1.Items[CheckListBox1.ItemIndex], s);
Reg.CloseKey;
Reg.Free;
CheckListBox1.Checked[CheckListBox1.ItemIndex] := False;
end;
// 删除
procedure TForm1.Button3Click(Sender: TObject);
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
if CheckListBox1.Checked[CheckListBox1.ItemIndex] then
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',True)
else
Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run-',True);
Reg.DeleteValue(CheckListBox1.Items[CheckListBox1.ItemIndex]);
Reg.CloseKey;
Reg.Free;
CheckListBox1.Items.Delete(CheckListBox1.ItemIndex);
end;
begin
end;
之间加上
try
...
except
end;