Hello all.. This page will help you learn basics of visual basic programming
1. Desigining a simple login form in VB.Net
Here, we will create a basic Login form using which one can login to the system after feeding true credentials.The form should throw error message if the username or passwod were not fed correctly.The input fields can't be blankThe password fields should not show characters entered.
Do you have a website? Increase traffic of your website. Submit your website to most of the search engines
Submit ExpressSearch Engine Marketing ServicesVB.Net provides a very easy to use development environment where one can develop a basic to full-fledged windows application easily.Before starting this tutorial, make sure you have installled following things
Microsoft visual studio 2008 MSDN (offline help library)
Now open the Microsoft visual studio 2008 or other latest versions.
First Session:
Design Part:
-
Go to File-> New Project
-
Select "Windows application" and give this project a name and click next
-
By default a Form will be created with the title "Form1",
-
Select the form and go to properties. A properties table will be there at the right side
-
Change the name to "Log On"
-
On the left panlel, a tollbox is there which contains all the tools that can be used on a windows form. Eg. Button, Textbox, Checkbox, radio button, frames, images, labels etc.
-
Now, drag a Button from the toolbox and drop it on the form where you want it to be. You can double click also on the button icon in the toolbox. The buttton will be added to your form. You will have to palce it correctly, then.
-
Select the button1 and go to the properties panel at the right. Change the Text from "Buttton1" to "Log On"
-
Like wise drag one more button from toolbox. You can copy the existing button of your form and paste there. A new button will be added with name "Button"
-
Now you have to get some lables, textboxes and set the properties as per below table.
Controls
|
Default text
|
New Value
|
Name
|
Button
|
Button1
|
Log On
|
btnLogin
|
Button
|
Button2
|
Cancel
|
btnCancel
|
Label
|
Label1
|
User Name
|
lblUser
|
Label
|
Label2
|
Password
|
lblPassword
|
TextBox
|
|
|
txtUser
|
TextBox
|
|
|
txtPassword
|
Label
|
Label3
|
Welcome! Please Enter your user name and password below.
|
|
PictureBox
|
|
Browse a picture
|
picLogin
|
Form
|
Form1
|
Log On
|
frmLogin
|
- Now select the password textbox "txtPassword" and go to properies panel. Set the password character to "*" o "&" . Now, when a character will be entered into this field, it will be displayed as password character "*".
-
Set the height and width of your window form to make it smaller as per the figure. Also set the resize value to "false". The form can be resiezed now.
-
If you want you can test your application. Build and run it. The form will appear, but no buttons will work.
Coding Part
-
Now we will add a little code on the button we have created. Coding in VB.Net is fun
-
Just double click the "Log on" button. You will enter the coding part. Here you have to enter the code that will be executed after clicking this button.
-
Enter these lines on the button "Log On" code:
if txtPassword.text.length<>0 && txtUser.text.length<>0 then
'checks if the some values are entered into the textboxes
if txtPassword.text="shankar" && txtUser.text="kumar"then messagebox.show "Login successful!!"
else' if the username not entered as "shankar" and password not'as "kumar", it will say: incorrect
messagebox.show "Sorry,User Name or password is incorrect!!"'clearFields()
end if
else
messagebox.show "Neither of fields can be blank!!"
end ifOn Cancel button put below code.
frmLogin.close()'to simply close the login window.You are done!!Now build and run this application.Additinally you can uncomment the "clearFields()" and put below code for it.public sub clearFields()txtPassword.text=""txtUser.text=""end subTo comment a line in VB.net just prefix it with " ' " .2.Designing a calculator in VB.net
This tutorial will help you to build a windows calculator from scratch We will see stepwise from basic to create this application 1. Open your visual Stdio 2008 or newer, Create a new Project, select windows application, give it a name
2.Now you will see a form by default. select the form and go the properties.
Set the text propery of form to "Calculator". It will give a title to your calculator
3. Now select a textbox from the toolbox and place it on your form. You can do it
by double clicking on the textbox in the toolbox or you can drag the textbox and
drop it on top of the form.
4. Set text alignment of the textbox to "right".5.Now place as many buttons as per the requirement. buttons for digits(0-9), (+,-,*,/, = ,.) and others.
Set properties of the buttons as shown in the below table.
Control Type Initial Value New Text Name Other Property Button Button0 0 btnkey0 Button Button1 21 btnkey1 --- --- --- --- Button Button9 9 btnkey9 Textbox textbox1 0 txtdisplay Text-align: Right Button Button10 + btnPlus Button Button11 - btnMinus Button Button12 * btnMult Button Button13 / btnDiv Button Button14 / btnEqual Button Button15 / btnBack Button Button16 / btnDecimal 6.Now Lets do some coding. Double click on the button with text "1". You will enter into the code section for this button. Here we have to put code for what we want to do when we click this button.The text on the textbox should change to 1 if its blank and 1 should be apended to the numbers if the display textbox is not blank. So put below code on this.Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clicktxtdisplay.Text += btnkey1.Text ' The button text '1' will be appended to the display textbox.End SubNote:- Private Sub---- End Sub are generated by just double clicking on the button. Just put the code in between.7.Do the same for all the buttons(0-9), also same for decimal button.See the figure below.8. Now coding for +,-,*,/ buttons. Declare some global variables at the top of your code section.Public Class Form1Dim b As Double ' To store the values of numeric buttons for +,-,*,/Dim a As Double ' To store the values of numeric buttons for +,-,*,/Dim oper As String9. Go to coding for the plus button.Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Clickoperation = "+" ' sets the oper string to "+" that means addition is to be donea = Val(TextBox1.Text) ' a will store the first number from the textboxTextBox1.Text = "" ' and the textbox will be blank again to get 2nd numberEnd SubDo the same for other operations: minus, multiply, divide. Only change is set operation="-" for minus button and so on..10.Now we will see result by coding for "=" button. Put below code on this button.b = Val(TextBox1.Text) ' will store the 2nd number from textboxIf oper.Equals("+") Then 'operation' is set to "+" the textbox will display a+bTextBox1.Text = a + bElseIf oper.Equals("-") Then ' same with othersTextBox1.Text = a - bElseIf oper.Equals("*") ThenTextBox1.Text = a * bElseIf oper.Equals("/") ThenTextBox1.Text = a / bEnd If
11. We need a back button also to clear the textbox one by one if user enters any wrong digit.So, think how you will make 123 to 12 or 5435 to 543 or 645 to 64...The logic is divide the number by 10, subtract the number by remainder and then divide the number by 10 and put the result into display. See code.Dim rema, nval As Doublerema = Val(TextBox1.Text) Mod 10nval = Val(TextBox1.Text) - remaTextBox1.Text = nval / 10.012. Additionally You can put a square root button, reciprocal(1/x) button, percentage button(%) also. See code for the same.Private Sub btnReciprocal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.ClickTextBox1.Text = 1.0 / Val(TextBox1.Text)End SubPrivate Sub btnSqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.ClickTextBox1.Text = Math.Sqrt(Val(TextBox1.Text))End SubPrivate Sub btnPercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button21.Clickb = Val(TextBox1.Text)Dim c As Doublec = a * bTextBox1.Text = Val(c / 100.0)End Sub13) To get the square of the number, you can try below code:Private Sub btnSquare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button22.ClickTextBox1.Text = Val(TextBox1.Text)*Val(TextBox1.Text)End Sub