[.net] VB.NET - Hide form on startup

Started by
3 comments, last by Rob Loach 17 years, 11 months ago
I've been searching for this for a while now but to no avail. I'm making the move from VB6 to VB.NET 2003 and in the project I'm currently working on I have a single form that I need to have hidden on startup and then display at certain times. I've tried various things that I have managed to find. Firstly I tried the obvious Me.Hide() and Me.Visible. Then I tried moving my startup code into a Sub Main as follows:

Public Shared Sub Main()
    ''''''''''''''''''''
    ' Setup the form
    ''''''''''''''''''''
    Dim frm As New frmMain
'    frm.Hide()/frm.Visible = False
    Application.Run(frm)
End Sub

Note that the commented like is meant to show that I have tried both frm.Hide() and frm.Visible = False, but again it didn't work. Any ideas anyone? Thanks!
Progress is born from the opportunity to make mistakes.

My prize winning Connect 4 AI looks one move ahead, can you beat it? @nickstadb
Advertisement
It's a little morein evolved than that for some technical reasons.

See here.

However, my advice would simply be to delay the form creation at all until you first need it.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
I would recommend handling the "Load" event of the form and calling Hide() there. You can also manually run the application's loop instead of using Application.Run (which calls Form.Show) as such:

Public Shared Sub Main()    Dim frm As New frmMain    While frm.Created        Application.DoEvents()    End WhileEnd Sub


The form will not be shown until you call frm.Show() on it. Hope this helps!
Quote:Original post by DaWanderer
I would recommend handling the "Load" event of the form and calling Hide() there.

Sorry, I forgot to mention that I tried "Me.Hide()" and "Me.Visible = False" within both the Form_Load and the Form_Activate events, and it doesn't work.

Either way, thanks for the help both of you, I'm going to take Microsoft's and jfclavette's advice and run my application in a Main() sub and only load my form when I need to use it.

Thanks!
Progress is born from the opportunity to make mistakes.

My prize winning Connect 4 AI looks one move ahead, can you beat it? @nickstadb
In the "Shown" event, call this.Hide() .

If you don't want the form to show when the user presses ALT-Tab, you're going to have to override CreateParams:

        #region CreateParams        // Thank you goes to uavfun for this awesome finding to disable the alt tab form.        private static uint WS_POPUP = 0x80000000;        private static uint WS_EX_TOPMOST = 0x00000008;        private static uint WS_EX_TOOLWINDOW = 0x00000080;        private static uint WS_MINIMIZE = 0x20000000;        protected override CreateParams CreateParams        {            get            {                CreateParams cp = base.CreateParams;                cp.Style = unchecked((int)(WS_POPUP | WS_MINIMIZE));                cp.ExStyle = (int)WS_EX_TOPMOST + (int)WS_EX_TOOLWINDOW;                // Set location                cp.X = 100;                cp.Y = 100;                return cp;            }        }        #endregion CreateParams

This is a snippet of code from ScreenCap.
Rob Loach [Website] [Projects] [Contact]

This topic is closed to new replies.

Advertisement