我。隐藏在Form_Load VB.Net中

时间:2015-04-08 18:02:24

标签: vb.net

我正在尝试为我的程序创建许可证注册添加。这将访问我的MySQL数据库,然后验证用户许可证。到目前为止,该程序正在运行,它可以访问数据库并验证许可证,但为了使用户不必每次都输入许可证密钥,它将许可证密钥保存到文本文件中。在Form_Load事件中,程序从那里读取密钥并验证它,然后继续下一个表单,但是从我的研究中,Me.Hide()并没有在form_load事件中工作,我试图在从中运行。我已经尝试过,并且已经成功,将不透明度更改为0或将表单设置为屏幕外的位置,但是不可行,因为表单仍然存在,只是没有显示。是否有任何解决方法允许在Form_Load事件中使用Me.Hide()?感谢您提供给我的任何帮助!

如果需要,这是我的代码:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim file As System.IO.StreamWriter
    If (Not System.IO.Directory.Exists("C:\Program Files\LicenseRegistration")) Then
        System.IO.Directory.CreateDirectory("C:\Program Files\LicenseRegistration")
    End If
    file = My.Computer.FileSystem.OpenTextFileWriter("C:\Program Files\LicenseRegistration\key.txt", True)
    file.Close()
    Dim txtreader As New System.IO.StreamReader("C:\Program Files\LicenseRegistration\key.txt")
    Dim Key As String
    Key = txtreader.ReadLine()
    txtreader.Close()
    If Key = "" Then
        GoTo FirstTime
    End If

    Dim READER As MySqlDataReader
    MysqlConn = New MySqlConnection
    MysqlConn.ConnectionString =
    'MySQL Login Info Cut
   "server=;userid=;password=;database="
    Try
        MysqlConn.Open()
        Dim Query As String
        Query = "select * from sql373296.LicenseKeys where License_Key='" & Key & "' "
        COMMAND = New MySqlCommand(Query, MysqlConn)
        READER = COMMAND.ExecuteReader
        Dim count As Integer
        count = 0
        While READER.Read
            count = count + 1
        End While

        If count = 1 Then
            Me.Hide()
            Form2.Show()
        Else
            MsgBox("Your license has been banned!")
        End If
        MysqlConn.Close()
    Catch ex As MySqlException
        MsgBox(ex.Message)
    Finally
        MysqlConn.Dispose()
    End Try
End Sub

2 个答案:

答案 0 :(得分:1)

您必须将其最小化并将其隐藏在任务栏中,否则某人可能会再次将其拉回来。

Me.ShowInTaskbar = False
Me.WindowState = FormWindowState.Minimized

答案 1 :(得分:-1)

除了@ RianBattle 评论之外,您还可以在Form.Opacity事件处理程序或表单中将Load属性设置为 0 构造函数“隐藏”表单可见性:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    ' your code...

    If count = 1 Then
        Me.Opacity = 0.0R
        Form2.Show()
    Else
       ' ...
    End If

    ' your code...

End Sub

恢复可见性,将值设置为最大值 1