PRB: Error Message "Invalid Procedure Call or Argument" While Modal Dialog or Form is Shown
ID: Q242347
|
The information in this article applies to:
-
Microsoft Visual Basic Learning, Professional, and Enterprise Editions for Windows, versions 5.0, 6.0
-
Microsoft Visual Basic Standard and Enterprise Editions, 32-bit only, for Windows, version 4.0
SYMPTOMS
If a modal dialog or form is programmatically shown in code, other executing code in the same process that calls SetFocus on a control on a dialog or form other than the one currently modal raises the following:
Run-time Error '5'
Invalid Procedure Call or Argument
This only occurs when running the application as a compiled EXE. This does not occur in the Visual Basic IDE.
CAUSE
In the case of the SetFocus call, when a modal dialog, such as a messagebox or a modal Visual Basic form is shown, any other forms and their controls are disabled. Calling SetFocus on a disabled control always generates an error of this type.
RESOLUTION
There are several workarounds that can be used:
The most elegant workaround is to first check to see if the target of the SetFocus is Enabled.
The problem can be avoided by changing the repro sample code in the Timer event from this:
Private Sub Timer1_Timer()
Command2.SetFocus
End Sub
to this:
Private Sub Timer1_Timer()
If Command2.Enabled = True Then
Command2.SetFocus
Else
Beep
End If
End Sub
This works because whenever a modal dialog or form is shown, the controls on non-modal forms in that process become disabled.
Other ways that can work around this problem include:
- Trapping the Error 5 within the procedure that actually calls the SetFocus.
- Setting a global flag that becomes True when a call to MsgBox is made and check it before you make a call to SetFocus.
STATUS
This behavior is by design.
MORE INFORMATION
Steps to Reproduce Behavior
- Start a new Standard EXE project in Visual Basic. Form1 is created by default.
- Add two Command Buttons, Command1 and Command2, and a Timer control, Timer1, to Form1.
- Add the following code to Form1:
Private Sub Command1_Click()
MsgBox "Wait until the timer control fires"
End Sub
Private Sub Timer1_Timer()
Command2.SetFocus
End Sub
- Set the Timer1.Interval property to 3000 for a three-second delay.
- Compile the project and run the newly-compiled EXE file.
- Immediately after starting the application, click Command1 and wait for the Timer control to fire its Timer Event. When the event fires, you receive the error dialog described in the SYMPTOMS section above. After dismissing this dialog and the message box, your application terminates.
Additional query words:
modally
Keywords : kbVBp kbVBp400 kbVBp500 kbVBp600 kbGrpVB kbDSupport
Version : WINDOWS:4.0,5.0,6.0
Platform : WINDOWS
Issue type : kbprb