Public Class Form1
' Declare variables for arithmetic operations
Dim first_number As Double
Dim second_number As Double
Dim a As Double
Dim Arithop As String
Dim n As Int64
' Event handler for numeric buttons (0-9, dot)
Private Sub Button_Click(sender As Object, e As EventArgs) Handles btn9.Click, btn8.Click, btn7.Click, btndot.Click, btn6.Click, btn5.Click, btn4.Click, btn3.Click, btn2.Click, btn1.Click, btn0.Click
' Cast the sender as a Button
Dim b As Button = sender
' Update the display when a numeric button is clicked
If Label1.Text = "0" Then
Label1.Text = b.Text
Else
Label1.Text = Label1.Text + b.Text
End If
End Sub
' Event handler for the clear button
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
' Get the text from Label1 and Label2
Dim label1Text As String = Label1.Text
Dim label2Text As String = Label2.Text
' Remove the last character from Label1
If label1Text.Length > 1 Then
Label1.Text = label1Text.Substring(0, label1Text.Length - 1)
Else
Label1.Text = "0"
End If
' Remove the last character from Label2
If label2Text.Length > 1 Then
Label2.Text = label2Text.Substring(0, label2Text.Length - 1)
Else
Label2.Text = ""
End If
End Sub
' Event handler for the clear entry button
Private Sub btnClearEntry_Click(sender As Object, e As EventArgs) Handles btnClearEntry.Click
' Clear both labels
Label1.Text = "0"
Label2.Text = ""
End Sub
' Event handler for arithmetic operation buttons (+, -, *, /, Mod, Exp)
Private Sub Arithmetic_fuctions(sender As Object, e As EventArgs) Handles btnMult.Click, btnMod.Click, btnMinus.Click, BtnExp.Click, btndiv.Click, btnAdd.Click
' Cast the sender as a Button
Dim ops As Button = sender
' Store the current number from Label1 and the selected operation
first_number = Label1.Text
Label2.Text = Label1.Text
Label1.Text = ""
Arithop = ops.Text
Label2.Text = Label2.Text + " " + Arithop
End Sub
' Event handler for the equals button (=)
Private Sub btnequals_Click(sender As Object, e As EventArgs) Handles btnequals.Click
' Retrieve the second number from Label1 and perform the selected operation
second_number = Label1.Text
If Arithop = "+" Then
a = first_number + second_number
Label1.Text = a
Label2.Text = ""
ElseIf Arithop = "-" Then
a = first_number - second_number
Label1.Text = a
Label2.Text = ""
ElseIf Arithop = "*" Then
a = first_number * second_number
Label1.Text = a
Label2.Text = ""
ElseIf Arithop = "/" Then
a = first_number / second_number
Label1.Text = a
Label2.Text = ""
ElseIf Arithop = "Mod" Then
a = first_number Mod second_number
Label1.Text = a
Label2.Text = ""
ElseIf Arithop = "Exp" Then
a = first_number ^ second_number
Label1.Text = a
Label2.Text = ""
End If
End Sub
' Event handler for the binary conversion button
Private Sub btnBin_Click(sender As Object, e As EventArgs) Handles btnBin.Click
If Int64.TryParse(Label1.Text, n) Then
' Convert the number to binary and update Label1
Label1.Text = Convert.ToString(n, 2)
Else
Label1.Text = ""
End If
End Sub
' Event handlers for various trigonometric functions
Private Sub btnSin_Click(sender As Object, e As EventArgs) Handles btnSin.Click
a = Math.Sin(Label1.Text)
Label1.Text = a
Label2.Text = ""
End Sub
Private Sub btnCos_Click(sender As Object, e As EventArgs) Handles btnCos.Click
a = Math.Cos(Label1.Text)
Label1.Text = a
Label2.Text = ""
End Sub
Private Sub btnTan_Click(sender As Object, e As EventArgs) Handles btnTan.Click
a = Math.Tan(Label1.Text)
Label1.Text = a
Label2.Text = ""
End Sub
Private Sub btnCosh_Click(sender As Object, e As EventArgs) Handles btnCosh.Click
a = Math.Cosh(Label1.Text)
Label1.Text = a
Label2.Text = ""
End Sub
Private Sub btnSinh_Click(sender As Object, e As EventArgs) Handles btnSinh.Click
a = Math.Sinh(Label1.Text)
Label1.Text = a
Label2.Text = ""
End Sub
Private Sub btnTanh_Click(sender As Object, e As EventArgs) Handles btnTanh.Click
a = Math.Tanh(Label1.Text)
Label1.Text = a
Label2.Text = ""
End Sub
' Event handler for logarithm base 10
Private Sub btnLog_Click(sender As Object, e As EventArgs) Handles btnLog.Click
a = Math.Log10(Label1.Text)
Label1.Text = a
Label2.Text = ""
End Sub
' Event handler for changing the sign of the number
Private Sub btnPlusMinus_Click(sender As Object, e As EventArgs) Handles btnPlusMinus.Click
Dim q As Double
q = Convert.ToDouble(Label1.Text)
Label1.Text = Convert.ToString(-1 * q)
End Sub
' Event handler for clearing and resetting the calculator
Private Sub btnBackSpace_Click(sender As Object, e As EventArgs) Handles btnBackSpace.Click
Label1.Text = "0"
first_number = 0
second_number = 0
End Sub
' Event handler for calculating cube roots
Private Sub btnCubeRoot_Click(sender As Object, e As EventArgs) Handles btnCubeRoot.Click
Dim inputvalue As Double
If Double.TryParse(Label1.Text, inputvalue) Then
a = Math.Pow(inputvalue, 1.0 / 3.0)
Label1.Text = a.ToString()
Label2.Text = ""
Else
Label1.Text = "Invalid input"
Label2.Text = ""
End If
End Sub
' Event handler for square roots
Private Sub btnsqrt_Click(sender As Object, e As EventArgs) Handles btnsqrt.Click
a = Math.Sqrt(Label1.Text)
Label1.Text = a
Label2.Text = ""
End Sub
' Event handler for inserting the value of pi
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnPi.Click
Label1.Text = "3.141592653589976323"
End Sub
' Event handler for natural logarithm
Private Sub btnNaturalLog_Click(sender As Object, e As EventArgs) Handles btnNaturalLog.Click
a = Math.Log(Label1.Text)
Label1.Text = a
Label2.Text = ""
End Sub
' Event handler for calculating the cube of a number
Private Sub btnCube_Click(sender As Object, e As EventArgs) Handles btnCube.Click
Dim inputValue As Double
If Double.TryParse(Label1.Text, inputValue) Then
Dim cube As Double = inputValue * inputValue * inputValue
Label1.Text = cube.ToString()
Label2.Text = ""
Else
Label1.Text = "Invalid input"
Label2.Text = ""
End If
End Sub
' Event handler for calculating the square of a number
Private Sub btnsquare_Click(sender As Object, e As EventArgs) Handles btnsquare.Click
Dim inputValue As Double
If Double.TryParse(Label1.Text, inputValue) Then
Dim square As Double = inputValue * inputValue
Label1.Text = square.ToString()
Label2.Text = ""
Else
Label1.Text = "Invalid input"
Label2.Text = ""
End If
End Sub
' Event handler for converting to octal
Private Sub btnOct_Click(sender As Object, e As EventArgs) Handles btnOct.Click
If Int64.TryParse(Label1.Text, n) Then
Label1.Text = Convert.ToString(n, 8)
Else
Label1.Text = ""
End If
End Sub
' Event handler for converting to hexadecimal
Private Sub btnHex_Click(sender As Object, e As EventArgs) Handles btnHex.Click
If Int64.TryParse(Label1.Text, n) Then
Label1.Text = Convert.ToString(n, 16)
Else
Label1.Text = ""
End If
End Sub
' Event handler for calculating the reciprocal
Private Sub btnReciprocal_Click(sender As Object, e As EventArgs) Handles btnReciprocal.Click
Dim inputValue As Double
If Double.TryParse(Label1.Text, inputValue) Then
If inputValue = 0 Then
Label1.Text = "Cannot divide by zero"
Label2.Text = ""
Else
Dim reciprocal As Double = 1.0 / inputValue
Label1.Text = reciprocal.ToString()
Label2.Text = ""
End If
Else
Label1.Text = "Invalid input"
Label2.Text = ""
End If
End Sub
' Event handler for initializing the unit conversion ComboBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Set the initial text for ComboBox
ComboBox1.Text = "Choose one ...."
' Add items to the ComboBox for unit conversions
ComboBox1.Items.Add("Celcious to Fahrenheit")
ComboBox1.Items.Add("Fahrenheit to Celcious")
ComboBox1.Items.Add("Miles to Kilometers")
ComboBox1.Items.Add("Kilometers to Miles")
ComboBox1.Items.Add("Centimeter to Meter")
ComboBox1.Items.Add("Meter to Centimeter")
ComboBox1.Items.Add("Celcious to Kelvin")
ComboBox1.Items.Add("Kelcin to Celcious")
End Sub
' Event handler for performing unit conversions
Private Sub btnConvert_Click(sender As Object, e As EventArgs) Handles btnConvert.Click
Dim convt As Double
If ComboBox1.Text = "Celcious to Fahrenheit" Then
' Perform Celsius to Fahrenheit conversion
convt = (9 / 5 * Double.Parse(TextBox1.Text)) + 32
Label4.Text = convt & " Fahrenheit"
ElseIf ComboBox1.Text = "Fahrenheit to Celcious" Then
' Perform Fahrenheit to Celsius conversion
convt = (5 / 9 * (Double.Parse(TextBox1.Text) - 32))
Label4.Text = convt & " Celcious"
ElseIf ComboBox1.Text = "Miles to Kilometers" Then
' Perform Miles to Kilometers conversion
convt = Double.Parse(TextBox1.Text) * 1.60934
Label4.Text = convt & " Kilometers"
ElseIf ComboBox1.Text = "Kilometers to Miles" Then
' Perform Kilometers to Miles conversion
convt = Double.Parse(TextBox1.Text) / 1.60934
Label4.Text = convt & " Miles"
ElseIf ComboBox1.Text = "Centimeter to Meter" Then
' Perform Centimeter to Meter conversion
convt = Double.Parse(TextBox1.Text) / 100
Label4.Text = convt & " Meters"
ElseIf ComboBox1.Text = "Meter to Centimeter" Then
' Perform Meter to Centimeter conversion
convt = Double.Parse(TextBox1.Text) * 100
Label4.Text = convt & " Centimeters"
ElseIf ComboBox1.Text = "Celcious to Kelvin" Then
' Perform Celsius to Kelvin conversion
convt = Double.Parse(TextBox1.Text) + 273.15
Label4.Text = convt & " Kelvin"
ElseIf ComboBox1.Text = "Kelvin to Celcious" Then
' Perform Kelvin to Celsius conversion
convt = Double.Parse(TextBox1.Text) - 273.15
Label4.Text = convt & " Celcious"
ElseIf ComboBox1.Text = "Choose one....." Or TextBox1.Text = "" Then
' Show a message if no conversion is selected or no input is provided
MessageBox.Show("Select a Unit conversion", "Unit Calculator", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
End Class Write, Run & Share VB.net code online using OneCompiler's VB.net online compiler for free. It's one of the robust, feature-rich online compilers for VB.net language, running on the latest version 16. Getting started with the OneCompiler's VB.net compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as VB.net. OneCompiler also has reference programs, where you can look for the sample code to get started with.
OneCompiler's VB.net online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample VB.net program which takes name as input and prints hello message with your name.
Public Module Program
Public Sub Main(args() As string)
Dim name as String = Console.ReadLine() ' Reading input from STDIN
Console.WriteLine("Hello " & name) ' Writing output to STDOUT
End Sub
End Module
Visual Basic is a event driven programming language by Microsoft, first released in the year 1991.
Variable is a name given to the storage area in order to identify them in our programs.
Simple syntax of Variable declaration is as follows
Dim variableName [ As [ New ] dataType ] [ = initializer ]
variableName = value
If condition-expression Then
'code
End If
If(conditional-expression)Then
'code if the conditional-expression is true
Else
'code if the conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
Else If(conditional-expression) Then
'code if the above conditional-expression is true
Else
'code if the above conditional-expression is false
End If
If(conditional-expression)Then
'code if the above conditional-expression is true
If(conditional-expression)Then
'code if the above conditional-expression is true
End If
End If
Select [ Case ] expression
[ Case expressionlist
'code ]
[ Case Else
'code ]
End Select
For counter [ As datatype ] = begin To end [ Step step ]
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ counter ]
For Each element [ As datatype ] In group
'code
[ Continue For ]
'code
[ Exit For ]
'code
Next [ element ]
While conditional-expression
'Code
[ Continue While ]
'Code
[ Exit While ]
'Code
End While
Do { While | Until } conditional-expression
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop
Do
'Code
[ Continue Do ]
'Code
[ Exit Do ]
'Code
Loop { While | Until } conditional-expression
Procedure is a sub-routine which contains set of statements. Usually Procedures are written when multiple calls are required to same set of statements which increases re-usuability and modularity.
Procedures are of two types.
Functions return a value when they are called.
[accessModifiers] Function functionName [(parameterList)] As returnType
'code
End Function
Sub-procedures are similar to functions but they don't return any value.
Sub ProcedureName (parameterList)
'Code
End Sub