devxlogo

Prevent Unwanted Recursion

Prevent Unwanted Recursion

A bug affecting the Sheridan 3D Controls appears in VB 4.0a. I don’t believe it was in 4.0, and I am certain it was not in 3.0. In any case, even if you use 4.0, you should pay attention because the newer OCX may be installed already on users’ machines when your apps are distributed. I use the Sheridan command buttons quite a bit for their ability to display an icon as well as text, and I ran into this problem when I installed VB 4.0a.If the user double-clicks on a common dialog box (for example, to select a file), and the double click is physically located above a Sheridan 3D command button, the Click procedure of that command button will be fired. To test this, start a new project and place a Sheridan command button and a common dialog control on Form1. In the Click procedure of the command button, include this code:

 Private Sub SSCommand1_Click()	Const CDERR_CANCEL = &H7FF3	CommonDialog1.DialogTitle = "Open File"	CommonDialog1.filename = "*.*"	CommonDialog1.DefaultExt = ".*"	CommonDialog1.CancelError = True	CommonDialog1.Filter = "All Files (*.*)|*.*"	On Error Resume Next	CommonDialog1.Action = 1	If Err = CDERR_CANCEL Then		Exit Sub	End If	On Error GoTo 0End Sub

Run the program, click on the command button, and when the common dialog appears, move it so that the name of any file appears over the button. Double-click on the file name. The common dialog will disappear and a new one will appear, resulting from the SSCommand1_Click event firing again.The solution is to declare a static “flag” variable, FalseClick, within the button’s Click event. Then change the code in the SSCommand1_Click procedure to read

 Private Sub SSCommand1_Click()	Const CDERR_CANCEL = &H7FF3	Static FalseClick As Boolean	If FalseClick Then Exit Sub	FalseClick = True	CommonDialog1.DialogTitle = "Open File"	CommonDialog1.filename = "*.*"	CommonDialog1.DefaultExt = ".*"	CommonDialog1.CancelError = True	CommonDialog1.Filter = "All Files (*.*)|*.*"	On Error Resume Next	CommonDialog1.Action = 1	If Err = CDERR_CANCEL Then		FalseClick = False		Exit Sub	End If	On Error GoTo 0	DoEvents 'allow for recursion	FalseClick = FalseEnd Sub

The DoEvents function is required in any procedure that is so short that the last statement executes before the recursion begins. I hope this workaround can help others fix the same problem.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist