DelphiFAQ Home Search:

How to check or uncheck a TCheckBox without causing an OnClick event

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

Question:

My application needs to update a few checkboxes and other controls but no events shall be fired by these changes. How can I prevent those OnClick() and other events?

Answer:

Before you update the control(s), you need to ..

  • assign NIL to the event handler property for each control
  • then update the control
  • reassign the correct event handler - as shown in the code below.

    If several controls share one event handler, it would be more elegant to have a boolean flag which you temporarily set to false and the event handler(s) would check this flag before they do anything.

    procedure SetCheckBox(chk: TCheckBox; b: boolean);
    var
      NE: TNotifyEvent;
    begin { SetCheckBox }
      with chk do 
      begin 
        NE := OnClick;
        OnClick := nil; 
        Checked := b; 
        OnClick := NE
      end; { with chk } 
    end; { SetCheckBox } 
    
    You don't like the formatting? Check out SourceCoder then!

    Comments:

    2008-02-14, 09:21:36
    anonymous from Betzdorf, Luxembourg  
    yuyuy

     

     

    Email address (not necessary):

    Rate as
    Hide my email when showing my comment.
    Please notify me once a day about new comments on this topic.
    Please provide a valid email address if you select this option.
     
    It seems that you are
    from Washington, US .

    Info/ Feedback on this

    Show city and country
    Show country only
    Hide my location
    You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
    Please type in the code:
    photo Add a picture:

    Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.