TRadioGroup:单击当前选中的项目两次以取消选中它。可以吗?

时间:2013-05-31 19:39:51

标签: delphi

我搜索过,找不到答案:

我在一个放射组中有四个项目,其中三个是汽车名称,第四个是我称之为“无”的项目。单击第四个项目时,它将索引设置为-1。这到目前为止工作正常。我希望有一个功能,如果我点击已经检查过的项目(activeindex),它会将其设置为-1。这样,我可以从列表中删除第四个“无”项。在无线电组中这可能吗?

这应该仅在单击的项目已经是索引(旧索引)时才有效,并且将取消选中。如果单击未选中的项目,它仍会将索引设置为该项目。

谢谢!

1 个答案:

答案 0 :(得分:4)

标准单选按钮的行为与您描述的不同。选中单选按钮后,将一直检查,直到检查同一组中的另一个单选按钮。

然而,通过一些手工工作,可能可以实现您的要求:

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选择是使用TComboBox代替,其中第4项用于选择任何内容。单个TComboBox将占用较少的房地产,然后是多个单选按钮,它仍然提供用户期望的单选行为。