Form.Hide() doesn't work in MyApplication_UnhandledException()

Suriv

Active member
Joined
Jul 16, 2007
Messages
33
Programming Experience
1-3
I have a strange problem:
When I place this code in ApplicationEvents.vb
VB.NET:
Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException
            'hide forms
            frmMain.Hide()

            e.ExitApplication = False

           'more code

'end
End
        End Sub

All of it works, except frmMain.Hide() doesn't work.

Does anyone knows this problem? :)
 
Well, where if frmMain defined? Or is it the name of the class of you Form? I think VB.NET uses default instances to automatically allow you to refer to a shared instance of a class through the class' name, but I am not exatcly sure how it works, nor would I suggest anyone to use it.

Try using this instead :

VB.NET:
My.Application.MainForm.Hide()
 
Thank for your reply :).

frmMain is the startup form of the application.

I've tried My.Application.MainForm.Hide(), but that doesn't work neither.
 
Strangely, I find that this block is not executed when you are in debug mode because the exception seems to be caught by the debugger first. Try to compile and run your app without the debugger (I can do that using ctrl-f5 instead of simply f5, I guess that's the default behavior) and it should work... At least, it works here, so the problem should be in your form.
 
Strange... And what does the exception say when it doesn't work? Maybe you have something in your VisibleChanged event handler or the form was disposed somewhere else... Also, if you code a message box just before the mainForm.Hide() line, is the main form apparent behind the message box?
 
All form events are empty except _Load and _Disposed (code in _Disposed: "End").

If I use this code:
VB.NET:
MsgBox("Test")
            frmMain.Hide()
frmMain doesn't show up until I click "OK".
 
Now we know the form isn't disposed until you actually hide it...

And what is the exception that is thrown when you try to hide the form anyway? What's its stack trace and message?
 
And what is the exception that is thrown when you try to hide the form anyway?
What do you mean?

The form should hide when an unhandled exception occurs in the application, but there is no exception when I try to hide the form. The form simply doesn't hide.
 
And using a messagebox right after the form.hide call, will the form show behind the messagebox. I am trying to determine that it truly is the form.hide call that's broken and not that you somehow activate, show or focus it later on.

If it truly is, look up the value of form.visible (if it's false, then the form that's shown is not the one you just hid).

If it's true try to set it wihtout going trough the hide method (just do form.visible = false) and see if it is still true.

If none of these work, there must be something that's listening on the visibility of the form, a databinding on the visible property, etc. It would be preferable to troubleshoot that code, but it may be easier to simply dispose the form and create a new one next time you want to show it...
 
VB.NET:
frmMain.Hide()
            Dim blnVisible As Boolean = frmMain.Visible
            MsgBox(blnVisible)
This outputs "False".

BUT the forms shows up anyway.

It seems like the application continues executing the code after MyApplication_UnhandledException is executed. :s
 
All I can see is that the form you are refering to as mainform is not the form that you see. The form that is shown probably is another form and the mainform is truly hidden.

Try this to see if it's the same form :

VB.NET:
MessageBox.Show(Form.ActiveForm Is frmMain)
 
Can you bring me up to date, what are you trying to accomplish with this, I am not following what this code will be used for? Basically when there is a error you want to hide your startup application??? why?

Frmmain would still need to have a object referance.

If FrmMain is the sender, the code to use is

e.hide

if it is not the sender, then you would need to get the referance from the sender prior to hiding.
 

Latest posts

Back
Top