Hello HackForumers!
This is my next tutorial on how to make a basic login system with a nice little feature for registering users! I saw a few members requesting this in other topics so I thought I would just share this. It was done as a school project (Just to get us used to VB.NET) earlier on in the year. A lot of this was done by myself as I loved to get ahead and do my own features so enjoy the tutorial!
Okay so first we should always design our GUIs! Make it look the best to your own standards, don't bother caring with what other people think (Unless it was really bland..) So here is my version of my GUI.
![[Image: 2piu8.png]](https://i.imgur.com/2piu8.png)
Okay so what we need in our form is these:
3 x Forms
3 x Buttons
2 x Labels
Pretty basic isn't it?
This is the simple version of what we have. You can name these anything you like however I've decided to name mine:
Forms: Login.vb & Success.vb & Register.vb
Buttons: btnLogin & btnReg & btnClose
Labels: txtName & txtPass
Alright so onto our coding. This isn't too hard if you know about objRead.
We need to declare these variables in our btnLogin (Or Button1).
Declaring filename is just using it for objRead which is just reading the text file for the login as well as it locates the file. (It's easier this way since we just use filename from now on in the button). Next we need to declare intError which if an error of 1 happens to appear, then an error will show. Lastly we declare objRead as a StreamReader with of coarse, our declared filename! This is so we don't have to type in the filepath every time.
Next we need to be able to read the text file as well as create a loop so it reads through the text file not just one or two lines!
This just reads through or simply "peeks" onto the next line.
Now we need to add an if statement for our validation process. This is so if we have the incorrect details then we won't login! We simply start off like this:
This is saying if the txtName and txtPword is equal to the objRead then it'll read the next line of code which is this:
Me.Hide() closes the current form and shows the next form which for us is Success.Show() (You can have many forms you need but it's up to you really). We also have intError = 0 which lets the program know that there is no errors!
Now we need to finish off our loop so we know that it'll read throughout the whole text file. We simply add this code after it:
Now we need to know that if the details are correct, we need to have an error which will be displayed as:
This lets the program know that the error is equal to 1, so we need to show a message box which lets the user know that the login details are incorrect.
Lastly we need to close the objRead so the program doesn't infinite loop and eventually crash! We do this by adding:
Also add for your register button:
This is so we can do our next part of registering new users!
Now that was the first form out of two for coding (The third is only just a welcome screen which I couldn't be bothered using). If the login details are correct then my Success form will show like so:
![[Image: ArzUy&x2V2al&2piu8]](https://i.imgur.com/x2V2a.pnghttp://imgur.com/ArzUy&x2V2al&2piu8)
Now it's time for coding the register details for the program!
First we should design the register form, so mine looks like this:
![[Image: ArzUy.png]](https://i.imgur.com/ArzUy.png)
I simply have:
2 x Buttons
3 x Labels
3 x Textboxes
(You can name these yourselves as you should have the simple knowledge on using these!)
This is because I added a little validation, if the two passwords aren't the same, then the program returns an error. You don't have to have this added but it'll be added into this source. So go click on your register form and lets get started.
First we need to declare two more variables:
First I need to explain objWrite and StreamWriter. This writes to the text file which we declared with filename (Tips will be down the bottom). Using true, this just writes to that file. Anyways this is all we need for this part of the coding, not much huh?
This is for our register button on the register form, so we need to add the validation sequence and if it's correct then objWrite will write the username and password to the text file, like so in the code below:
Now we need to add validation if it's incorrect.
Add a little validation if they're left blank! Now we need to close the objWrite otherwise the program can crash. Do this by adding:
Exiting the sub with Exit Sub just lets the program stop itself. Now we need to finish off the IF statement so lets do this by adding:
That wasn't so hard, was it?
Add a message box to let the user that the account is created just for personal use!
Otherwise if there is an error with the passwords, then show an error by adding:
Now we can finish the program by adding:
The program seems pretty simple, doesn't it?
Hints and Tips:
Try declaring the declarations above the the buttons just under Form1. This'll let it know that they're Global (So they can be used all over the form, not just where they're specified). Try it out yourself and let me know how this program is.
To get this, you should drop the .txt file in the bin folder.
Thanks again!
My program will be added here later.. But there isn't much to it.
Happy Coding!
-Lenneth
This is my next tutorial on how to make a basic login system with a nice little feature for registering users! I saw a few members requesting this in other topics so I thought I would just share this. It was done as a school project (Just to get us used to VB.NET) earlier on in the year. A lot of this was done by myself as I loved to get ahead and do my own features so enjoy the tutorial!
Okay so first we should always design our GUIs! Make it look the best to your own standards, don't bother caring with what other people think (Unless it was really bland..) So here is my version of my GUI.
![[Image: 2piu8.png]](https://i.imgur.com/2piu8.png)
Okay so what we need in our form is these:
3 x Forms
3 x Buttons
2 x Labels
Pretty basic isn't it?
This is the simple version of what we have. You can name these anything you like however I've decided to name mine:
Forms: Login.vb & Success.vb & Register.vb
Buttons: btnLogin & btnReg & btnClose
Labels: txtName & txtPass
Alright so onto our coding. This isn't too hard if you know about objRead.
Code
Dim filename As String = "E:\Login_Program\Login\login.txt"
Dim intError = 1
Dim objRead As New System.IO.StreamReader(filename)We need to declare these variables in our btnLogin (Or Button1).
Declaring filename is just using it for objRead which is just reading the text file for the login as well as it locates the file. (It's easier this way since we just use filename from now on in the button). Next we need to declare intError which if an error of 1 happens to appear, then an error will show. Lastly we declare objRead as a StreamReader with of coarse, our declared filename! This is so we don't have to type in the filepath every time.
Next we need to be able to read the text file as well as create a loop so it reads through the text file not just one or two lines!
Code
Do While objRead.Peek() <> -1Now we need to add an if statement for our validation process. This is so if we have the incorrect details then we won't login! We simply start off like this:
Code
If txtName.Text = objRead.ReadLine() And txtPword.Text = objRead.ReadLine() ThenCode
Me.Hide()
Success.Show()
intError = 0
End IfNow we need to finish off our loop so we know that it'll read throughout the whole text file. We simply add this code after it:
Code
LoopCode
If intError = 1 Then 'If an error has occured'
MessageBox.Show("Incorrect Username or password", "Login error", MessageBoxButtons.OK, MessageBoxIcon.Error)Lastly we need to close the objRead so the program doesn't infinite loop and eventually crash! We do this by adding:
Code
End If
objRead.Close()Also add for your register button:
Code
Me.Hide()
Register.Show()This is so we can do our next part of registering new users!
Now that was the first form out of two for coding (The third is only just a welcome screen which I couldn't be bothered using). If the login details are correct then my Success form will show like so:
Now it's time for coding the register details for the program!
First we should design the register form, so mine looks like this:
![[Image: ArzUy.png]](https://i.imgur.com/ArzUy.png)
I simply have:
2 x Buttons
3 x Labels
3 x Textboxes
(You can name these yourselves as you should have the simple knowledge on using these!)
This is because I added a little validation, if the two passwords aren't the same, then the program returns an error. You don't have to have this added but it'll be added into this source. So go click on your register form and lets get started.
First we need to declare two more variables:
Code
Dim filename As String = "login.txt"
Dim objWrite As New System.IO.StreamWriter(filename, True)This is for our register button on the register form, so we need to add the validation sequence and if it's correct then objWrite will write the username and password to the text file, like so in the code below:
Code
If txtPass.Text = txtRPass.Text Then
objWrite.WriteLine(txtUser.Text) 'Write username'
objWrite.WriteLine(txtPass.Text) 'Write password'Code
If txtUser.Text = "" And txtPass.Text = "" Then
MessageBox.Show("No username/password entered!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)Code
objWrite.Close()
Exit SubCode
Else
End IfAdd a message box to let the user that the account is created just for personal use!
Code
MessageBox.Show("The new account has been created!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)Code
Else
MessageBox.Show("Passwords are the same", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)Code
End If
objWrite.Close()
End SubThe program seems pretty simple, doesn't it?
Hints and Tips:
Try declaring the declarations above the the buttons just under Form1. This'll let it know that they're Global (So they can be used all over the form, not just where they're specified). Try it out yourself and let me know how this program is.
Code
Dim filename As String = "file.txt"Thanks again!
My program will be added here later.. But there isn't much to it.
Happy Coding!
-Lenneth
