giving calculator input from keyboard keys without pressing calculator buttons - vb.net

I created a VB.net calculator application.It works fine but say I want to add 5+7.Then as my application works I can press 5 from the keyboard as I gave the text of button with digit 5 as &5 but then if I press + from the keyboard it doesn't work.I have to press the + button in the calculator.
I think that's because my add button is designed to handle click event as btn_add_Click.Is there a way I can make this application to work so that without pressing the buttons in the calculator I can press the keys in keyboard and do the computation.Here's my code:
Imports System.Math
Public Class Form1
Private isFirstExist As Boolean
Private inputOperator As String
Private secondNum As Decimal
Private firstNum As Decimal
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtCalc.TextChanged
End Sub
Private Sub btn_zero_Click(sender As System.Object, e As System.EventArgs) Handles btn_zero.Click
removeFrontZero(0)
End Sub
Private Sub btn_one_Click(sender As System.Object, e As System.EventArgs) Handles btn_one.Click
removeFrontZero(1)
End Sub
Private Sub btn_two_Click(sender As System.Object, e As System.EventArgs) Handles btn_two.Click
removeFrontZero(2)
End Sub
Private Sub btn_clear_Click(sender As System.Object, e As System.EventArgs) Handles btn_clear.Click
txtCalc.Clear()
txtCalc.Text = "0"
End Sub
'Remove zero which is at the start
Public Sub removeFrontZero(ByVal digit As Integer)
If txtCalc.Text = "0" Then
txtCalc.Text = CStr(digit)
Else
txtCalc.Text &= digit
End If
End Sub
Private Sub btn_add_Click(sender As System.Object, e As System.EventArgs) Handles btn_add.Click
inputOperator = "+"
isFirst()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
isFirstExist = False
End Sub
Private Sub btn_equal_Click(sender As System.Object, e As System.EventArgs) Handles btn_equal.Click
If isFirstExist Then
secondNum = CType(txtCalc.Text, Decimal)
End If
'Calculating the result
Dim result As Decimal = calculate(firstNum, secondNum, inputOperator)
txtCalc.Text = result.ToString()
isFirstExist = False
End Sub
Private Function calculate(ByVal num1 As Decimal, ByVal num2 As Decimal, ByVal inputOp As String) As Decimal
Dim output As Decimal
firstNum = num1
secondNum = num2
Select Case inputOp
Case "+"
output = num1 + num2
Case "-"
output = num1 - num2
Case "/"
Dim value As Decimal
Try
isFirst()
value = (num1 / num2)
Catch ex As DivideByZeroException
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
End Try
output = value
Case "*"
output = num1 * num2
Case "Mod"
output = (num1 Mod num2)
Case "^"
output = CDec(Math.Pow(num1, num2))
End Select
Return output
End Function
Private Sub isFirst()
If isFirstExist = False Then
firstNum = CType(txtCalc.Text, Decimal)
isFirstExist = True
txtCalc.Text = "0"
End If
End Sub

Assuming you're using the textbox to hold the users input, in the Textchanged or even KeyPressed handler trap the operator keys and run the function instead of displaying the operator.
Something like this:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCalc.KeyPress
If e.KeyChar = "+"c Then
inputOperator = "+"
isFirst()
e.Handled = True
End If
End Sub
To display the operator set handled to false.

Related

If my counter ends at 10 at textbox1(TB1) the random number won't show in my textbox2(TB2)

If my counter ends at 10 at textbox1 (TB1) the random number won't show in my textbox2(TB2)
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If TB1.Text = 10 Then
Dim num1 As Integer
Dim randomnumber As New Random
num1 = randomnumber.Next(100, 201)
TB2.Text = num1
End If
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TB1.Text = TB1.Text + 1
If TB1.Text = 10 Then
Timer1.Enabled = False
End If
End Sub
End Class
Use a numeric variable to keep the count, not the TextBox. The TextBox.Text property is of type String, not Integer!
Putting Option Strict On at the top of your code will enforce using proper types. VB by default allows you to loosely convert between integer and string, which is poor practice.
Option Strict On
Public Class Form1
Private count As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
count = 0
Timer1.Enabled = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If count = 10 Then
Dim randomnumber As New Random()
Dim num1 = randomnumber.Next(100, 201)
TB2.Text = num1.ToString()
End If
Timer1.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
count += 1
Timer1.Enabled = count <> 10
TB1.Text = count.ToString()
End Sub
End Class

VB Calculator add some functions

