我正在为带有无线电组的对话框创建代码,作为首选项表单的一部分。我们代码的一部分是,当打开首选项表单时,单击单选按钮组,这会配置一堆内容(即,如果单选按钮处于“关闭”状态,则隐藏一堆配置内容)。
我想要知道用户何时实际单击单选按钮组,而不是在首选项对话框打开时触发单选按钮组。
所以代码看起来像这样:
(open preferences)...
rgMyGroupClick(nil)
procedure TdlgPreferences.rgMyGroupClick(Sender:TObject)
if sender <> nil then
begin
//do something useful
end;
但是当打开首选项对话框时也会执行此代码。我应该在那里放置什么,以便仅在用户实际单击按钮上的鼠标时执行?
Thanks
测试您的发件人
您可以通过两种方式测试发件人:
procedure TdlgPreferences.rgMyGroupClick(Sender:TObject)
begin
if sender = RadioButton1 then //do action
else if sender = RadioButton2 then ....
或者您可以测试发件人的类型。
procedure TdlgPreferences.rgMyGroupClick(Sender:TObject)
begin
if sender is TRadioButton then //do action
else if sender is TForm then ....
The is
关键字测试以查看对象是否属于某种类型。
请注意,测试if AObject is TObject
始终为真,因为每个对象都是从 TObject 派生的。
打字更有趣
事实是is
对对象类型和所有祖先的测试也可用于其他目的:
procedure TdlgPreferences.rgMyGroupClick(Sender:TObject)
begin
//TObject does not have a 'tag' property, but all TControls do...
if (sender is TControl) and (TControl(Sender).Tag = 10) then ....
因为短路布尔求值 http://en.wikipedia.org/wiki/Short-circuit_evaluationDelphi首先会检查(sender is TControl)
and only如果这是真的,请继续。进行后续测试(TControl(Sender).Tag = 10)
使用安全。
如果你不明白其构造TControl(Sender)
你可以阅读有关类型转换的内容。
here: http://delphi.about.com/od/delphitips2007/qt/is_as_cast.htm http://delphi.about.com/od/delphitips2007/qt/is_as_cast.htm
和这里:http://delphi.about.com/od/oopindelphi/a/delphi_oop11.htm http://delphi.about.com/od/oopindelphi/a/delphi_oop11.htm
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)