Results 1 to 9 of 9

Thread: Updating Labels Through button Click action

Threaded View

  1. #5
    Lively Member
    Join Date
    Jun 2023
    Posts
    79

    Re: Updating Labels Through button Click action

    Code:
    Imports  Microsoft.VisualBasic.ApplicationServices
    
    
    Public Class FrmCreateAccount
    
        Private Sub CreateAccountBtn_Click(sender As Object, e As EventArgs) Handles CreateAccountBtn.Click
    
        If ValidatePlayerName(txtUsername.Text.Trim) And ValidatePlayerPassword(txtPassword.Text.Trim) Then
                ErrorMsgLbl.Text = ("Account creation successful. Please sign in")
                Me.Hide()
                FrmLogin.Show() 'this is intended to navigate the user back to the login page where they use these creditentials (im planning to write them to a file later)
            End If
        End Sub
    
        Public Function ValidatePlayerName(PlayerUsername As String) As Boolean
            Dim PlayerNameValid As Boolean = False
            If PlayerUsername.Length < 3 Then
                PlayerNameValid = False
                ErrorMsgLbl.Text = ("Username is too short. Aim to have more than 3 characters.")
            ElseIf PlayerUsername.Length > 10 Then
                PlayerNameValid = False
                ErrorMsgLbl.Text = ("Username is too long. Aim to have less than 10 characters.")
            Else
                PlayerNameValid = True
            End If
            
            ValidatePlayerName = PlayerNameValid
    
            Return ValidatePlayerName
        End Function
    
        Public Function ValidatePlayerPassword(PlayerPassword As String) As Boolean
            Dim PlayerPasswordValid As Boolean = False
            If PlayerPassword.Length < 6 Then
                PlayerPasswordValid = False
                ErrorMsgLbl.Text = ("Password is too short. Aim to have more than 6 characters.")
            ElseIf PlayerPassword.Length > 10 Then
                PlayerPasswordValid = False
                ErrorMsgLbl.Text = ("Password is too long. Aim to have less than 10 characters.")
                PlayerPasswordValid = True
            End If
    
            ValidatePlayerPassword = PlayerPasswordValid
    
            Return ValidatePlayerPassword
        End Function
    End Class
    You create the same variable at the class level and in your ValidatePlayerPassword function. I would get rid of the class level variables and just create local ones in the functions. Originally you did not call the functions anywhere, so they would never run and check the validity of the username and password. Using them as boolean and checking if they are valid is one way to do that. I wouldn't use While in your button click event, either. I would use a single check using each of your functions. To make your functions public, you just use "Public" instead of "Private". I'm not sure that is needed in this case as it's all in the same class, but switching to Public lets you access the functions from any other form or module in your code. This is helpful if you have a lot of functions that will be used all over your program. You can make a module or code file with your public functions and call them from wherever you need them.

    Looks like my indentations got screwed up, along with extra line spacing? Awesome. Sorry about that.

    You will also need to get the user input. I added generic names for text boxes, but just substitute those with whatever your actual control names are, or variables or whatever.

    I would also suggest storing your error messages to a string, and then display that string in your button click event if there are errors. The way you have it now, ErrorMsgLbl will only show the last error that occurred, which isn't a huge deal, but I would personally display all applicable errors.

    If the username is too long, and the password is too short, it will only tell the user that their password is too short. This still works, because when they use the same username but add length to their password, it will then tell them that their username is too long. Honestly I would just use a single generic message from each telling the user what IS a valid entry. Something like: "Password must be between 6 - 10 characters in length." rather than in two different error messages. Same with username.
    Last edited by themindgoblin; May 22nd, 2024 at 07:30 AM. Reason: I keep forgetting things.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width