I have a message dialog that pops up when a user clicks a checkbox to alert the user of other values that might have changed. I put this message dialog code in the Action event of the checkbox. That works fine. However, when the user selects a new record I have to set the value of the checkbox. When I programmatically set the value of the checkbox, it triggers the Action event which fires the message dialog. I don’t want to alert the user in this situation. Is it a better idea to put the message dialog code in the MouseUp event instead? I tried that but I didn’t like the way it was behaving.
The CheckBox.Action event handler is called regardless of whether the checkbox is set by the user or in code.
If you don’t want your code to run, the typical thing to do is to add a boolean flag of some kind, perhaps called SkipUpdate. Set it to True before you set the checkbox and False after.
In the CheckBox.Action event handler, put your code in an If Not SkipUpdate Then block.
This is a (very) simple solution meant to give you some ideas.
Subclass the CheckBox and add a DisableActionEvent property as Boolean. The Action event code will look like this:
if not DisableActionEvent then
// Do stuff
The external code would look like this:
cb.DisableActionEvent = true
cd.Value = bool
cb.DisableActionEvent = false
You can consolidate this into a subclass computed property:
Get ValueWithoutAction As Boolean
Set ValueWithoutAction (value As Boolean)
me.DisableActionEvent = true
me.Value = value
me.DisableActionEvent = false
If I need to set certain controls on the main window based upon this checkbox action, is it proper to put that code in the subclass action event too? I would just prepend “MainWindow” to each control I need to set, or is there a more proper way to do this?
The subclass of a control should never refer to a specific parent (unless that specific parent is part of the control)… This defeats the entire purpose of creating a custom control via subclassing.
IF you need to refer to a control on another window , then prepend the window name in the code doing the referring, not inside the subclass itself. This allows you to re-use the subclassed control on multiple windows without needing to do anything special.
To add to what Dave advised, look at it this way. When you add the standard CheckBox to your window, it doesn’t know anything about your window or your code or how you will use it. It offers some features that allow you to customize it for your specific purpose.
When you subclass the CheckBox, your subclass will just add a few additional features, ones that you can use in any project. The subclass should still not know anything about your code or how you will use it.
Finally, you change the CheckBoxes you’ve already added to your window to your new subclass. They will work the same way, but now have these additional features. It’s within the window that you will customize the code for your purpose, and you can still use the subclass in another window or another project.
I kinda knew that wasn’t the proper way, but I just thought that I could consolidate the code by putting it all in the subclass. For example, if the user checks the checkbox, I need to enable some other control. I also need to do that same thing when the user changes records. Normally I would write this code once in the Action event of the control, but now I don’t have access to the Action event of that control because I subclassed it. So is this where I should add an Event Definition named “Action”?
Also, how do I call the Get and Set methods in my main window?
Sure you have access to the ACTION event… even if you have code in the SUBCLASS for ACTION
All you need to do is add the line
in the SUBCLASSes ACTION event…
Put it at the beginning or end depending of WHEN you want the instance to respond to the event.
In the subclass, you have to define an event, “Action”. In the subclass’ Action event, you would
RaiseEvent Action() to call the Action event of the instance in your window.
Thanks guys. I understand what Dave said, but I was looking for what Kem suggested.
One more question… I store booleans from the checkboxes as 0s and 1s (BOOL) in my database. How could I override the value property in my checkbox subclass to return 0s and 1s instead of True and False?
You cannot override properties. You would be better off adding a new read-only computed property, that converts the Value from a boolean to an integer and returns it (the Set section of the computed property is left blank to make it read-only):
Property GetValueAsInteger As Integer
If Self.Value Then
When creating a computed property, RealBasic adds a protected property with an “m” prepended to it’s name. Can these be deleted if not using them?
Yes. It does that as a convenience to you. Feel free to delete the property.
If you convert a regular property into a computed property, the original will be renamed with an “m” prefix. You can of course delete it if you don’t need it.
But if you don’t need it, you probably should have just created a new computed property (Add->Computed Property) rather than trying to convert an existing property.
Thanks for clearing that up for me Paul and Tim.