Lazzer555
Lazzer555

Reputation: 11

Visual Basic, Opening forms

I am trying to make a program that requires the user to sign in and if the right username and password is entered then open a form for that user. The way I have it done just now is the username is stored in a variable and I was wondering how I can use the text from the variable in the form name for example:

UserName = User1 then

FrmUser1.show

UserName = JohnSmith then

FrmJohnSmith.show

Here is an excerpt from the code I have so far:

    Dim Path As String = "Account_File.text "
    Dim Read_File() As String = File.ReadAllLines(Path)
    Dim NoOfLines As Integer = Read_File.Length
    Dim UserName(NoOfLines) As String
    Dim Password(NoOfLines) As String
    Dim LastNonEmpty As Integer = -1
    Dim InputUserName As String = ""
    Dim InputPassword As String = ""

    If TxtUserName.Text = "" Then
        MsgBox("Please enter a username.")
        GoTo InvalidDetails
    Else
        InputUserName = TxtUserName.Text
    End If

    If TxtPassword.Text = "" Then
        MsgBox("Please enter a Password.")
        GoTo InvalidDetails
    Else
        InputPassword = TxtPassword.Text
    End If

    For i = 0 To NoOfLines
        Dim SplitString() As String = Split(Read_File(i))
        For j As Integer = 0 To SplitString.Length - 1
                If SplitString(j) <> "" Then
                LastNonEmpty += 1
                SplitString(LastNonEmpty) = SplitString(j)
            End If
        Next
        ReDim Preserve SplitString(LastNonEmpty)
        LastNonEmpty = -1
        UserName(i) = SplitString(0) 
        Password(i) = SplitString(1)
    Next

    For i = 0 To NoOfLines
        If UserName(i) = InputUserName And Password(i) = InputPassword Then
            frm.show()
        Else
            MsgBox("Either the username or password is incorrect.")
        End If
    Next

Upvotes: 1

Views: 104

Answers (1)

Hans Passant
Hans Passant

Reputation: 942256

Some sample code:

    Dim formname = "Form" + TxtUserName.Text
    Dim typename = Me.GetType().Namespace + "." + formname
    Dim type = Me.GetType().Assembly.GetType(typename)
    If type IsNot Nothing Then
        Dim form = CType(Activator.CreateInstance(type), Form)
        form.Show()
    End If

Which assumes that all forms live in the same assembly and have the same namespace. Do keep in mind that your customer isn't very likely to be thrilled to have to call you every single time he gets a new employee. Or for that matter is going to like you asking for username + password without providing the kind of security guarantees that a Windows logon already provides. Don't do this.

Upvotes: 2

Related Questions