- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我搜索后找不到答案:
我的 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/
我不知道该在标题中添加什么内容,但我有以下代码: @Controller public class WorkdayAddController { @Autowired private WorkdayR
已编辑:我需要用户按其全局进度 [order by avg(progression) DESC] 以及按进度排序的详细信息行进行排序。 示例: USER 1 campaign 1 progressio
我知道这是一个更“沉重”的问题,但我认为它也很有趣。它是 of my previous questions about compiler functions 的一部分, 但回过头来我解释得非常糟糕,许
我有一个函数(表单中的事件处理程序)结构如下: Dim errMsg as String = "" CheckIfValidUser(..., errMsg) If errMsg.Length > 0
我正在为客户正在举办的事件创建注册表。基本的用户详细信息被提交到第三方系统(即姓名、电子邮件等),但其余字段需要通过电子邮件发送给客户。 我需要这样的事情发生: 列出项目 用户填写表单 jquery
我有一个ClassLoader,它从源文件加载由JavaCompiler编译的类。但是当我更改源文件、保存它并重新编译它时,ClassLoader 仍然加载该类的第一个版本。 ClassLoad
我有一个使用 Kotlin 编写的 RecognitionListener 的项目。语音转文本功能一直很成功,从未出现任何问题。 从上周开始,它的 onResult 函数开始被调用两次。该项目没有进行
大家好(和圣诞快乐) 希望有人能回答我的问题,那将是一个美好的圣诞节 礼物。 我的 UIViewController 一个表,在表部分区域中有一个分段控件。看起来像这样 每次用户单击分段控件时,我都会
我正在制作一个程序,输入三个数字,然后计算一些不同的东西(每个东西都必须有自己的函数)。该程序首先告诉用户他们的选择并等待他们的输入。执行任何情况后,程序将再次打印菜单,除非它将使用默认情况,然后它将
我的 Android 应用程序有一个自定义接收器,如下所示: public class CustomReceiver extends BroadcastReceiver{ String URL; St
我有一系列带日期的事件,如下所示: 事件 1,2014 年 1 月 1 日 事件 2,2014 年 1 月 2 日 事件 3,2014 年 1 月 3 日 假设我想要按时间顺序排列的最近两个事件。如果
我对 jQuery“切换”方法有一个神秘的问题。我单击它一次,但它会设置两次动画。你自己去看看吧! Demo 页面源码贴在下面,(解决后链接会消失) html: About us S
我对Python完全陌生,当我遇到这个问题时,我正在尝试线程模块:-线程由于某种原因运行两次,我不知道为什么。我到处寻找,但没有找到任何答案。希望我能在这里得到一些帮助 import time fro
先决条件:无障碍对讲开启。 问题:当从软键盘向编辑文本输入字符时,字符被读出两次。(我想一次是通过键盘,一次是通过编辑文本)。 最佳答案 问题:视力正常的用户不能很好地理解/理解盲人用户需要的信息类型
我的问题是,当我再次对相同的值进行评分时,它不会响应第二次..这意味着当我第一次评分为“4”时,我不能将其评分为“4”第二次..只有当我评价其他值而不是'4'时它才会响应。 这是我尝试过的(我的代码中
我必须设计一个问卷系统。我们一直在讨论很多事情,包括如何持有答案,以及如何持有可能的答案。现在,尽管我不同意,但我还是屈服于我的同事。我只想知道您对此的看法以及原因。对我来说,两次保存相同的信息是一个
我将 Angular2 与路由一起使用,我的页面在初始加载时显示重复内容。单击任何链接(Page1 或 Page2)后,一切正常(不再有重复内容)。为什么我在初始加载时得到重复的内容以及我需要什么才能
我用KTX。。我有以下代码:。为什么它显示了两次?我该如何解决这个问题呢?。。预计“点击开始游戏”的标签只会显示一次。。我试着删除了代码。但它还是发生了。。然后,我尝试删除Add(LoadingSta
现象说明 maven的java项目,测试用例和main所在的源码文件均符合缺省写法和格式,但是在使用mvn clean sonar:sonar进行编译时提示can't be indexed twi
我使用 React 创建了一个简单的 Meteor 应用程序。它使用名为 client 的文件夹中的三个文件(没有其他文件)。在控制台中,应用程序打印出: withTracker rendering
我是一名优秀的程序员,十分优秀!