Recent

Author Topic: Control of Multiple Checkboxes  (Read 2233 times)

J-G

  • Hero Member
  • *****
  • Posts: 953
Control of Multiple Checkboxes
« on: August 27, 2016, 05:00:07 pm »
I've had a fantastic few days of coding during which I've learned how to force a screen update (I searched the forum and found Application.Processmessages ) and quite a few more facilities but I've now come to an impasse with the use of TCheckBox.

Using one which was either 'checked' or not was simple enough - a click on the box changed its state without any coding but when I came to use two - which can be both [off] or mutually exclusive - I've run into all manner of problems.

First the IDE created a [change] procedure when I anticipated a [Click] and when I put code in there it seemed to be recursive, which I found by stepping through. I then forced a [Click] Procedure using the 'Events' section and again that is recursive.

The logic I've used is   
If this is [on], turn it [off], if not, turn it [on] and make sure the other is [off]
Then check again for [on] when more processing is needed.


and the code is :
Code: Pascal  [Select][+][-]
  1. procedure TEllipse_Form.E_M_PresetChange(Sender: TObject);
  2. begin
  3. end;
  4.  
  5. procedure TEllipse_Form.E_M_PresetClick(Sender: TObject);
  6. begin
  7.     If E_M_PreSet.checked then
  8.       E_M_PreSet.checked  := false
  9.   else
  10.     begin
  11.       E_M_PreSet.checked  := true;
  12.       Pre_Set_1.checked := false;
  13.     end;
  14.   if E_M_PreSet.Checked then
  15.     Get_Preset(1);
  16. end;
  17.  
  18. procedure TEllipse_Form.Pre_Set_1Change(Sender: TObject);
  19. begin
  20. end;
  21.  
  22. procedure TEllipse_Form.Pre_Set_1Click(Sender: TObject);
  23. begin
  24.   if Pre_Set_1.Checked then
  25.       Pre_Set_1.checked  := false
  26.   else
  27.     begin
  28.       Pre_Set_1.checked  := true;
  29.       E_M_PreSet.checked := false;
  30.     end;
  31.   if Pre_Set_1.Checked then
  32.     Get_Preset(2);
  33.  
  34. end;
  35.  

I've left the 'change' Procedures in to show that they are dormant.

Stepping through with a break-point at line 7, it jumps to 11 then 7 then 11 - ad-infinitum until there is a stack overflow.

I suspect that this is due to some obvious (to those in the know) 'windows method' but would appreciate pointers to my error.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Control of Multiple Checkboxes
« Reply #1 on: August 27, 2016, 05:10:16 pm »
The TCheckBox's state will already be toggled when the Click event handler is called, so don't change it in your code. What's happening is exactly what should happen if you do that.


J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Control of Multiple Checkboxes
« Reply #2 on: August 27, 2016, 06:24:31 pm »
Thanks Phil, but that doesn't allow for mutual exclusivity - or am I missing something obvious?
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Control of Multiple Checkboxes
« Reply #3 on: August 27, 2016, 06:27:09 pm »
Use radio buttons, not check boxes, for a group of 2 or more that can have only one selected.

J-G

  • Hero Member
  • *****
  • Posts: 953
Re: Control of Multiple Checkboxes
« Reply #4 on: August 27, 2016, 06:34:44 pm »
Ahhh!  Knowing which tool to use is an important lesson  :)

Since posting the question I've modified the design anyway and got around the problem but I've now used a TToggleBox (for a different control) and your first response led me to correct a similar error.

I was going to change the 'state' in the code but now see that Windows (or is it Laz) does that automatically. As soon as I removed that line the [Toggle] worked correctly.
FPC 3.0.0 - Lazarus 1.6 &
FPC 3.2.2  - Lazarus 2.2.0 
Win 7 Ult 64

 

TinyPortal © 2005-2018