Click event runs when initializing a check box?

Click event runs when initializing a check box?

mdhutchinson
Advisor Advisor
408 Views
4 Replies
Message 1 of 5

Click event runs when initializing a check box?

mdhutchinson
Advisor
Advisor
I have an event procedure for my check box intended to be ran if the user clicks the control… but for some reason the event procedure is running when I put a check mark in the control … even before the form gets to the screen. Below are the three procedures.

Public Sub Main()
‘ this code runs first…
‘ do some stuff

UserForm1.Show ‘ fires UserForm_Initialize event

End Sub
‘*************************************

Private Sub UserForm_Initialize()
‘ do some stuff here
‘ then initialize my check box…

initMyCheckBox ‘ run sub procedure to initialize check box

End Sub
‘*************************************

Private Sub initMyCheckBox ()
' check & set status
‘ if condition is true then set the value of the check box to True

If condition Then
MyCheckBox.Value = True ‘ << runs MyCheckBox _Click event… WHY???
MyCheckBox.Enabled = False
End If
End Sub
‘*************************************
Private Sub MyCheckBox _Click()

‘ If user clicks to place a check in the check box then…
‘ ….. do some stuff
‘ else
‘ ….. do some other stuff.

If MyCheckBox.Value = True Then
‘do some stuff
Else
‘do some other stuff
End If
End Sub
0 Likes
409 Views
4 Replies
Replies (4)
Message 2 of 5

Anonymous
Not applicable
I think what you're looking for is under:
MyCheckBox _Change

(not) MyCheckBox _Click
0 Likes
Message 3 of 5

mdhutchinson
Advisor
Advisor
Well I thought ... THAT'S IT... but no...

I edited my event procedure to be MyCheckBox _Change
instead of MyCheckBox _Click...

Now the Change event is running...

All I want to do is intialize the form with a check in the box based on a condition being true... If the condition is true then I want the dialog presented to the user with a check in the control. But the click event code intended for the user click runs mistakenly ...
0 Likes
Message 4 of 5

Anonymous
Not applicable
Yes, Check_Box_Click/CheckBox_Change event fires when you programmatcially
set Check Box's value.

So, if you want to avoid code in the event handler procedure running in
UserForm_Initialize(), where you set CheckBox's value, you could declear a
flag in UserForm module level. Like this:

Private mInitialize as Boolean

Private Sub UserForm_Initial()
'Set flag to false
mInitialize=False
'...Do initializing, including set CheckBox's value
'....

'After all initialzation done, set flag to true
mInitialize=True
End Sub

Private Sub CheckBox1.Change()

''If the event is fired during initialization, exit immediately
If Not mInitialize Then Exit Sub

''Following code executed when event fired due to user action
......

End Sub

wrote in message news:5451981@discussion.autodesk.com...
I have an event procedure for my check box intended to be ran if the user
clicks the control. but for some reason the event procedure is running when
I put a check mark in the control . even before the form gets to the screen.
Below are the three procedures.

Public Sub Main()
' this code runs first.
' do some stuff

UserForm1.Show ' fires UserForm_Initialize event

End Sub
'*************************************

Private Sub UserForm_Initialize()
' do some stuff here
' then initialize my check box.

initMyCheckBox ' run sub procedure to initialize check box

End Sub
'*************************************

Private Sub initMyCheckBox ()
' check & set status
' if condition is true then set the value of the check box to True

If condition Then
MyCheckBox.Value = True ' << runs MyCheckBox _Click event. WHY???
MyCheckBox.Enabled = False
End If
End Sub
'*************************************
Private Sub MyCheckBox _Click()

' If user clicks to place a check in the check box then.
' ... do some stuff
' else
' ... do some other stuff.

If MyCheckBox.Value = True Then
'do some stuff
Else
'do some other stuff
End If
End Sub
0 Likes
Message 5 of 5

mdhutchinson
Advisor
Advisor
Thanks... that worked...

This same variable should work for any other initializing that I need when it triggers other events.

What I did in my form module

Option Explicit
Private initializing As Boolean
'------------------------------------

Private Sub UserForm_Initialize()
initializing = True
initMyCheckBox ‘ run sub procedure to initialize check box
'... any other initializing code
initializing = False
End Sub
'------------------------------------

Private Sub initMyCheckBox ()
If condition Then
MyCheckBox.Value = True
MyCheckBox.Enabled = False
End If
End Sub
'------------------------------------

Private Sub MyCheckBox _Click()
If initializing Then Exit Sub ' exit the sub if initializing
If MyCheckBox.Value = True Then
‘do some stuff
Else
‘do some other stuff
End If
End Sub
0 Likes