I have programmed a calculator with Visual Basic, but I would like to add a few more functions. But I can't figure out how to solve the following problems in my code:
if more than one zero is entered in the input field, it should simply delete the leading zeros.(example 0000111 -----> 111 or 0002345 ----> 2345)
likewise, a decimal number should not have several zeros before the decimal point (example 000.176 ----> 0.176)
furthermore, it should not be possible to enter two commas (Example 0.187.56 -----> should not be Possible -------------> 0.18756)
Could somebody help me ?
Public Class Form1
Public LCD As Single
Public OPZ As String
Private Sub LCDfuellen(sender As Object, e As EventArgs) Handles btn3.Click, btn2.Click, btn1.Click, btn9.Click, btn8.Click, btn7.Click, btn6.Click, btn5.Click, btn4.Click
txtLCD.Text &= ActiveControl.Tag
Replace(LTrim(Replace(txtLCD.Text, "0", " ")), " ", "0")
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtLCD.Clear()
End Sub
Private Sub btnCE_Click(sender As Object, e As EventArgs) Handles btnCE.Click
'txtLCD.Text = txtLCD.Text.Remove(txtLCD.TextLength - 1, 1)
txtLCD.Text = LSet(txtLCD.Text, txtLCD.TextLength - 1)
End Sub
Private Sub OPZlesen(sender As Object, e As EventArgs) Handles btnPlus.Click, btnMinus.Click, btnMal.Click, btnDurch.Click
LCD = CSng(txtLCD.Text)
txtLCD.Clear()
OPZ = ActiveControl.Tag
End Sub
Private Sub btnGleich_Click(sender As Object, e As EventArgs) Handles btnGleich.Click
Dim activeLCD As Single
activeLCD = CSng(txtLCD.Text)
Select Case OPZ
Case "+" : txtLCD.Text = CStr(LCD + activeLCD)
Case "-" : txtLCD.Text = CStr(LCD - activeLCD)
Case "*" : txtLCD.Text = CStr(LCD * activeLCD)
Case "/" : txtLCD.Text = CStr(LCD / activeLCD)
End Select
End Sub
Private Sub btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click
txtLCD.Text &= ActiveControl.Tag
End Sub
Private Sub btnPunkt_Click(sender As Object, e As EventArgs) Handles btnPunkt.Click
txtLCD.Text &= ActiveControl.Tag
End Sub
End Class

Add a cancel button to input box VB

