# Visual Basic > Visual Basic 6 and Earlier >  Define Checkbox value without triggering its "click" event ?

## Couin

Hi, 

Is there a way to define a checkbox value , but without triggering its "click" event ? 

Some small example code to show what is my problem :



```
Private Sub Check1_Click()
MsgBox "Checkbox clicked" 
End Sub

Private Sub Form_Load()
Check1.Value = vbChecked
End Sub
```

I just want the checkbox appears as checked on form load (like if its value was "1 - Checked" in IDE) but without having the code in its "Click" event launched (so without habit the "Checkbox clicked" message box appaers). 

Of course, here, it is a simple example, so please do not answer me to check it in IDE and/or remove MsgBox "Checkbox clicked"  in the Click event  :Big Grin: 
The checkbox value is in reality defined after reading a parameter in an ini file, and in the click event, there are some other actions than a simple message box  :Smilie: 

Thanks  :Smilie: 
 :Duck:

----------


## dz32

I ususally do Something like:



```
dim loaded as Boolean 

Sub form_load()
     load checkboxes
    Loaded = true
End sub

Sub check1_check()
   If not loaded then exit sub
    normal stuff
End sub
```

Maybe there is a better way but Ive never looked for it.

----------


## OptionBase1

```
Dim blnIgnoreCheck1Click As Boolean

Private Sub Form_Load()
  blnIgnoreCheck1Click = False
End Sub

Private Sub Check1_Click()
  If blnIgnoreCheck1Click Then
    blnIgnoreCheck1Click = False
    Exit Sub
  End If
  
  MsgBox "Clicked with mouse, not by setting .Value"
End Sub

Private Sub Command1_Click()
  blnIgnoreCheck1Click = True
  Check1.Value = vbChecked
End Sub
```

----------


## Couin

Hi dz32 and OptionBase1

Thanks for the idea. 
It looks your solutions are similar, unless better , I will try to modify my project in this way  :Smilie:

----------


## vb6forever

FWIW:  It is normal practice to declare a Private or Global boolean (blnDirty, blnIsLoading, whatever name you want) and use that in any control event you don't want to trigger during form load.   My first line in a control event is:


```
   If blnIsLoading then Exit Sub
```

Most basic texts on VB show this FWIW.
HTH

----------


## dilettante

QBasic coders never had knowledge of data binding, which is how many of the controls were intended to be used.  Today we see a lot of "high code" procedural programming instead.  Fiddling around to bypass change events is a symptom of this.  A consequence of making unusual practices their "normal."

It's like looking at a Mad Max post-apocalyptic world with lost children "doing the tell."

----------


## Arnoutdv

I never used QB and I don't agree with your point on these specific click events.
An event for a change of state sounds fine, but raising a click event when a value is programmatically changed is a design failure.
Because to put it simply, the control is not clicked.

----------


## dilettante

These events are raised when the control has focus and the space bar pressed, no click at all.  Is that a "failure" on somebody else's part too?

----------


## VanGoghGaming

Regarding checkboxes I simply use the MouseUp event instead of click to differentiate between value changes and actual clicks. And yeah, whoever uses Tab and Space instead of mouse click gets the shaft I guess! In retrospective I see that a boolean variable might be a better approach.

----------


## Eduardo-

> An event for a change of state sounds fine, but raising a click event when a value is programmatically changed is a design failure.
> Because to put it simply, the control is not clicked.


That event was misnamed, its name should had been 'Change'.

But it is convenient to raise the event every time the value changes.

For the loading issue, the flag variable is the solution.

PS: I don't know what dilettante is talking about... data binding... what does this issue have to be with data binding?

----------


## VanGoghGaming

Probably he meant the original role of these controls was to be bound to a recordset and so the "Change" event would reflect on the underlying data.

----------


## dz32

Mark and member

https://www.youtube.com/watch?v=rn4aIinTJBQ

Still love that scene I actually just sought it out to rewatch like a month ago lol. This is what happens if you dont use data binding. Just sayn

----------


## tubus

> I never used QB and I don't agree with your point on these specific click events.
> An event for a change of state sounds fine, but raising a click event when a value is programmatically changed is a design failure.


...I don't quite understand why the _.Enabled_ property is not used for that.
If not enabled, don't trigger the event! What could be easier than that??

----------


## VanGoghGaming

I never got into the whole "Mad Max" franchise. The most memorable line that stuck with me was Tina Turner's "Bust the deal, face the wheel"!

----------


## wqweto

> QBasic coders never had knowledge of data binding, which is how many of the controls were intended to be used.  Today we see a lot of "high code" procedural programming instead.  Fiddling around to bypass change events is a symptom of this.  A consequence of making unusual practices their "normal."
> 
> It's like looking at a Mad Max post-apocalyptic world with lost children "doing the tell."


No, it's just plain wrong decision on part of MS control creators team to raise Click on setting Value.

This should never happen like most modern controls (created in the last 20 years) refrain from raising events from "code actions" and do it only for "user actions" with mouse or keyboard.

