Wednesday, 28 August 2013

Design a form to perform calculator functions using vb.net

hello friends in this answer some commands are missing. sorry for that.

First we design an interface in VB.Net form as shown in the picture.
In the code window of the form write the code as under:
First create two variables f and res as shown under.

Public Class Form1
    Dim f As Double
    Dim res As Double

Then you need to write the code on each of the buttons click procedure on the VB.Net form.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = TextBox1.Text & 1
    End Sub

Private Sub ButtonEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        If f = 1 Then
            TextBox1.Text = res + Val(TextBox1.Text)
        ElseIf f = 2 Then
            TextBox1.Text = res - Val(TextBox1.Text)
        ElseIf f = 3 Then
            TextBox1.Text = res * Val(TextBox1.Text)
        ElseIf f = 4 Then
            TextBox1.Text = res / Val(TextBox1.Text)
       
    End Sub


    Private Sub btndot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
        TextBox1.Text = TextBox1.Text & "."
    End Sub

Private Sub btndiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndiv.Click
        f = 4
        res = Val(TextBox1.Text)
        TextBox1.Text = ""
    End Sub

    Private Sub btnmul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmul.Click
        f = 3
        res = Val(TextBox1.Text)
        TextBox1.Text = ""
    End Sub
System.EventArgs) Handles btnadd.Click
        f = 1
        res = Val(TextBox1.Text)
        TextBox1.Text = ""
    End Sub

    Private Sub btnsroot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsroot.Click

There is also a function called Keys which is used to prevent to type any text in the textbox.

    Private Function keys(ByVal key As String) As Boolean
        If (key >= 48 And key <= 57) Or key = 8 Then
            keys = False
        Else
            keys = True
        End If
    End Function

Now we can call the function on the keypress event of textbox.
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
e.Handled = keys(Asc(e.KeyChar))
End Sub


2 comments:

  1. Thanks for your sharing of designing a form to perform calculator functions using vb.net.Here i found another article to do the same things:
    http://www.dreamincode.net/forums/topic/32951-basic-calculator-in-vbnet/

    ReplyDelete
  2. Thanks for writing to us. You can subscribe to my youtube channel channel for latest tech videos:
    https://www.youtube.com/channel/UCnRCUB8utEFY0xqm7L92yZg

    ReplyDelete