我有两个TSpeedButton
:btn1
和btn2
。设置它们的属性是为了使它们一起在一个组中起作用并且互斥,这意味着当按下一个按钮时,另一个按钮将自动被按下:
AllowAllUp = False
GroupIndex = 1
OnClick = onSpeedButtonsClick
我在
onSpeedButtonsClick()
中有一些代码,根据单击的两个按钮中的哪个来运行一些代码。我正在尝试做的是,如果
btn1
当前为Down
,并且用户按下此按钮,则什么也不会发生:procedure frmMyForm.onSpeedButtonsClick(Sender: TObject);
begin
{ Don't do anything if the clicked button is already currently pressed down. }
if ((Sender = btn1) and btn1.Down) or
((Sender = btn2) and btn2.Down) then
Exit();
{ ... some other code here that should only run when
the `Down` state of the buttons changes }
end;
问题是,当
btn1
当前处于关闭状态并且用户按下btn2
时,Down
的btn2
属性在执行True
之前被设置为onSpeedButtonsClick()
,因此无论什么情况,它都早于Exit()
。 。 最佳答案
只需将按钮状态存储在表单字段中,然后将其设置在事件处理程序的结尾即可(我使用位字段)
bState := Ord(btn1.Down) or (Ord(btn2.Down) shl 1);
检查:
if (bState and 1) <> 0 then
//it would be nicer to use constants like btn1down = 1 instead of magic numbers
btn1 WAS down before
if (bState and 2) <> 0 then
btn2 WAS down before