-
Dec 10th, 2017, 07:35 AM
#1
Thread Starter
Junior Member
[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
-
Dec 10th, 2017, 08:14 AM
#2
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
Last edited by si_the_geek; Dec 10th, 2017 at 08:17 AM.
-
Dec 10th, 2017, 08:15 AM
#3
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 :-(
-
Dec 10th, 2017, 08:18 AM
#4
Thread Starter
Junior Member
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
-
Dec 10th, 2017, 08:23 AM
#5
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()
-
Dec 10th, 2017, 08:33 AM
#6
Thread Starter
Junior Member
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 !
-
Dec 10th, 2017, 08:45 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|