-1

I am making a program that needs to hide itself just after starting. I have been doing this I am sure in VB 2008, 2010. But in VS 2015, Me.Hide does not seem to work. I do have pretty good knowledge in VB.NET, but I just can't find a solution to this. Googled, looked in the code etc.

Here is the code:

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

Also when I put it into a timer it seems to work. But that is not my solution since I need to show the form sometimes.

triboi
  • 1
  • 1
  • 2
  • 1
    That has never worked, you'll need to avoid calling Show(). Very unclear why that is a problem, only other way is by [overriding SetVisibleCore](http://stackoverflow.com/a/3742980/17034). – Hans Passant Nov 28 '15 at 12:48
  • Uff, I am confused, how do you hide a form in VB.NET without closing it, and then show it. – triboi Nov 28 '15 at 12:49
  • 1
    If you don't want to look at it then just don't create the form in the first place. Until later, whenever you *do* want to show it. If it is the startup form of your app then the linked post shows the workaround. – Hans Passant Nov 28 '15 at 12:56

2 Answers2

4

The Me.Hide should always be utilized outside the LOAD event. You may put it at SHOWN event. But, anyway, why don't you set the property MINIMIZED=TRUE or even OPACITY=0 at design time? You will have the same effect.

Or even set the form's visibility to false at design time.

David BS
  • 1,822
  • 1
  • 19
  • 35
1

One way to accomplish this is as Hans alluded to is if you don't want the form to be visible, don't call the Show() method.

  1. Add a class to your project and add a Shared Sub Main to the class
  2. In your project's properties, turn off the application framework by unchecking the Enable application framework checkbox.
  3. In your project's properties, set the startup object to Sub Main

Use code similar to the following in Sub Main:

Public Class Program
    Public Shared Sub Main()
        Application.EnableVisualStyles()
        Application.SetCompatibleTextRenderingDefault(False)

        'This will run any code in the forms constructor (Sub New)
        'This line must appear AFTER the two lines above.
        Dim startUpForm As New Form1()

        'This will start the application without showing the form.  The
        'form won't show in the task bar either.
        'You must provide some mechanism to show the form later, such as a 
        'tray icon
        Application.Run()
    End Sub
End Class
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48