dcsimg
Results 1 to 7 of 7

Thread: [RESOLVED] Messagebox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    21

    Resolved [RESOLVED] Messagebox

    I have written to a text file for my stock login form and it writes to the file perfectly

    I have this code for the login button :

    Code:
      Private Sub Btn_Login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Login.Click
    
            For Each line In IO.File.ReadLines("Login.dat")
                If line = Tbx_Username.Text & "|" & Tbx_Password.Text Then
                    MainMenu.Show()
                ElseIf line <> Tbx_Username.Text & "|" & Tbx_Password.Text Then
                    MsgBox("inccorrect")
                End If
    
            Next
    however I want to create a validation check so that if the data (username and password) they have entered are not in the file the user knows they have inputted their data incorrectly. However I am having trouble with the messagebox. if incorrect data which is not found in file is flagged up the msgbox incorrect comes up twice in a row which is annoying. Also even if they input correct data and the mainmenu is shown the incorrect msgbox comes up again
    Last edited by si_the_geek; Dec 10th, 2017 at 08:14 AM. Reason: fixed code tags

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,208

    Re: Messagebox

    Welcome to VBForums

    The problem is that you are showing "inccorrect" for every row that doesn't match what they entered, rather than checking that none of the rows match before showing it.

    One way you could check all rows is to add a variable for "login successful", which you set in the loop and check afterwards, eg:
    Code:
            Dim loginSuccessful as Boolean = False
            For Each line In IO.File.ReadLines("Login.dat")
                If line = Tbx_Username.Text & "|" & Tbx_Password.Text Then
                    loginSuccessful = True
                    Exit For  'there is no need to check more rows, so skip the rest
                End If
            Next
    
            If loginSuccessful Then  'no need for =True  because If tests boolean values, and acts on True
                MainMenu.Show()
            Else  'no need for ElseIf, because we want to check the opposite of the previous check
                MsgBox("inccorrect")
            End If

  3. #3
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    552

    Re: Messagebox

    You have a For Each without a Next. Assuming your actual code does include the missing Next, you will get a messagebox for every line of the file that is "incorrect". Your structure is a little awkward; try it like this:

    Code:
    For Each line as string In IO.File.ReadLines("Login.dat")
        If line = Tbx_Username.Text & "|" & Tbx_Password.Text Then
            MainMenu.Show()
        Else
            MsgBox("incorrect")
        End If
    Next
    EDIT: Too slow :-(

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    21

    Re: Messagebox

    Thanks so much both of you guys !!!! would there be a way where I can now on my main menu place a label saying "Welcome" and the user that logged in username ? to make the system more professional

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,208

    Re: Messagebox

    As you already have the username stored by itself (in Tbx_Username.Text), you can simply assign the value to another control, eg:
    Code:
            If loginSuccessful Then  
                MainMenu.WelcomeLabel.Text = "Welcome " & Tbx_Username.Text
                MainMenu.Show()

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Dec 2017
    Posts
    21

    Re: Messagebox

    That's Brill ! Really made my flower system more professional, is there a way i can give feedback to your response as you highly deserve it !

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,208

    Re: Messagebox

    I'm happy to help


    You can give feedback by clicking "rate this post" at the bottom of any posts that helped (but can't rate another post by the same person, until you've rated several others).


    As you now have this situation sorted out, could you please do us a little favour, and mark the thread as Resolved?
    (this saves time reading for those of us who like to answer questions, and also helps those who search to find answers)

    You can do it by clicking on "Thread tools" just above the first post in this thread, then "Mark thread resolved". (like various other features of this site, you need JavaScript enabled in your browser for this to work).

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width