Results 1 to 9 of 9

Thread: Updating Labels Through button Click action

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2024
    Posts
    4

    Updating Labels Through button Click action

    Hello, I'm currently trying to create a create account form with two textboxes (PlayerUsername, PlayerPassword), a label (ErrorMsgLbl) and a button (CreateAccountBtn). My current aim is to get the Error label to update if the requirements for the name and password are not met. Currently I dont have any errors! Except it won't display the message if the input wrong, there's no response. Sorry this is probably a silly error. Pasted below is my code and I was wandering how to call for my functions to work because I made them private but im trying to make ti public and I feel like the errors right infront of me. This is VB 6 by the way. Any help is appreciated.

    Code:
    Imports  Microsoft.VisualBasic.ApplicationServices
    
    Public Class FrmCreateAccount
        Dim PlayerNameValid As Boolean = False
        Dim PlayerPasswordValid As Boolean = False
    
        Private Sub CreateAccountBtn_Click(sender As Object, e As EventArgs) Handles CreateAccountBtn.Click
    
            While PlayerNameValid = True And PlayerPasswordValid = True
    
                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 While
    
        End Sub
    
    
        Private Function ValidatePlayerName(PlayerUsername As String)
    
            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
    
            Return PlayerNameValid
    
        End Function
    
    
    
        Private Function ValidatePlayerPassword(PlayerPassword As String)
    
            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
    
            Return PlayerPasswordValid
    
        End Function
    
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Updating Labels Through button Click action

    Quote Originally Posted by h3llo View Post
    This is VB 6 by the way.
    No it isn't. It's VB.NET. Maybe you're targeting .NET 6 in your project, but that's very different to VB6. Thread moved to VB.NET forum.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Updating Labels Through button Click action

    You have this:
    Code:
    Dim PlayerNameValid As Boolean = False
    Dim PlayerPasswordValid As Boolean = False
    and you have this:
    Code:
    While PlayerNameValid = True And PlayerPasswordValid = True
    How are those fields ever going to be set to True? You have methods that can set them to True but they are Private and I don't see where they are called anywhere, so I don't see how they'll ever be executed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2024
    Posts
    4

    Re: Updating Labels Through button Click action

    Quote Originally Posted by jmcilhinney View Post
    You have this:
    Code:
    Dim PlayerNameValid As Boolean = False
    Dim PlayerPasswordValid As Boolean = False
    and you have this:
    Code:
    While PlayerNameValid = True And PlayerPasswordValid = True
    How are those fields ever going to be set to True? You have methods that can set them to True but they are Private and I don't see where they are called anywhere, so I don't see how they'll ever be executed.

    I'm sorry for mixing the VB Net and 6 up. Sorry again. How would you go about making the functions public or returning the value of the inputs public? I'm sorry I'm not very good at this and I'm struggling to find sources online. Sorry

  5. #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.

  6. #6

    Thread Starter
    New Member
    Join Date
    Feb 2024
    Posts
    4

    Re: Updating Labels Through button Click action

    Quote Originally Posted by themindgoblin View Post
    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.
    Thank you so much for helping me, I really appreciate you and your suggestions are far more efficient. Thanks for taking your time to help (like greatly appreciated). Your edits work great and its now responds well. Thank you again!!!!!! <3

  7. #7
    Hyperactive Member
    Join Date
    Jul 2007
    Posts
    479

    Re: Updating Labels Through button Click action

    Code:
    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
    If I am reading this correct, there is some ambiguity here or there is a logic error.

    The code it saying that there must be more than 3 characters and therefore only 4 or more should work, but you are testing for <3 not <=3. Same with more than 10. 10 is not allowed per the message label but is allowed with the test.

  8. #8
    Lively Member
    Join Date
    Jun 2023
    Posts
    79

    Re: Updating Labels Through button Click action

    No problem.

    rasinc is correct on the ambiguous errors as well. The way it's written is: Username can be 3 - 10 characters in length, and password can be 6 to 10 characters in length. You'll want to change those to <= and >= if you want those to be the rules, or edit your wording in the errors.

    I didn't really think about it before, but in both of your validity functions you can remove each line of code that sets your boolean to false. The default value is false so you only need to worry about setting it to True. It's probably got zero impact on the speed or efficiency of the program, but it's unnecessary code that could add confusion later.

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Updating Labels Through button Click action

    Code:
    Public Function ValidateEntry(Value As String, MinLength as Integer, MaxLength as Integer) As Boolean
        return Value.Length >= MinLength AndAlso Value.Length <= MaxLength
    End Function
    
    'Use:
    If !ValidateEntry(txtUserName.Text.Trim, 3, 10) Then
        ErrorMsgLbl.Text = "UserName must be between 3 and 10 characters long."
    ElseIf !ValidateEntry(txtPassword.Text.Trim, 6, 10) Then
        ErrorMsgLbl.Text = "Password must be between 6 and 10 characters long."
    Else
      ' The user & Password pass, create the account.
    End If
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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