How to click a button on a vb6 form

vb6

I have a vb6 form with an ocx control on it. The ocx control has a button on it that I want to press from code. How do I do this?

I have:

Dim b As CommandButton
Set b = ocx.GetButton("btnPrint")
SendMessage ocx.hwnd, WM_COMMAND, GetWindowLong(b.hwnd, GWL_ID), b.hwnd

but it doesn't seem to work.

Best Solution

I believe the following will work:

Dim b As CommandButton
Set b = ocx.GetButton("btnPrint")
b = True

CommandButtons actually have two functions. One is the usual click button and the other is a toggle button that acts similar to a CheckBox. The default property of the CommandButton is actually the Value property that indicates whether a button is toggled. By setting the property, the Click event is generated. This is done even if the button is not styled as a ToggleButton and therefore doesn't change its state.

Related Question