tags:

views:

623

answers:

4

This is in VB6 (may also apply to VB.net)

CheckBoxes can have three states (Checked, Unchecked, Greyed).

But I'm using them to set boolean variables (MuteSound, etc.) This is definitely a value that has only two states. Unfortunately, the Checked and Unchecked don't correspond to a True/False value.

So, I can't have:

bMuteSound=ChkMute.value .......... If bMuteSound then blah blah

Two options: 1. Force the value when setting/reading the Checkbox.value to be boolean:

bMuteSound=Checkbox.value=Checked .....

Checkbox.value=ConvertBooleanToCheckBox

A: 

Does the boolean variable have a default value? If so, then just don't change the variable if the box is greyed and change it to the correct value if it isn't greyed.

theycallmemorty
+3  A: 

bMuteSound = abs(chkMute.Value)
chkMute.Value = abs(bMuteSound)

shahkalpesh
+1 I like this one because it uses the boolean conversion of the enum type (1=Checked, 0=UnChecked, 2=Grayed)
bendewey
The second line is elegant!Shouldn't the first line be:bMuteSound=- abs(chkMute.value) ?
Clay Nichols
I.e., shouldn't there be a minus sign in front of ABS in the first line? (converting Checked (1) to False (-1).At least that's what True equals in VB6.
Clay Nichols
Although... -1 or 1 will both convert to TRUE in VB as well and VB6 won' be changing anytime soon ;-)
Clay Nichols
A: 

in a general sense a checkbox could be also be greyed out and checked thus you have 4 possible cases just use 2 booleans, in terms of separation your processing code needs to know nothing of checkboxes and thus should not be passing around a greyed or non greyed state but only the active or inactive state.

Indeed the processing code should not pass round greyed/nongreyed but should pass Booleans... so you need to convert... which is kind of the point of the question... which I think you may have missed?
MarkJ
+1  A: 

Looks simple enough to me:

Dim tsValue As VbTriState

'Conversions
tsValue = -Check1.Value
Check1.Value = -tsValue
Bob