I have made a simple calculator and I would like to know if you put in a string instead of a double and if you press close or not. The program is a simple calculator where the menu is a box with buttons for addition, subtraction, multiplication, and division. I would like it if you enter a string a messagebox pops up that tells you to enter a number and when you enter an empty message or click cancel it takes you back to the start of the program. Here is the code
Public Class CalcForm
Property num1 As Double = Nothing
Property num2 As Double = Nothing
Private Sub CalcForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Addition_Click(sender As Object, e As EventArgs) Handles Addition.Click
Getnum()
Dim Answer As Double = num1 + num2
MessageBox.Show("The answer is " & Answer, "Answer")
End Sub
Private Sub Subtraction_Click(sender As Object, e As EventArgs) Handles Subtraction.Click
Getnum()
Dim Answer As Double = num1 - num2
MessageBox.Show("The answer is " & Answer, "Answer")
End Sub
Private Sub Multiplication_Click(sender As Object, e As EventArgs) Handles Multiplication.Click
Getnum()
Dim Answer As Double = num1 * num2
MessageBox.Show("The answer is " & Answer, "Answer")
End Sub
Private Sub Division_Click(sender As Object, e As EventArgs) Handles Division.Click
Getnum()
Dim Answer As Double = num1 / num2
MessageBox.Show("The answer is " & Answer, "Answer")
End Sub
Private Sub Getnum()
num1 = InputBox("Enter your first number", "Number 1")
num2 = InputBox("Enter your second number", "Number 2")
End Sub
End Class
Also I don't care about the difference between an empty ok or a cancel. But since I use doubles do I have to make it a string then convert to an double? If I do, how do I detect if the string has letters and ask the user to re-enter their number?
Do you really need your message box?
If you follow Hans Passant's recommendations and start using TextBox, you could simply bind your variable properties and parse and format them.
Have a look at the example below, it is more lines of code but gets you an opportunity to look at binding parsing and formatting.
It is a simple form with your four buttons and 3 text boxes. User cannot "get out" of the TextBox value is not numeric. Should you go for this solution, I would recommend to set the TextBox property for the result to ReadOnly :)
Imports System.Math
Imports System.ComponentModel
Public Class Form1
Implements INotifyPropertyChanged
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub NotifyPropertyChanged(ByVal info As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
End Sub
Private _num1 As Double
Property num1 As Double
Get
Return _num1
End Get
Set(value As Double)
If (value <> _num1) Then
_num1 = value
NotifyPropertyChanged("num1")
End If
End Set
End Property
Private _num2 As Double
Property num2 As Double
Get
Return _num2
End Get
Set(value As Double)
If (value <> _num2) Then
_num2 = value
NotifyPropertyChanged("num2")
End If
End Set
End Property
Private _Answer As Double
Property Answer As Double
Get
Return _Answer
End Get
Set(value As Double)
If (value <> _Answer) Then
_Answer = value
NotifyPropertyChanged("Answer")
End If
End Set
End Property
Private Sub CalcForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' TextBox for result
TextBox3.ReadOnly = True
Set_bindings()
End Sub
Private Sub Addition_Click(sender As Object, e As EventArgs) Handles Addition.Click
Answer = num1 + num2
End Sub
Private Sub Substraction_Click(sender As Object, e As EventArgs) Handles Substraction.Click
Answer = num1 - num2
End Sub
Private Sub Multiplication_Click(sender As Object, e As EventArgs) Handles Multiplication.Click
Answer = num1 * num2
End Sub
Private Sub Division_Click(sender As Object, e As EventArgs) Handles Division.Click
Answer = num1 / num2
End Sub
Private Sub Set_bindings()
Dim binH As Binding
TextBox1.DataBindings.Clear()
binH = New Binding("Text", Me, "num1", True, DataSourceUpdateMode.OnPropertyChanged)
AddHandler binH.Parse, AddressOf NumParser
AddHandler binH.Format, AddressOf NumFormatter
TextBox1.DataBindings.Add(binH)
TextBox2.DataBindings.Clear()
binH = New Binding("Text", Me, "num2", True, DataSourceUpdateMode.OnPropertyChanged)
AddHandler binH.Parse, AddressOf NumParser
AddHandler binH.Format, AddressOf NumFormatter
TextBox2.DataBindings.Add(binH)
TextBox3.DataBindings.Clear()
binH = New Binding("Text", Me, "Answer", True, DataSourceUpdateMode.Never)
AddHandler binH.Parse, AddressOf NumParser
AddHandler binH.Format, AddressOf NumFormatter
TextBox3.DataBindings.Add(binH)
End Sub
Private Sub NumParser(ByVal sender As Object, ByVal e As ConvertEventArgs)
If IsNumeric(e.Value) Then
e.Value = e.Value
End If
End Sub
Private Sub NumFormatter(ByVal sender As Object, ByVal e As ConvertEventArgs)
e.Value = FormatNum(e.Value)
End Sub
Private Function FormatNum(ByVal x As Double) As String
Dim sFormat As String
sFormat = ""
Select Case Abs(x)
Case 1 To 1000
sFormat = "##0.000"
Case Is > 10000
sFormat = "0.000E+00"
Case Is = 0
sFormat = "#0.0"
Case Is < 0.001
sFormat = "0.000E-00"
Case Is < 1
sFormat = "#0.0000"
End Select
FormatNum = Format(x, sFormat)
End Function
End Class

Square of value VB 2010

How do i write a program that square the value in the textbox and add it in the listbox each time the button is click. from 0 to 100000. thanks
This is what I have so far
Public Class frmSquare
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
Dim x As Double
Dim number As Double = txtNumber.Text
lstSquare.Items.Add(number * number)
x = number
End Sub
Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
Me.Close()
End Sub
End Class
Here's another way to do this, hope this helps:
If IsNumeric(TextBox1.Text) Then
Dim dbl As Double = CDbl(TextBox1.Text)
dbl *= dbl
ListBox2.Items.Add(dbl)
Else
MessageBox.Show("Value entered is not a Number")
End If

How to store last button clicked and add to listbox

So I'm doing this calculator program and need the numbers, the operator used and the "=" sign to show up in the listbox so "1 + 1 = 2" should show up. I have the calculator working and can move items to the listbox, but can't figure out how to remember the button that was clicked to get the outcome and move it over also.
Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & NumTwo.Text & Result.Text)
End Sub
End Class
As far as you just need to store one character, you can rely on the Tag property of ListBox1 (basically, a black box where you can store anything you want). Sample code:
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
ListBox1.Tag = "+"
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
ListBox1.Tag = "-"
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
ListBox1.Tag = "x"
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
ListBox1.Tag = "/"
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & ListBox1.Tag.ToString() & NumTwo.Text & "=" & Result.Text)
End Sub