Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 更改复选框状态而不调用OnClick事件_Delphi_C++builder_Tcheckbox - Fatal编程技术网

Delphi 更改复选框状态而不调用OnClick事件

Delphi 更改复选框状态而不调用OnClick事件,delphi,c++builder,tcheckbox,Delphi,C++builder,Tcheckbox,我想知道当我改变复选框的状态时 CheckBox->Checked=false; 它调用CheckBoxOnClick事件,如何避免它?我希望有一个按钮解决方案,但您可以将当前事件存储在TNotifyEvent变量中,然后将Checkbox.OnChecked设置为nil,然后再将其还原。您可以使用类似于 if myFlag then begin ...event code... end; 如果不希望执行,请将myFlag设置为false,并在复选框状态更改后将其设置回

我想知道当我改变复选框的状态时

CheckBox->Checked=false;

它调用CheckBoxOnClick事件,如何避免它?

我希望有一个按钮解决方案,但您可以将当前事件存储在TNotifyEvent变量中,然后将Checkbox.OnChecked设置为nil,然后再将其还原。

您可以使用类似于

if myFlag then
  begin
    ...event code...
  end;

如果不希望执行,请将myFlag设置为false,并在复选框状态更改后将其设置回true。

另一个选项是使用以下插入器类更改受保护的ClicksDisable属性:

type
  THackCheckBox = class(TCustomCheckBox)
  end;

procedure TCheckBox_SetCheckedNoOnClick(_Chk: TCustomCheckBox; _Checked: boolean);
var
  Chk: THackCheckBox;
begin
  Chk := THackCheckBox(_Chk);
  Chk.ClicksDisabled := true;
  try
    Chk.Checked := _Checked;
  finally
    Chk.ClicksDisabled := false;
  end;
end;

CheckBox.State:=cbUnchecked
在Delphi中工作,在较新的Delphi版本中,这不会触发
onClickEvent
AFAIK

,您可以使用类帮助程序添加此功能:

CheckBox.SetCheckedWithoutClick(False);
通过将以下类帮助器用于VCL
TCheckBox

TCheckBoxHelper = class helper for TCheckBox
    procedure SetCheckedWithoutClick(AChecked: Boolean);
end;

procedure TCheckBoxHelper.SetCheckedWithoutClick(AChecked: Boolean);
begin
    ClicksDisabled := True;
    try
        Checked := AChecked;
    finally
        ClicksDisabled := False;
    end;
end;
为了完整起见:FMX
TCheckBox
的行为类似(触发
OnChange
)。您可以使用以下类帮助器解决此问题:

TCheckBoxHelper = class helper for TCheckBox
    procedure SetCheckedWithoutClick(AChecked: Boolean);
end;

procedure TCheckBoxHelper.SetCheckedWithoutClick(AChecked: Boolean);
var
    BckEvent: TNotifyEvent;
begin
    BckEvent := OnChange;
    OnChange := nil;
    try
        IsChecked := AChecked;
    finally
        OnChange := BckEvent;
    end;
end;
免责声明:谢谢dummzeuch的原创创意。请注意有关课堂助手的常见提示。

请尝试以下方法:

Checkbox.OnClick := nil;
try
  Checkbox.Checked := yourFlag;
finally
  Checkbox.OnClick := CheckboxClick;
end;

简单的解决方案是将onclick代码放在onmouseup事件中

另一个更简单的解决方案不是避免OnClick事件,而是修改事件处理程序,使其在数据集不响应的情况下不响应。状态为dsEdit或dsInsert,由用户触发的TDBCheckBox单击启动,例如:

procedure TForm1.chkSelectClick(Sender: TObject);
begin
  if chkSelect.Checked = True then
    if DataSource1.DataSet.State in [dsEdit,dsInsert] then
      begin
        { your event handler }
      end;
end;

使用focused属性确定控件是否已单击或选中的控件是否已在控件外部更新

如果tcheckbox.focused那么

 run the content of the method
否则


CheckBox->Checked=false在按钮上,但它甚至调用onClick事件。那我就试试看。谢谢。是的-这真的很容易。不知道为什么我没有自己做>\u这正是我想要的!!谢谢你为我祈祷。这似乎是最简单的解决办法。
skip the content