TCheckBox.OnClick bugs me

Posted on 2003-02-26
Medium Priority
Last Modified: 2008-02-26
I don't like the fact that the OnClick event fires when the Checked property is set to True.  If the checkbox is changed programmatically (like reading from a database), then I don't want OnClick to fire.

All truth be told, I want to do something when a user clicks a TDBCheckBox and do nothing when a field value checks it on or off from a database read from a TADOTable.  I wish TDBCheckBox would use the State property (cbChecked, cbUnchecked), because that doesn't fire an event.

Anyway, what event do I listen to?  Something in TADOConnection or the TADOTable?  Please don't tell me I have to create a descendent from TDBCheckBox; I'd like to just use Delphi's components as is, if that's okay.

Question by:Pummel
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Expert Comment

ID: 8027782
Yup - nuisance. I tend to set a flag before programmatically changing the state, and check it in the OnClick handler, so I can ignore it...

Expert Comment

ID: 8027832
  FireEvents: Boolean;
  DBCheckBox1State: TCheckBoxState;
  DBCheckBox2State: TCheckBoxState;

procedure TForm1.Create.....
  FireEvents := False
  // Give all of your tables time to load and so on...
  FireEvents := True;
  // Before you start an automatic update of the table,
  // set FireEvents := False again.

procedure TForm1.CheckBox1OnClick....
 if FireEvents then
   if CheckBox1.State = cbChecked then
    end { if CheckBox1.State = Checked }
   if CheckBox1.State = cbUnChecked then
    end; { if CheckBox1.State = UnChecked }
  end { if FireEvents = True }
  // Do nothing

// Create an object of type TCheckBoxState and assign it
// if the state is that needed.  Else just evaluate the
// values.  The above will suit your needs though, and you
// can just use regular checkboxes.
procedure TForm1.DBCheckBox1OnClick....
   DBCheckBox1State := cbChecked
   DBCheckBox1State := cbUnChecked;

Expert Comment

ID: 8028747
Try to use a boolean to work with it.
When reading from a database, make the boolean, for example, true.
if not reading from database, make it false.
At the ONCLICK event, check the boolean to exit the event or to run it.
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8028924
What you should do is use the mousedown event on the checkbox opposed to the onclick.

That way changing the checked state in code would never trigger the code that would be on the mousedown event, but when the user clicked the checkbox it would trigger the code in the mousedown event.

Author Comment

ID: 8029003
All of your answers are great so far.  But I need something a little more specific.  Here is my feedback thus far on your comments already:

andrewjb: Thank you for your idea.
cjm30305: Thank you for code relating to andrewjb's idea.
luisseixas: See bottom comment
ProDelphi: True, but OnClick is only generated when a MouseDown and MouseUp have been detected in the same control at the bookends of a MouseMove.  If a user has pressed the mouse button down, but decided he didn't want to do it, he can drag the mouse out and let go of the mouse button.  The box doesn't get checked.  Try it with almost any Windows button and you'll see that functionality in action.

How, though, should I detect that a TDataSet is reading a record and filling controls with strings and checkboxes?  I haven't found a way yet.  Also, I changed my TADOTables to just TTables, so the answer can put in terms of those.

Expert Comment

ID: 8029362
u ce use a tbale and query event

afterscroll and

and a bolean varijable nowscroling
in the event before scroll
put nowscroling:=true;

if the event after scroll
pu nowscroling:=false;

and after that just test the now scroling

good idea?

Expert Comment

ID: 8030251

If you don't want the OnClick event of a CheckBox to fire in certain situations when you set the checked property , here are some more ideas

1)Use the OnMouseUp event instead of the OnClick event

2)CheckBox1.OnClick := nil;
CheckBox1.Checked := true;
//CheckBox1Click is the name of the OnClick procedure
CheckBox1.OnClick := CheckBox1Click;

//On a Button click or something else

CheckBox1.>Tag := 0;
CheckBox1.Checked := true;
CheckBox1.Tag := 1;

On the OnClick event of the CheckBox

if CheckBox1.Tag=1 then begin
//then do whatever you want here
ShowMessage("Hi, just testing");

Just choose the one that fits you best.

Expert Comment

ID: 8030513

It sounds like you are using ADO.

how about setting the OnClick equal to nil or to a function inside of the

TCustomADODataSet.OnFieldChangeComplete event?

it looks like you can read through the fields which are being changed and see if the field is one of the 'checkbox' fields... maybe see if it's a type of boolean? and if it is, then set that checkbox.OnClick := checkbox_EventHandler;

just a suggestion.


Accepted Solution

ProDelphi earned 216 total points
ID: 8030550
So why couldn't you use the mouseup event?

Author Comment

ID: 8037146
I ALWAYS thought the MouseUp event fired even if another control received MouseDown, then the user moved the mouse, then let go of the mouse button when above another control.  Bad assumption.

But, hey, everyone who tried to help really did a good job.  I appreciate the time and effort you put in to writing code samples and stuff.  Thank you all.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question