Calculate Button Twice

In response to the following MSDN Forum Thread(Calculate Button Twice)

The original poster asked the following question:

Hello,

I’m having trouble with a simple code in vb. For some reason I’m having to push my calculate button twice to get the result I need with this simple calculator. Anyone see where I may have made a mistake?

Here is the code:

    Public Class Form1
        Dim Number1 As Decimal
        Dim Number2 As Decimal
        Dim Answer As Decimal
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            CheckBox1.Checked = True
            CheckBox2.Checked = False
            CheckBox3.Checked = False
            CheckBox4.Checked = False
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Number1 = Val(TextBox1.Text)
            Number2 = Val(TextBox2.Text)
            Label4.Text = Answer

            If TextBox1.Text = "" OrElse TextBox2.Text = "" Then MsgBox("No Number(s) is given", MsgBoxStyle.Critical, "Error")

            If CheckBox1.Checked = True Then Answer = Number1 + Number2
            If CheckBox2.Checked = True Then Answer = Number1 - Number2
            If CheckBox3.Checked = True Then Answer = Number1 * Number2
            If CheckBox4.Checked = True Then Answer = Number1 / Number2

            If CheckBox1.Checked = False And CheckBox2.Checked = False And CheckBox3.Checked = False And CheckBox4.Checked = False Then MsgBox("No Operators is Checked!", MsgBoxStyle.Critical, "Error")
            If CheckBox1.Checked = True And CheckBox2.Checked = True Then MsgBox("You can not have more than one item checked", MsgBoxStyle.Critical, "Error")
            If CheckBox1.Checked = True And CheckBox3.Checked = True Then MsgBox("You can not have more than one item checked", MsgBoxStyle.Critical, "Error")
            If CheckBox1.Checked = True And CheckBox4.Checked = True Then MsgBox("You can not have more than one item checked", MsgBoxStyle.Critical, "Error")

            If CheckBox2.Checked = True And CheckBox3.Checked = True Then MsgBox("You can not have more than one item checked", MsgBoxStyle.Critical, "Error")
            If CheckBox2.Checked = True And CheckBox4.Checked = True Then MsgBox("You can not have more than one item checked", MsgBoxStyle.Critical, "Error")

            If CheckBox3.Checked = True And CheckBox4.Checked = True Then MsgBox("You can not have more than one item checked", MsgBoxStyle.Critical, "Error")
        End Sub
    End Class

When you are expecting to display a result after calculating it, it is important to write code that executes in that order.

Since you are only displaying your result after it is calculated, I also suggest using variables in a block scope verses using variables in a class scope.

I also suggest making the following changes to your code:

Option Strict On
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        rbSubtraction.Checked = True
        rbAddition.Checked = False
        rbDivision.Checked = False
        rbMultiplication.Checked = False
    End Sub
    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        Dim Number1 As Decimal = 0D
        Dim Number2 As Decimal = 0D
        Dim Answer As Decimal = 0D
        Decimal.TryParse(tbNumber1.Text, Number1)
        Decimal.TryParse(tbNumber2.Text, Number2)
        Dim msg As String() = {"You must provide a Number {0}"}
        Select Case True
            Case tbNumber1.Text = String.Empty : lblResult.Text = String.Format(msg(0), "1") : Exit Sub
            Case tbNumber2.Text = String.Empty : lblResult.Text = String.Format(msg(0), "2") : Exit Sub
            Case rbSubtraction.Checked : Answer = Number1 - Number2
            Case rbAddition.Checked : Answer = Number1 + Number2
            Case rbMultiplication.Checked : Answer = Number1 * Number2
            Case rbDivision.Checked : Answer = Number1 / Number2
        End Select
        lblResult.Text = Answer.ToString
    End Sub
    Private Sub tbNumber1_KeyDown(sender As Object, e As KeyEventArgs) Handles tbNumber2.KeyDown, tbNumber1.KeyDown
        Dim count As Integer = (From c As Char In DirectCast(sender, TextBox).Text Where c = "."c Select c).Count
        Select Case True
            Case {8, 46, 37, 38, 39, 40}.ToList.IndexOf(e.KeyCode) > -1 : Exit Sub
            Case e.KeyCode = Keys.Decimal OrElse e.KeyCode = Keys.OemPeriod
                If tbNumber1.SelectionStart = 0 Then Exit Select
                If count > 0 Then Exit Select Else Exit Sub
            Case Char.IsDigit(ChrW(e.KeyCode)) : Exit Sub
        End Select
        e.SuppressKeyPress = True
        e.Handled = True
    End Sub
End Class

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s