Hide a form as it loads

K

Kirk Graves

Can anyone tell me why this code does not work?


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Hide()
End Sub

If runs, but the form won't hide itself. This is the main form of the
application.

to duplicate, just start a brand new windows application, double click the
default form and enter Me.Hide().

I really need this to work. Any thoughts?

Thanks

Kirk
 
H

Herfried K. Wagner [MVP]

* "Kirk Graves said:
Can anyone tell me why this code does not work?


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Hide()
End Sub

If runs, but the form won't hide itself. This is the main form of the
application.

to duplicate, just start a brand new windows application, double click the
default form and enter Me.Hide().

I really need this to work. Any thoughts?

The form is shown directly after the 'Load' event handler has been
executed, so a call to 'Me.Hide' would no do anything because the form
is not yet visible at this time.
 
T

The Grim Reaper

Kirk,

To hide a form as it loads, you need to move your initialisation code to a
different place - the constructor - Public Sub New(). For example;

Class StartApplicationHere
Sub Main
Dim gvMainForm As New Form1 ' Form initialising here while hidden
Application.Run(New Form1) ' Form shown here
End Sub
End Class

Class Form1
Sub New()
' Do initialisation here while form is hidden
End Sub
Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
' Shouldn't need to do a lot here
End Sub
End Class
_________________________________
The Grim Reaper
 
K

Kirk Graves

Herfried,

thanks for the reply. do you have a solution to my problem? I need the
main form to hide itself without ever showing to the user. I am creating a
TaskBar application and don't want the user to even see the form.

The irritating part is that there are a number of demos for this on the web,
but they all show the main form at startup.

Thanks

Kirk
 
H

Herfried K. Wagner [MVP]

* "Kirk Graves said:
thanks for the reply. do you have a solution to my problem? I need the
main form to hide itself without ever showing to the user. I am creating a
TaskBar application and don't want the user to even see the form.

Add your code to your form's constructor, or to your 'Sub Main', if you
don't even need a form in your application. You can create a 'Sub
Main', ba adding this code to your application:

\\\
Public Module Program
Public Sub Main()
...
End Sub
End Module
///

Then set the startup object (project properties window) to 'Sub Main'.
 
C

Cor Ligthert

Kirk,

I assume that you needs to show that form one time a normal situation for
this is that you use a splash form.

When you put the loading of that in the "load event" of your main form, the
splash form will first be showed. At the moment that the "load event" is
completed than will be automaticly showed your main form, something like
this

\\\
Private Sub FormMain_Load(ByVal sender As _
Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim splash As New Form2
splash.ShowDialog(Me)
splash.Dispose()
End Sub
///

I hope this helps?

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top