[Gambas-user] Check Boxes (a bug?)
T Lee Davidson
t.lee.davidson at gmail.com
Fri Jun 11 14:56:03 CEST 2021
On 6/11/21 4:33 AM, Bruce Steers wrote:
> To stop click events triggering try something like this...
>
> Object.Lock(ChkAflag_CM)
> ChkAflag_CM.Value = myAflag_CM
> Object.UnLock(ChkAflag_CM)
>
That's one way to skin a cat.
There is also another solution that, although a bit more involved, could, IMO, simplify the code somewhat. I don't have time at
the moment to present full example code, but it involves the following principles.
1. Controls can be grouped. If the checkboxes are given the group, say, CheckBoxGroup, then a single event handler
(CheckBoxGroup_Click) could be used for all four.
2. Within that single event handler, the Last keyword can be used as a reference to the control that triggered the handler. The
value of the checkbox that changed would then be accessed with Last.Value.
3. The Settings component can store a Collection. For example, define the defaults as follows:
Public myAflags as Collection = ["CM": true, "MO": false, "LO": false, "ICM": false]
And, change the code to store and retrieve the flags as a collection instead of individual variables.
4. Controls have a Tag property. This could be used to tag the individual checkboxes with the appropriate string, ie, "MO",
"CM", etc.
Using these principles, all that would be needed to be done in the event handler is:
Public Sub CheckBoxGroup_Click()
Dim myAflagsString as String[]
myAflags[Last.Tag] = Last.Value ' We set the value only on the flag that changed and do not mess with the others.
For Each myFlag as String in myAflags
If myFlag Then
myAflagsString.Add(myAflags.Key)
End If
Next
LblAflags.Text = myAflagsString.Join(",")
End
I may have missed a few things and have not tested that code.
--
Lee
More information about the User
mailing list