Skip to main content

Visual Basic . Net #2


Hello Guys, If you didn't see Visual Basic Series Introduction click here

Ok, Let's start Open Visual Studio 2012
In this tutorial, You will learn

How to make a simple calculator 


First, Simple calculator idea is very easy user will enter 2 number in 2 Textbox and She/He will choose one operation between multiply, divide, subtract, add and the result will appear in label
so we will need 4 Buttons, 2 Textboxes,  4 Label 1 For Result and 2 to Hint near Textboxes and one to write description



1. Make a new Project File > New > Project, or From Start Page left sidebar press New Project.


Start Page

2. Choose Visual Basic and WindowsForm Application as in Picture and Change the name


3. Put 4 buttons, 2 textboxes, 4 labels and change the text property as following

  • Button1 →→→→→→→→ Add
  • Button2 →→→→→→→→ Subtract
  • Button3 →→→→→→→→ Multiply
  • Button4 →→→→→→→→ Divide
  • Label1 →→→→→→→→   Result
  • Label2 →→→→→→→→   Num1
  • Label3 →→→→→→→→   Num2
  • Label4 →→→→→→→→  "Any Thin you want in my case I will write Welcome to Simple Calculator that made by ThinkFakar"

Program Design

4. Ok Let's go to Coding window press Double click on Add Button (Button1)

The Main rule in Visual Basic is Control.Property = Value 
Write this code

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim num1 As Integer
        Dim num2 As Integer
        num1 = TextBox1.Text
        num2 = TextBox2.Text
        Dim result As Integer
        result = num1 + num2
        Label1.Text = result
    End Sub
End Class

Ok Let's analyse the code

Dim num1 As Integer

Dim is a word to Define a variable
num1  is the name of the variable
"As" after it, you can find the type of the variable
Integer is the type of the variable

num1 = TextBox1.Text

This line means that PC will take the value in Textbox1.text ( The Number that User will enter ) and put it in variable num1

result = num1 + num2

In this Line, the PC will take num1 and num2 and it will add them then put it in result variable

5. Coding of Subtract Button (Button2)

The code

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim result As Integer
        result = CInt(TextBox1.Text) - CInt(TextBox2.Text)
        Label1.Text = "Result : " & result
End Sub

Ok Let's analyse the code

result = Cint(TextBox1.Text) - Cint(TextBox2.Text)

Ok, now we have a new Function Cint (Conversation Integer). It is very easy function that will convert any types to integer read about it more here so I save time instead making 3 variable I made 1 only Read More here

Label1.Text = "Result : " & result

This line will appear word "Result: " without quotation and the result

6. Coding of Multiply Button

The Code

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Label1.Text = "Result : " & (CInt(TextBox1.Text) * CInt(TextBox2.Text))
End Sub

As we see here we didn't use any variable

7. Coding of Divide Button

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Label1.Text = "Result : " & (CInt(TextBox1.Text) / CInt(TextBox2.Text))
End Sub

same Code as Multiply Button

Conclusion

We must learn from this tutorial that the code can be written in different ways and it do the same things every time 

Has any question?  Comment Below ↓↓↓↓
Wait for me next tutorial how to make simple Game ( Catch Me )




Comments

Popular posts from this blog

How to change PC resolution with VB.NET

Ok, Hi guys, After very long time I back with a surprise a code to change resolution of PC with VB.NET just copy this and past it and Call it with ChangeRes( 1024 , 768 , 32 ) The CODE  function generate() { Public Class Form1 ' change resolution Constants, Types and Declares ' Using this option to shutdown windows does not send ' the WM_QUERYENDSESSION and WM_ENDSESSION messages to ' the open applications. Thus, those apps may loose ' any unsaved data. ' Const EWX_FORCE As Short = 4 Const CCDEVICENAME As Short = 32 Const CCFORMNAME As Short = 32 Const DM_BITSPERPEL As Integer = &H40000 Const DM_PELSWIDTH As Integer = &H80000 Const DM_PELSHEIGHT As Integer = &H100000 Const CDS_UPDATEREGISTRY As Short = &H1S Const CDS_TEST As Short = &H2S Const CDS_FULLSCREEN As Short = &H4S Const DISP_CHANGE_SUCCESSFUL As Short = 0 Const DISP_CHANGE_RESTART As Short = 1 ...

Visual Basic ( Catch Me ) Introduction

Hello guys, Welcome back to Visual Basic Tutorial ( GAME ) Catch me: - It is a simple game that a Picture will appear Random in the Windows Form and the player (user) will try to click on it by mouse that all We have lot of version to Catch me so I will make a list for them 1. Catch me Version 1.0.0  Features : User will play in 800*600 window state  User will play it without any effects The result will not be saved 2. Catch me Version 2.0.0  Features : User will play  in Full Screen When User will press in the picture ( Target ) the black color will be green the back to Default The result will  be saved as long as the game is opened 3. Catch me Version 3.0.0  Features : User will play  in Full Screen User will choose the level of game ( Insane - Extreme - Hard - Normal - Easy - Baby Level ) The result will be saved in Database with Names ------------------------------------------------- Notice The ti...

How Bitcoin Mining Works

In traditional fiat money systems, governments simply print more money when they need to. But in bitcoin, money isn’t printed at all – it is discovered. Computers around the world ‘mine’ for coins by competing with each other. How does mining take place? People are sending bitcoins to each other over the bitcoin network all the time, but unless someone keeps a record of all these transactions, no-one would be able to keep track of who had paid what. The bitcoin network deals with this by collecting all of the transactions made during a set period into a list, called a block. It’s the miners’ job to confirm those transactions, and write them into a general ledger. Making a hash of it This general ledger is a long list of blocks, known as the 'blockchain'. It can be used to explore any transaction made between any bitcoin addresses, at any point on the network. Whenever a new block of transactions is created, it is added to the blockchain, creating an increasingly le...