我搜索并找不到此问题的答案:
我在无线电组中有四项,其中三项是汽车名称,第四项是我所谓的“无”。单击第四个项目时,它将索引设置为-1。到目前为止,这工作正常。我想要一个功能,如果我点击一个已经检查过的项目(activeindex),它会将它设置为-1。这样,我可以从列表中删除第四个“无”项。这在radiogroup中可能吗?
这应该仅在单击的项目已经是索引(旧索引)并且它将被取消选中时才有效。如果单击未选中的项目,它仍会将索引设置为该项目。
谢谢!!
我搜索并找不到此问题的答案:
我在无线电组中有四项,其中三项是汽车名称,第四项是我所谓的“无”。单击第四个项目时,它将索引设置为-1。到目前为止,这工作正常。我想要一个功能,如果我点击一个已经检查过的项目(activeindex),它会将它设置为-1。这样,我可以从列表中删除第四个“无”项。这在radiogroup中可能吗?
这应该仅在单击的项目已经是索引(旧索引)并且它将被取消选中时才有效。如果单击未选中的项目,它仍会将索引设置为该项目。
谢谢!!
标准单选按钮的行为不像您描述的那样。选中单选按钮后,将一直选中,直到选中同一组中的另一个单选按钮。
但是,仍然可以通过一些手动工作来实现您的要求:
type
TMyForm = class(TForm)
RadioGroup1: TRadioGroup;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
OriginalWndProcs: array[0..2] of TWndMethod;
procedure RadioButtonWndProc1(var Message: TMessage);
procedure RadioButtonWndProc2(var Message: TMessage);
procedure RadioButtonWndProc3(var Message: TMessage);
procedure RadioButtonWndProc(const Index: Integer; var Message: TMessage);
public
{ Public declarations }
end;
procedure TMyForm.RadioButtonWndProc1(var Message: TMessage);
begin
RadioButtonWndProc(0, Message);
end;
procedure TMyForm.RadioButtonWndProc2(var Message: TMessage);
begin
RadioButtonWndProc(1, Message);
end;
procedure TMyForm.RadioButtonWndProc3(var Message: TMessage);
begin
RadioButtonWndProc(2, Message);
end;
procedure TMyForm.FormShow(Sender: TObject);
var
NewWndProcs: array[0..2] of TWndMethod;
I: Integer;
begin
NewWndProcs[0] := RadioButtonWndProc1;
NewWndProcs[1] := RadioButtonWndProc2;
NewWndProcs[2] := RadioButtonWndProc3;
for I := 0 to 2 do
begin
OriginalWndProcs[I] := RadioGroup1.Buttons[I].WindowProc;
RadioGroup1.Buttons[I].WindowProc := NewWndProcs[I];
end;
end;
procedure TMyForm.RadioButtonWndProc(const Index: Integer; var Message: TMessage);
begin
if (Message.Msg = CN_COMMAND) and
(TWMCommand(Message).NotifyCode = BN_CLICKED) and
(RadioGroup1.Buttons[Index].Checked) then
begin
RadioGroup1.Buttons[Index].Checked := False;
Exit;
end;
OriginalWndProcs[Index](Message);
end;
更新:上面的代码可以简化一点,通过TRadioButton
直接传递对象RadioButtonWndProc()
而不需要使用中间代理方法:
type
TMyForm = class(TForm)
RadioGroup1: TRadioGroup;
procedure FormShow(Sender: TObject);
private
{ Private declarations }
OriginalWndProcs: array[0..2] of TWndMethod;
procedure RadioButtonWndProc(var Message: TMessage);
public
{ Public declarations }
end;
procedure TMyForm.FormShow(Sender: TObject);
var
I: Integer;
Btn: TRadioButton;
M: TWndMethod;
begin
for I := 0 to 2 do
begin
Btn := RadioGroup1.Buttons[I];
Btn.Tag := I;
OriginalWndProcs[I] := Btn.WindowProc;
M := RadioButtonWndProc;
TMethod(M).Data := Btn; // <-- makes Self in RadioButtonWndProc() point to the Button instead of the Form...
Btn.WindowProc := M;
end;
end;
procedure TMyForm.RadioButtonWndProc(var Message: TMessage);
var
Btn: TRadioButton;
begin
Btn := TRadioButton(Self);
if (Message.Msg = CN_COMMAND) and
(TWMCommand(Message).NotifyCode = BN_CLICKED) and
(Btn.Checked) then
begin
Btn.Checked := False;
Exit;
end;
MyForm.OriginalWndProcs[Btn.Tag](Message); // <-- note, using the global Form pointer to reach Form members...
end;
如果您想支持多个无线电组,可以进一步调整代码:
type
PRadioButtonInfo = ^TRadioButtonInfo;
TRadioButtonInfo = record
OriginalWndProc: TWndMethod;
end;
TRadioGroupInfo = record
ButtonInfo: array of TRadioButtonInfo;
end;
TMyForm = class(TForm)
RadioGroup1: TRadioGroup;
RadioGroup2: TRadioGroup;
// as many RadioGroups as you want...
procedure FormShow(Sender: TObject);
private
{ Private declarations }
GroupInfo: array of TRadioGroupInfo;
procedure PrepareRadioGroup(GroupIndex: Integer; RadioGroup: TRadioGroup);
procedure RadioButtonWndProc(var Message: TMessage);
public
{ Public declarations }
end;
procedure TMyForm.FormShow(Sender: TObject);
begin
SetLength(GroupInfo, 2); // as many groups as you need to have this non-standard behavior...
PrepareRadioGroup(0, RadioGroup1);
PrepareRadioGroup(1, RadioGroup2);
// and so on...
end;
procedure TMyForm.PrepareRadioGroup(GroupIndex: Integer; RadioGroup: TRadioGroup);
var
I: Integer;
Btn: TRadioButton;
M: TWndMethod;
begin
with GroupInfo[GroupIndex] do
begin
SetLength(ButtonInfo, RadioGroup.Items.Count);
for I := 0 to Length(ButtonInfo)-1 do
begin
Btn := RadioGroup.Buttons[I];
ButtonInfo[I].OriginalWndProc := Btn.WindowProc;
Btn.Tag := NativeInt(@ButtonInfo[I]); // <-- or Longint prior to XE2
M := RadioButtonWndProc;
TMethod(M).Data := Btn;
Btn.WindowProc := M;
end;
end;
end;
procedure TMyForm.RadioButtonWndProc(var Message: TMessage);
var
Btn: TRadioButton;
begin
Btn := TRadioButton(Self);
if (Message.Msg = CN_COMMAND) and
(TWMCommand(Message).NotifyCode = BN_CLICKED) and
(Btn.Checked) then
begin
Btn.Checked := False;
Exit;
end;
PRadioButtonInfo(Btn.Tag).OriginalWndProc(Message);
end;
另一种方法是对TRadioGroup
对象进行子类化,而不是对单个TRadioButton
对象进行子类化:
type
TMyForm = class(TForm)
RadioGroup1: TRadioGroup;
RadioGroup2: TRadioGroup;
// as many RadioGroups as you want...
procedure FormShow(Sender: TObject);
private
{ Private declarations }
OriginalWndProcs: array of TWndMethod;
procedure PrepareRadioGroup(GroupIndex: Integer; RadioGroup: TRadioGroup);
procedure RadioGroupWndProc(var Message: TMessage);
public
{ Public declarations }
end;
procedure TMyForm.FormShow(Sender: TObject);
begin
SetLength(OriginalWndProcs, 2); // as many groups as you need to have this non-standard behavior...
PrepareRadioGroup(0, RadioGroup1);
PrepareRadioGroup(1, RadioGroup2);
// and so on...
end;
procedure TMyForm.PrepareRadioGroup(GroupIndex: Integer; RadioGroup: TRadioGroup);
var
I: Integer;
M: TWndMethod;
begin
RadioGroup.Tag := GroupIndex;
OriginalWndProcs[GroupIndex] := RadioGroup.WindowProc;
M := RadioGroupWndProc;
TMethod(M).Data := RadioGroup;
RadioGroup.WindowProc := M;
end;
procedure TMyForm.RadioGroupWndProc(var Message: TMessage);
var
Grp: TRadioGroup;
Ctl: TWinControl;
Btn: TRadioButton;
begin
Grp := TRadioGroup(Self);
if (Message.Msg = WM_COMMAND) and
(TWMCommand(Message).NotifyCode = BN_CLICKED) then
begin
Ctl := FindControl(TWMCommand(Message).Ctl);
if Ctl is TRadioButton then
begin
Btn := TRadioButton(Ctl);
if Btn.Checked then
begin
Btn.Checked := False;
Exit;
end;
end;
end;
MyForm.OriginalWndProcs[Grp.Tag](Message);
end;
现在,说了这么多,我一开始就不推荐这种 UI 设计。这根本不是用户期望单选按钮的行为方式。更好的 UI 选择是使用 aTComboBox
代替,其中包含第 4 项用于不选择任何内容。与多个单选按钮相比,一个TComboBox
单选按钮占用的空间更少,并且它仍然提供用户期望的单选行为。