Btw, data binding was such a monumental failure in VB6, it's not even funny. No one uses DataEnvironment or any form of data binding for production quality software.

cheers,
</wqw>

----------


## PlausiblyDamp

> ...I don't quite understand why the _.Enabled_ property is not used for that.
> If not enabled, don't trigger the event! What could be easier than that??


The problem is if the item is enabled and you want to change the value then you would need to disable the item, change the value, and enable the item again. That is assuming there are no problematic side effects caused by disabling and enabling the control.

----------


## Elroy

Here's the code of a custom user control I use in my main application.  To use it, just start a new custom control, and throw this code into it ... and also throw a standard CheckBox onto this new user control, named *chk*.

For me, it's named *CheckBoxEx*, but name it whatever you like.

This isn't an exact answer to the OP's question, but it still may provide an answer.  As in the comments, it provides two new (unique) events:   *ClickFromUser* and *ClickFromCode*

All the typical CheckBox properties and events aren't implemented, but the ones I use are.  Implement whatever others you like/need.



```

'
' The main advantage of this one is that it has ClickFromUser & ClickFromCode,
' in addition to the typical Click, events.
'
'
Option Explicit
'
Event ClickFromUser()   ' New event, to detect when USER changes Value.
Event ClickFromCode()   ' New event, to detect when CODE changes Value (doesn't fire if code sets Value but isn't changing it).
Event Click()           ' This is just a passthrough of the standard click event.
'
Event KeyDown(KeyCode As Integer, Shift As Integer)
Event KeyPress(KeyAscii As Integer)
Event KeyUp(KeyCode As Integer, Shift As Integer)
Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'
Dim mbSettingValueFromCode As Boolean
'

Public Property Get Value() As CheckBoxConstants
    Value = chk.Value
End Property
Public Property Let Value(prop As CheckBoxConstants)
    mbSettingValueFromCode = True
    chk.Value = prop
    mbSettingValueFromCode = False
    PropertyChanged "Value"
End Property

Private Sub chk_Click()
    RaiseEvent Click        ' We just always raise this one.  It'll work as the typical CheckBox does.
    '
    ' And now to differentiate how it was generated.
    If mbSettingValueFromCode Then
        RaiseEvent ClickFromCode
    Else
        RaiseEvent ClickFromUser
    End If
End Sub




Public Property Get Alignment() As AlignmentConstants:      Alignment = chk.Alignment:      End Property
Public Property Get Appearance() As Long:                   Appearance = chk.Appearance:    End Property
Public Property Get BackColor() As OLE_COLOR:               BackColor = chk.BackColor:      End Property
Public Property Get Caption() As String:                    Caption = chk.Caption:          End Property
Public Property Get Enabled() As Boolean:                   Enabled = chk.Enabled:          End Property
Public Property Get Font() As Font:                     Set Font = chk.Font:                End Property
Public Property Get ForeColor() As OLE_COLOR:               ForeColor = chk.ForeColor:      End Property


Public Property Let Alignment(prop As AlignmentConstants):      chk.Alignment = prop:   PropertyChanged "Alignment":    End Property
Public Property Let Appearance(prop As Long):                   chk.Appearance = prop:  PropertyChanged "Appearance":   End Property
Public Property Let BackColor(prop As OLE_COLOR):               chk.BackColor = prop:   PropertyChanged "BackColor":    End Property
Public Property Let Caption(prop As String):                    chk.Caption = prop:     PropertyChanged "Caption":      End Property
Public Property Let Enabled(prop As Boolean):                   chk.Enabled = prop:     PropertyChanged "Enabled":      End Property
Public Property Set Font(prop As Font):                     Set chk.Font = prop:        PropertyChanged "Font":         End Property
Public Property Let ForeColor(prop As OLE_COLOR):               chk.ForeColor = prop:   PropertyChanged "ForeColor":    End Property



Private Sub chk_KeyDown(KeyCode As Integer, Shift As Integer):  RaiseEvent KeyDown(KeyCode, Shift):     End Sub
Private Sub chk_KeyPress(KeyAscii As Integer):                  RaiseEvent KeyPress(KeyAscii):          End Sub
Private Sub chk_KeyUp(KeyCode As Integer, Shift As Integer):    RaiseEvent KeyUp(KeyCode, Shift):       End Sub

Private Sub chk_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single):   RaiseEvent MouseDown(Button, Shift, X, Y):  End Sub
Private Sub chk_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single):   RaiseEvent MouseMove(Button, Shift, X, Y):  End Sub
Private Sub chk_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single):     RaiseEvent MouseUp(Button, Shift, X, Y):    End Sub



Private Sub UserControl_Resize() ' Keep sub-controls the same size as the UserControl.
    chk.Top = 0: chk.Left = 0: chk.Width = Width: chk.Height = Height
End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
    chk.Alignment = PropBag.ReadProperty("Alignment", vbLeftJustify)
    chk.Appearance = PropBag.ReadProperty("Appearance", 1&)
    chk.BackColor = PropBag.ReadProperty("BackColor", &H8000000F)
    chk.Caption = PropBag.ReadProperty("Caption", vbNullString)
    chk.Enabled = PropBag.ReadProperty("Enabled", True)
    Set chk.Font = PropBag.ReadProperty("Font", chk.Font)
    chk.ForeColor = PropBag.ReadProperty("ForeColor", &H80000012)
    chk.Value = PropBag.ReadProperty("Value", vbUnchecked)
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    PropBag.WriteProperty "Alignment", chk.Alignment, vbLeftJustify
    PropBag.WriteProperty "Appearance", chk.Appearance, 1&
    PropBag.WriteProperty "BackColor", chk.BackColor, &H8000000F
    PropBag.WriteProperty "Caption", chk.Caption, vbNullString
    PropBag.WriteProperty "Enabled", chk.Enabled, True
    PropBag.WriteProperty "Font", chk.Font
    PropBag.WriteProperty "ForeColor", chk.ForeColor, &H80000012
    PropBag.WriteProperty "Value", chk.Value, vbUnchecked
End Sub



```

