Question Checkbox's event is acting strange

marrcko

New member
Joined
Mar 24, 2015
Messages
2
Programming Experience
1-3
Hi,
Well, By saying it's acting strange I meant: I have 2 simple checkboxes cDeprecated and cOnHold. cOnHold cannot be set True if cDeprecated already are True. WHAT part is working great on all events (click, keypress and etc.). However the problem starts, when I try to check cOnHold to be True when cDeprecated is False - cOnHold cannot be checked at all by clicking it or by pressing space (by pressing space you can see how checkbox is checked and then unchecked)
Here's code:
Click event
Code:
Private Sub cOnHold_MouseClick(sender As Object, e As MouseEventArgs) Handles cOnHold.MouseClick
        If (cOnHold.Checked = False) Then
            ToogleOnHold()
        Else : cOnHold.Checked = False
        End If
    End Sub
keyPress event
Code:
Private Sub cOnHold_KeyPress(sender As Object, e As KeyPressEventArgs) Handles cOnHold.KeyPress
        If (cOnHold.Checked = False) Then
            ToogleOnHold()
        Else : cOnHold.Checked = False
        End If


    End Sub
Method
Code:
Public Sub ToogleOnHold()
        If (cDeprecated.Checked = True) Then
            cOnHold.Checked = False
            MessageBox.Show("This requirement is already " + cDeprecated.Text + " and cannot be " + cOnHold.Text + "!")
        Else
            cOnHold.Checked = True
        End If
    End Sub
P.S. I forgot to mention that theses checboxes are DevExpress checkEdit
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,171
Location
Sydney, Australia
Programming Experience
10+
The "proper" way to do this is to disable cOnHold when cDeprecated is checked. It's dead easy then.
Private Sub cDeprecated_CheckedChanged(sender As Object, e As EventArgs) Handles cDeprecated.CheckedChanged
    cOnHold.Checked = False
    cOnHold.Enabled = Not cDeprecated.Checked
End Sub
That's all you need. That was written for standard CheckBox controls so the members may be slightly different in your case but you get the idea. The second box can always be cleared whenever the first box changes and the the second box is enabled if and only if the first box is cleared.
 
Top Bottom