Rudi
  • Home
  • About
  • Contact
  • VB.Net
    • Flippin
    • Calculator
    • Mine Scout
    • Slot Machine
    • Snake
    • Find Words
    • Accounting using Child forms
  • Excel
    • Calendar 1
    • Calendar 2
    • Sudoku
    • Mine Scout
    • Tilexcel
    • ExceLudo
    • Alphabet Game
  • C# Child Forms
  • C# Struct Array
VB.Net Calculator
It has a Number button array and one procedure handles clicking any of the numbers. Another button array handles clicking any of the calculation buttons.

This calculator uses Reverse Polish Notation. Here we enter 2 numbers then click the mathematical action button. An Equals button is not required.

The numbers can be typed straight into the Operand textboxes. Alternatively, clicking a number button builds the number in the Input box then we transfer the number to an Operand box by clicking on an To Operand label.


Picture


Button Array

1. Select the 12 buttons on the left
2. Select Events by clicking on the flash in the Properties box
3. Double click the Click event

Picture

Code:

  Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn9.Click, btn8.Click, btn7.Click, btn6.Click, btn5.Click, btn4.Click, btn3.Click, btn2.Click, btn1.Click, btn0.Click,
    btnPoint.Click, btnBackSpace.Click

    If InStr(txtInput.Text, ".") <> 0 And sender.Text = "." Then Exit Sub

    If sender.Text = "<" Then
      If txtInput.TextLength > 0 Then
        txtInput.Text = txtInput.Text.Substring(0, txtInput.TextLength - 1)
      End If
      Exit Sub
    End If

    txtInput.Text += sender.Text
    txtInput.Focus()
    txtInput.SelectionStart = txtInput.TextLength

  End Sub

Select the 4 function buttons and start the click event as before


Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles _
    btnSubtract.Click, btnMultiply.Click, btnDivide.Click, btnAdd.Click

    Try

      Select Case sender.text
        Case "/"
          txtResult.Text = CSng(txtOperand1.Text) / CSng(txtOperand2.Text)
        Case "*"
          txtResult.Text = CSng(txtOperand1.Text) * CSng(txtOperand2.Text)
        Case "+"
          txtResult.Text = CSng(txtOperand1.Text) + CSng(txtOperand2.Text)
        Case "-"
          txtResult.Text = CSng(txtOperand1.Text) - CSng(txtOperand2.Text)
      End Select

    Catch ex As Exception
      MsgBox("Error")
    End Try

    txtInput.Text = ""
    txtInput.Focus()

  End Sub
Here are the click procedures for the Labels To Operand 1 and To Operand 2. They transfer the number from the Input textbox to an operand textbox. Double click each label.

Private Sub lblToOp1_Click(sender As Object, e As EventArgs) Handles lblToOp1.Click

    txtOperand1.Text = txtInput.Text
    txtInput.Text = ""
    txtInput.Focus()

  End Sub

  Private Sub lblToOp2_Click(sender As Object, e As EventArgs) Handles lblToOp2.Click

    txtOperand2.Text = txtInput.Text
    txtInput.Text = ""
    txtInput.Focus()

  End Sub

To use the calculator enter a number. Transfer the number to an operand. Repeat. Click a function button. Could also type numbers straight into the operand textboxes.

The Storages text boxes can store numbers by using Ctrl-C say in the Result and then Ctrl-V in a Storage which is what I did in the picture above.

Now go and add buttons for special functions.

Have a nice day.
Rudi
Proudly powered by Weebly