正如上面各位朋友提出的那样,你应该使用TRadioGroup,而不是TCheckListBox,否则,你的程序就会让使用者不习惯(不是在这样场合惯用的控件)。如果仅是探讨使用TCheckListBox时,如何能做到那样子,可以这么做: procedure TForm1.CheckListBox1Click(Sender: TObject); var i:integer; b:boolean; begin //记住原来的状态: b:=TCheckListBox(Sender).Checked[TCheckListBox(Sender).ItemIndex]; //清空所有的选择: for i:= 0 to TCheckListBox(Sender).Items.Count-1 do TCheckListBox(Sender).Checked[i]:=false; //将被点击的项取反: TCheckListBox(Sender).Checked[TCheckListBox(Sender).ItemIndex]:=not b; end;
radio 才是王道
CheckListBox也可以实现单选的效果,不过要写代码:procedure TForm1.CheckListBox1ClickCheck(Sender: TObject); var i,index:integer; begin index:=CheckListBox1.ItemIndex;if CheckListBox1.Selected[index] then for i:=0 to CheckListBox1.Items.Count-1 do CheckListBox1.Checked[i]:=i=index; end;
或者你可以用一组Radiobutton
procedure TForm1.CheckListBox1Click(Sender: TObject);
var i:integer;
b:boolean;
begin
//记住原来的状态:
b:=TCheckListBox(Sender).Checked[TCheckListBox(Sender).ItemIndex];
//清空所有的选择:
for i:= 0 to TCheckListBox(Sender).Items.Count-1 do
TCheckListBox(Sender).Checked[i]:=false;
//将被点击的项取反:
TCheckListBox(Sender).Checked[TCheckListBox(Sender).ItemIndex]:=not b;
end;
var
i,index:integer;
begin
index:=CheckListBox1.ItemIndex;if CheckListBox1.Selected[index] then
for i:=0 to CheckListBox1.Items.Count-1 do
CheckListBox1.Checked[i]:=i=index;
end;
但是从专业上来讲就是要用Radio的