Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 919
  • Last Modified:

TCheckBox.OnClick bugs me

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.

Thanks!
0
Pummel
Asked:
Pummel
1 Solution
 
andrewjbCommented:
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...
0
 
cjm30305Commented:
public
  FireEvents: Boolean;
  DBCheckBox1State: TCheckBoxState;
  DBCheckBox2State: TCheckBoxState;

procedure TForm1.Create.....
begin
  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.
end;

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

// 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....
begin
  if TABLENAMECOLNAME = True then
   DBCheckBox1State := cbChecked
  else
   DBCheckBox1State := cbUnChecked;
end;
0
 
luisseixasCommented:
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.
0
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!

 
ProDelphiCommented:
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.
0
 
PummelAuthor Commented:
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.
0
 
ILECommented:
u ce use a tbale and query event

afterscroll and
beforescrool

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?
0
 
BTechoCommented:
Hi.

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;


3)
//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");
end;




Just choose the one that fits you best.
0
 
BorlandManCommented:

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.

hth,
J
0
 
ProDelphiCommented:
So why couldn't you use the mouseup event?
0
 
PummelAuthor Commented:
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.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Tackle projects and never again get stuck behind a technical roadblock.
Join Now