Hi and Welcome to my Visual Basic .NET Tutorial! I'll be sharing with you some of my knowledge about Visual Basic .NET with the help of Visual Studio 2017 intended for beginners who have basic understanding of Visual Basic Programming and its IDE.
Today will be about:
Comparing 2 Inputs in Visual Basic .NET
The objective is to know whether the input in Textbox1 matches the input in Textbox2. The result should be displayed just below the command button that says " Match Inputs".
If input in Textbox1 don't match input in Textbox2 after you click the command button, display the message "Input 1 don't match with input 2!" or display "Input 1 matches with input 2!" if otherwise.
If no input was given to either Textbox1 or Textbox2 then, display the message "Input error! Please try again."
The result should look like this:
Or this :
And this:
Let's assume you have your Form named "Form1" which is the default name open already. It should look like this:
Let's start putting in some tools.
For this we'll only need 2 Textboxes, 2 Labels and 1 Command Button. If you click the "Toolbox" on the left side of your IDE ( Integrated Development Environment), you'll see a list of tools you can use and you should notice a search bar there that you can use to search for tools you need to pull up. Search for "Textbox", "Label" and "Button". You can either double click or just drag it towards your form.
By default, the first textbox, label or button you'll put in your Form will be named Textbox1, Label1 and Button1 respectively. You can change it but for the sake of this tutorial, we'll just use the default. Once your done positioning the tools and resizing your Form,it should look like this:
It's Coding Time!
Step #1: Double-click inside the form ( not the textboxes, label, etc ) and you should see something like this:
Between that "Private Sub" and "End Sub", you will write your code. Notice that it says "Form1_Load". This means that the code you write here is only executed every time Form1 loads.
Write this code between your Form1 Private Sub and End Sub:
Label1.Text = "Enter 1st Input"
Label2.Text = "Enter 2nd Input"
Button1.Text = "Match Inputs"
TextBox1.Text = ""
TextBox2.Text = ""
Step #2: Double-click the command button and you should see something like this:
Notice that this time, it doesn't say "Form1_Load" but "Button1_Click". This means that the code will be executed every time you click button1 which is right now, the only button on your Form1.
Add this code in between Private Sub and End Sub:
'Declare Variables and Variable Type
Dim input1, input2 As String
'Assign Variables
input1 = TextBox1.Text
input2 = TextBox2.Text
'Check if there's an input for either textbox1 or textbox2. If either is none or blank, then show error message
If input1 = "" Or input2 = "" Then
MessageBox.Show("Input error! Please try again.")
' check if textbox1 and textbox2 have the same input. If input is the same, show message saying input is the same
ElseIf input1 = input2 Then
MessageBox.Show("Input 1 matches with input 2!")
' check if not the same. If not then show message saying input don't match
Else
MessageBox.Show("Input 1 don't match with input 2!")
End If
I put some comments on the code. Please notice that the one with apostrophe signs are not included on the code. You can remove them and the program will still work.
Step #3: Execute and Enjoy!
If you're on standard view, you should see a play icon there that says "start" just above the IDE.
Click on that to test and run your program. There should be no errors if you follow the code right. Give it a try!
Here's the Full Code Without Comments:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label1.Text = "Enter 1st Input"
Label2.Text = "Enter 2nd Input"
Button1.Text = "Match Inputs"
TextBox1.Text = ""
TextBox2.Text = ""
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim input1, input2 As String
input1 = TextBox1.Text
input2 = TextBox2.Text
If input1 = "" Or input2 = "" Then
MessageBox.Show("Input error! Please try again.")
ElseIf input1 = input2 Then
MessageBox.Show("Input 1 matches with input 2!")
Else
MessageBox.Show("Input 1 don't match with input 2!")
End If
End Sub
End Class
That's it! I might share long and complex codes in the future but for now, let's start with something a beginner can understand. Computer Programming can give you real headache sometimes but it's really a fun and awesome thing to learn!
Until Next Time My Steemian Friends!
Please Don't Forget to Resteem and Upvote If You Liked this Post!
This post has received a 4.53 % upvote from @booster thanks to: @gvincentjosephm.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit