gpt4 book ai didi

delphi - TRadioGroup : Click on current checked item twice to uncheck it. 可以吗?

转载 作者:行者123 更新时间:2023-12-02 06:52:32 25 4
gpt4 key购买 nike

我搜索后找不到答案:

我的 radio 组中有四个项目,其中三个是汽车名称,第四个项目我称之为“无”。当单击第四个项目时,它将索引设置为 -1。到目前为止效果很好。我想要一个功能,如果我单击一个已经选中的项目(activeindex),它会将其设置为-1。这样,我可以从列表中删除第四个“无”项目。这在 radio 组中可能吗?

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

谢谢!!

最佳答案

标准单选按钮的行为与您描述的不同。一旦选中一个单选按钮,它就会保持选中状态,直到选中同一组中的另一个单选按钮。

但是,仍然可以通过一些手动工作来实现您所要求的内容:

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;

如果您想支持多个 radio 组,可以进一步调整代码:

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 来代替,其中包含第四个项目,用于不选择任何内容。单个 TComboBox 比多个单选按钮占用更少的空间,并且它仍然提供用户期望的单选行为。

关于delphi - TRadioGroup : Click on current checked item twice to uncheck it. 可以吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16864680/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com