-2

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?

Doozku
  • 41
  • 5

1 Answers1

0

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
Ben238
  • 98
  • 7
  • Yeah, I was a real noob when I made this. Since then, I've never used inputboxes again. Thanks for the help. – Doozku Jul 16 '15 at 02:31