Hiding Form

vwiley1

New member
Joined
May 25, 2006
Messages
2
Programming Experience
Beginner
Hi Everyone,

I have not used Visual Basic since version 3. I have VB 2005 Express now.

While some things are the same, others have changed and are driving me absolutely crazy.

Here's my current problem:

The app starts by loading Form1

Form1 is basically a registration screen. The end user of my software will put in there email address and registration code. I have already programmed the functionality so it checks the email address and registration code by making a POST to a PHP Script.

If the PHP Script reply's back that it is a valid REG Code, certain registry keys are set, then Form1 should hide, and form2 should show.

The next time the software is loaded, The load statement checks if the Registry keys are in place.. If they are, Form1 should hide immediately, and form2 should show.

This is where the problem lies.. Form2 is shown, but form1 never gets hidden.

Here's the Form1_Load code


VB.NET:
Expand Collapse Copy
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Not going to type the code...but this is where it checks for registry key
If RegKey = True Then
Dim nForm As Form = New Form2
nForm.Show()
Me.Hide()
End if
End Sub

I am not exactly sure what the problem is, Why can't I hide Form1?
 
When the Load event handler is executed the form hasn't even been displayed yet, so you can't Hide it. At the end of the Load event handler the form becomes visible, no matter what went before.

What I would suggest is setting the form's WindowState property to Minimized at design time. You may also want to set its ShowInTaskbar property to False. Now the user will not see the form even if its Visible property is True. You can now do a little bit of fiddling:
VB.NET:
Expand Collapse Copy
Private hideForm As Boolean

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'This is executed BEFORE the form is loaded.
    If RegKey = True Then
        Dim nForm As Form = New Form2
        nForm.Show()
    End If

    Me.hideForm = RegKey
End Sub

Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
    'This is executed AFTER the form is loaded.
    If Me.hideForm Then
        'Really hide the form so it acnnot be Alt+Tabbed to.
        Me.Visible = False
    Else
        'Restore the form.
        Me.WindowState = FormWindowState.Normal
        Me.ShowInTaskbar = True 'Optional.
    End If
End Sub
 
Why not just use a splash screen? When the splash screen loads, it will check that there user isnt registered and bring up Form1. If the user is registered it will bring up Form2 instead.

@jmcilhinney: surely that way isnt very good? Because you'll still see the form popup?
 
UncleRonin said:
@jmcilhinney: surely that way isnt very good? Because you'll still see the form popup?
No they won't. I specifically said in my post that you set the startup form's WindowState to Minimized so that it's not seen. Once the Shown event is raised then you can decide whether to make the form totally visible by setting the WindowState to Normal or totally invisible by setting Visible to False. Try it and see for yourself.
 
Thank you to everyone who helped out. It all makes perfect sense now :)

Now all I need is a fast way to check if a net connection is present.. Currently, if there is no connection... the program just hangs for about 30 seconds. It would be nice if I could take this down to 10 seconds or so.

Before this post, the online registration check was in the Form Load, so the window wouldn't even show for 30 seconds. I have now moved that part to the Form Shown sub along with hiding the form.
 
Back
Top