----------


## ColinE66

Three click events is a bit overkill, no? Why not just:

Event Click(viaUser as Boolean)

----------


## wqweto

How about separate Value propery and FireClick method?

When I set my Values I dont need the control to notify me that Value has changed, I already know this as Im the one changing it :))

If I needed Control_Click handler to be executed at this time I can call it myself just by calling Control_Click procedure (I assume FireClick is not available yet:)).

----------


## Elroy

> Three click events is a bit overkill, no? Why not just:
> 
> Event Click(viaUser as Boolean)





> How about separate Value propery and FireClick method?
> 
> When I set my Values I dont need the control to notify me that Value has changed, I already know this as Im the one changing it )
> 
> If I needed Control_Click handler to be executed at this time I can call it myself just by calling Control_Click procedure (I assume FireClick is not available yet).


Pfff, you guys are something else sometimes.  If y'all want to do it differently, then do it.   :Stick Out Tongue:   That's something I did several years ago, and thought I'd share.

Ohh, and the main reason I didn't add an argument to the Click event, as I wanted the control to "drop in" for existing CheckBoxes.  A new property would have worked for that though, but I just did what I did.   :Wink:

----------


## Eduardo-

> When I set my Values I dont need the control to notify me that Value has changed, I already know this as Im the one changing it )


And if someone else changed its value by code?

----------


## VanGoghGaming

Instead of a full fledged new user control, you could just make a small event sink class to handle the troublesome "Click" event for the checkbox:

clsCheckBoxSink


```
Option Explicit

Private WithEvents m_chkCheckBoxSink As CheckBox, m_bValueFromCode As Boolean

Public Event Click()

Public Property Set SetCheckBox(chkCheckBox As CheckBox)
    Set m_chkCheckBoxSink = chkCheckBox
End Property

Public Property Get Value() As CheckBoxConstants
    Value = m_chkCheckBoxSink.Value
End Property

Public Property Let Value(chkValue As CheckBoxConstants)
    m_bValueFromCode = True
    m_chkCheckBoxSink.Value = chkValue
    m_bValueFromCode = False
End Property

Private Sub m_chkCheckBoxSink_Click()
    If Not m_bValueFromCode Then RaiseEvent Click
End Sub
```

Main Form with a checkbox (chkTest) and a command button (cmdTestCheckBox)


```
Private WithEvents chkTestSink As clsCheckBoxSink

Private Sub chkTestSink_Click()
    Debug.Print "Mouse Click!"
End Sub

Private Sub cmdTestCheckBox_Click()
    If chkTestSink.Value = vbChecked Then
        chkTestSink.Value = vbUnchecked
    Else
        chkTestSink.Value = vbChecked
    End If
End Sub

Private Sub Form_Load()
    Set chkTestSink = New clsCheckBoxSink
    Set chkTestSink.SetCheckBox = chkTest
End Sub
```

----------


## Old Goat

I have been out of the game for a while (7 - 8 years).  In MY Form Listview I have the user turn on checkboxes from a separate control to enable a manual delete process the User employs to get rid of old MDB records.

The Listview is a *****ly animal, so:

Since the Checkboxes are already enabled (visible) why can't I make the checkbox Checked=true by letting the User click on the Listview item and use the Listview_ItemClick event to check the checkbox instead of clicking on the checkbox itself?  Is it because the Listview control structure is too convoluted

----------


## Old Goat

Umm, Yep, I can.  In the MouseUp event:

Private Sub LV1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim i

    With LV1
        If .Checkboxes = True Then
            i = .SelectedItem.Index
            .ListItems.Item(i).Checked = True
        End If
    End With


End Sub

----------


## Couin

Hi friends, 

Sorry for reply delay  :Blush: 

Thanks for your ideas, I'll try to make a small project to test them  :Smilie: 
 :Duck:

----------

