Checkbox state problem

I’ve programmed in Xojo (read RealBASIC) in the past and recently reactivated my license for a project to create a basic survey app. I’ve got a simple screen with 5 checkboxes. Each time I check a box I need to see if the number if current boxes checked exceeds the maximum allowed by the question. When the screen loads I have a property that gets set to the maximum choices allowed (intMaxChoices). On each checkbox I have an mousedown event set with the following code (the mousedown event below is from CheckBox1)

[quote] Dim intNumberChecked As Integer = 0

// Get the number if boxes already checked

If CheckBox2.State = CheckBox.CheckedStates.Checked Then
intNumberChecked = intNumberChecked +1
End If

If CheckBox3.State = CheckBox.CheckedStates.Checked Then
intNumberChecked = intNumberChecked +1
End If

If CheckBox4.State = CheckBox.CheckedStates.Checked Then
intNumberChecked = intNumberChecked +1
End If

If CheckBox5.State = CheckBox.CheckedStates.Checked Then
intNumberChecked = intNumberChecked +1
End If

// If the checkbox is “unchecked” to start.
If CheckBox1.State = CheckBox.CheckedStates.Unchecked Then
MsgBox “State Unchecked”

// Test to see if the number of boxes currently "checked" is less than the maximum allowed.
// If so set the state of the checkbox to "checked"
If intNumberChecked < intMaxChoices  Then
  MsgBox("inNumerChecked < intMaxChoices - Already checked " + Str(intNumberChecked) + " max allowed " + Str(intMaxChoices))
  CheckBox1.State = CheckBox.CheckedStates.Checked
  
  // Otherwise set the CheckBox to "unchecked".
Else 
  MsgBox("inNumerChecked >= intMaxChoices Already checked " + Str(intNumberChecked) + " max allowed " + Str(intMaxChoices))
  CheckBox1.State = CheckBox.CheckedStates.Unchecked
End If

Else // If the CheckBox is “checked”, “uncheck” it.
MsgBox “in Else”
End If[/quote]

Obvious it doesn’t work. I sure I’m making newbie error but I have no idea what it might be.

Any ideas?

Try

If Checkbox1.value = True Then
intNumberChecked = intNumberChecked +1
End If

The test and increment is function perfectly. It’s setting the state that appears to be the issue. I’m validating the test and increment with the MsgBox statements.

You should be able to set the state of the checkbox: Checkbox1.value = True

Set up a function that returns a boolean. In that function count the number of checkboxes, and return true if this checkbox is allowed to be checked, false if too many are checked. In the checkbox Activate event, run the function as:

me.setboolean(tooManyCheckedFunction())

Haven’t tested it to see if it goes wild because changing the value programmatically also calls the Activate function (maybe use MouseDown instead?) But that should open itself up to being expandable so you can use it across multiple questions. Subclass Checkbox and make some adjustments like a maximum count property, and a question id property to make building questions even faster and easier.

Thanks Ken. Value vs. State works. According to the docs State should work but I suspect State only works in an object other than the one being executed upon. Hi Ho, Hi Ho…

you’re forgetting to return true or false as appropriate
http://documentation.xojo.com/index.php/Checkbox.MouseDown

// Test to see if the number of boxes currently "checked" is less than the maximum allowed.
// If so set the state of the checkbox to "checked"
If intNumberChecked < intMaxChoices Then
MsgBox("inNumerChecked < intMaxChoices - Already checked " + Str(intNumberChecked) + " max allowed " + Str(intMaxChoices))
CheckBox1.State = CheckBox.CheckedStates.Checked
return true 
// Otherwise set the CheckBox to "unchecked".
Else 
MsgBox("inNumerChecked >= intMaxChoices Already checked " + Str(intNumberChecked) + " max allowed " + Str(intMaxChoices))
CheckBox1.State = CheckBox.CheckedStates.Unchecked
return true
End If

Thanks Norman. That’s what I missed. It wasn’t the State vs. Value but if the control event was processed or not. I didn’t need to set the value just decide (through returning True or False) IF the event was going to be processed. Duh.