界面上有一个checkbox名叫cb1,我对其定义了单击操作cb1Click(sender:tobject)
在这个操作中对界面上的一系列控件作了数据显示的修改同时,我还有一个procedure,里面有这样一条语句:cb1.checked:=true;我想问的是这条语句会不会激发cb1Click事件?因为我的试验下来好像是这样的。如果这条语句真的能够激发cb1Click操作,那么请问如何才能只让cb1的checked为true而不要激发这个事件?谢谢

解决方案 »

  1.   

    不会触发这个事件的,除非你自己调用:cb1Click(nil);
      

  2.   

    我亲自试验了,我用showmessage进行试验,证明了你的说法是错的
      

  3.   

    第一,我用的版本是Delphi 2007 Second Edition,可能版本不一样
    第二,我用Delphi 2007 Second Edition这个版本建了一个checkbox,建了一个edit,建了一个button
    我点击button时,使得checkbox的属性checked发生变化,即,从true变到false,或是从false变到true.
    同时,checkbox的onclick事件里定义了edit里显示的内容,即,如果checked为true,那么edit就显示'true',
    如果checked为false,则edit就显示'false'第三,我的实验结果显示:当按下button时,edit里的内容发生了变化,在'true'和'false'之间变化着第四,如果按照我的实验结果来看,1楼的说法完全错误.第五,要说明的一点是,回答问题前最好先自己试验一下
      

  4.   

    定义一个全局变量,在那个procedure设为True,然后再cb1.checked:=true;
    这样会激发onclick事件,在onclick事件里判断那个全局变量是否为true;如果是的话,则exit;
      

  5.   

    好了,谢谢各位了第一,对于6楼的回答,我要说的是,我那句话是要说:我们要有那种科学的态度第二,我们的问题已经解决,解决方法如下:因为checkbox的checked的改变有两种方式,一种是直接调用语句:checkbox.checked:=true/false;
    还有一种是直接点击这个控件,当然前题是要有checkboxClick事件.可能还会有其他的方法,目前来讲,我只知道这两种.我的问题是这样的:我在直接点击checkbox的时候要发生一组反应,比如界面上数据的显示变化.而当我直接用第一种方法(checkbox.checked:=true)时,我要求没有什么变化,只要打个勾就行了.本以为这样的语句调用不会激发checkbox的onclick事件,但(无论是全局变量也好,局部变量也好,都试过了,详细看前面的回帖),但就是激发了,而这是我不想看到的结果.思考了一些时间后,我突然发现,这两种状态有一点区别,就是checkbox的焦点.如果是点击的话,则其焦点就应该为true,而用语句改变时,在我的程序中,checkbox的焦点不为true.这样就能区分两种情况了.最后我在checkbox的onclick事件中加入的焦点判断这一个条件,试验结果比较满意.
      

  6.   

    如果是预先写好的CHECKED属性为TRUE,那么这个属性的预定不会触发你的CLIIK事件,如果触发,说明你那个procedure事件被触发了。否则只有你单击此控件时才会触发这个事件。
      

  7.   

    The TCheckBox Delphi control displays a check box that can be on (checked) or off (unchecked). The Checked property specifies whether the check box is checked or not. When the user click the check box to change its Checked state, the OnClick event for the check box is fired. How to Change the check box's Checked property Without raising the OnClick event?
    Since there is no OnCheckedChanged event, you will probably handle the program logic dependant on the checked state of the check box in its OnClick event. 
    However, if you programmatically change the Checked property the OnClick event will be fired - even though no user iteraction took place. There are (at least) two ways to programmatically change the checked property of the check box while "disabling" the OnClick event. Remove OnClick Handler, Change Checked, Put Back the Original OnClick handler
    In Delphi for Win32 an event can have only one event handler (procedure) attached to it (even though there is a way to mimic multicast events in Delphi for Win32). The OnClick event's signature of a TCheckBox control is "type TNotifyEvent = procedure(Sender: TObject) of object;" 
    If you assign NIL to the OnClick event before you change the state of the check box, then revert to the original OnClick event handling procedure - the OnClick event will not be fired. procedure SetCheckedState(const checkBox : TCheckBox; const check : boolean) ;
    var
      onClickHandler : TNotifyEvent;
    begin
      with checkBox do
      begin
        onClickHandler := OnClick;
        OnClick := nil;
        Checked := check;
        OnClick := onClickHandler;
      end;
    end; 
    Usage of this procedure is simple: 
    //toggle Checked state
    begin
      SetCheckedState(CheckBox1, NOT CheckBox1.Checked) ;
    end;The SetCheckedState above toggles the Checked property of the CheckBox1 check box. 
    Protected Hack: ClicksDisabled := true
    Another way to stop the OnClick from executing, when you programmatically change the Checked property of a Check box, is to take advantage of the "hidden" (protected) ClicksDisabled property. 
    By looking at the TCheckBox's SetState procedure which gets executed whenever the Checked property changes, the OnClick is fired if ClicksDisabled is not true. Since ClicksDisabled is protected you cannot access it from your code. Luckily, the protected hack technique enables you to access those hidden / protected properties of a Delphi control. The accessing protected members of a component provides more info on the subject. What you need to do is to declare a simple dummy class extending the TCheckBox in the same unit where you will use the ClicksDisabled property. Once you get your hands on the ClicksDisabled, simply set it to true, change the Checked property, then set ClicksDisabled back to false (default value): typeTCheckBoxEx = class(TCheckBox) ;...with TCheckBoxEx(CheckBox1) do
    begin
      ClicksDisabled := true;
      Checked := NOT Checked;
      ClicksDisabled := false;
    end;Note: the above code toggles the Checked property of the check box named "CheckBox1" using the protected ClicksDisabled property.