0

I am attempting to create a License Registration addition to my program. This accesses my MySQL database, then validates the users license. This program is working so far, it can access the database and validate the license, but in an effort to make it so that the user does not have to input the license key every time, it saves the license key to a text file where in the Form_Load event, the program reads the key from there, and validates it, moving on to the next form, but as from my research, Me.Hide() doesn't work in the form_load event, where I am attempting to run it from. I have tried, and been successful with, changing the opacity to 0 or setting the form to a location way off screen, but is just not feasible, as the form is still there, just not displayed. Is there any workaround that will allow Me.Hide() in the Form_Load event? Thank you for any assistance you may provide me!

Here is my code if needed:

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
Jake
  • 99
  • 1
  • 2
  • 12
  • Sure, doesn't work. Why are you calling the form's Show() method when you don't actually want to make it visible? This code just doesn't belong in the Load event at all. Put it in the Application's Startup event handler instead. Change Me.MainForm if you want to show Form2 instead. – Hans Passant Apr 08 '15 at 18:12
  • Rather than do it this way around, why not just check if key.txt exists and only open the registration form if it doesn't? You should also look at using parameterised SQL queries too. – The Blue Dog Apr 08 '15 at 18:14
  • 1
    You can't hide the form in the `Load` event because it hasn't been shown yet. The `Load` event handles processing before the form becomes visible, therefore you can't hide it at that point because it isn't there to hide. You can add `Me.Hide()` in the form's `Shown` event if you really need to do it this way, but I suggest creating a `Sub Main()` in this case to control which form you want to show. – RianBattle Apr 08 '15 at 18:17
  • how to [start app using sub main](http://stackoverflow.com/a/25554057/1070452) – Ňɏssa Pøngjǣrdenlarp Apr 08 '15 at 21:19

2 Answers2

1

You have to minimize it and hide it from the taskbar or a person could just pull it back up again.

Me.ShowInTaskbar = False
Me.WindowState = FormWindowState.Minimized
Viper151
  • 30
  • 8
-1

In addition to @RianBattle comment, you could set the Form.Opacity property to 0 inside your Load event-handler or in the Form's constructor to "hide" the Form visibility:

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

Restore the visibility setting the value to its max, 1.

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417