vb message help

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
Im making a calculator with a number pad. When I type the code, I get a message and the form designer is not loading.
Message:
Message 1 Exception from HRESULT: 0x80041FEB 0 0
Code:Option Explicit On
Imports System.Math
Imports System.Windows.Forms
Imports Microsoft.VisualBasic


Public Class frmCalculator

Dim HasResult As Boolean = False

Dim FirstNumber As Single
Dim SecondNumber As Single
Dim Arthemetic As String

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click

FirstNumber = Val(lblResult.Text)
lblResult.Text = "0"
Arthemetic = "+"

End Sub

Private Sub btnSubtract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubtract.Click

FirstNumber = Val(lblResult.Text)
lblResult.Text = "0"
Arthemetic = "-"

End Sub

Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click

FirstNumber = Val(lblResult.Text)
lblResult.Text = "0"
Arthemetic = "X"

End Sub

Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click

FirstNumber = Val(lblResult.Text)
lblResult.Text = "0"
Arthemetic = "/"

End Sub

Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click

SecondNumber = Val(lblResult.Text)

Select Case Arthemetic

Case "+"
lblResult.Text = FirstNumber + SecondNumber
Case "-"
lblResult.Text = FirstNumber - SecondNumber
Case "X"
lblResult.Text = FirstNumber * SecondNumber
Case "/"
lblResult.Text = FirstNumber / SecondNumber
If SecondNumber = 0 Then
lblResult.Text = "Cannot Divide By Zero"
Exit Sub
End If

End Select

HasResult = True


End Sub

Private Sub btnDecimalPlace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimalPlace.Click

If lblResult.Text.IndexOf(".") >= 0 Then
lblResult.Text = lblResult.Text
Else
lblResult.Text = lblResult.Text & "."
End If

End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

lblResult.Text = "0"
FirstNumber = 0
SecondNumber = 0
Arthemetic = ""

End Sub

Private Sub btnClearError_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearError.Click

lblResult.Text = "0"

If Arthemetic > "" Then
SecondNumber = 0
End If

End Sub

Private Sub Digit(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click

If HasResult Then btnClear.PerformClick() : HasResult = False

If lblResult.Text = "0" Then
lblResult.Text = sender.text
Else
lblResult.Text &= sender.text
End If

End Sub


Private Sub frmCalculator_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown

Select Case e.KeyCode

Case Keys.Escape : btnClear_Click(btnClear, e)
Case Keys.Back : If lblResult.Text.Length > 0 Then lblResult.Text = lblResult.Text.Remove(lblResult.Text.Length - 1, 1) : FirstNumber = CSng(lblResult.Text)
Case Keys.Enter : btnEquals.PerformClick()

Case Keys.Add : Arthemetic = "+" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0"
Case Keys.Subtract : Arthemetic = "-" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0"
Case Keys.Divide : Arthemetic = "/" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0"
Case Keys.Multiply : Arthemetic = "*" : FirstNumber = CSng(lblResult.Text) : lblResult.Text = "0"


Case Keys.Decimal : btnDecimalPlace.PerformClick()
Case Keys.NumPad1, Keys.D1 : Digit(btn1, e)
Case Keys.NumPad2, Keys.D2 : Digit(btn2, e)
Case Keys.NumPad3, Keys.D3 : Digit(btn3, e)
Case Keys.NumPad4, Keys.D4 : Digit(btn4, e)
Case Keys.NumPad5, Keys.D5 : Digit(btn5, e)
Case Keys.NumPad6, Keys.D6 : Digit(btn6, e)
Case Keys.NumPad7, Keys.D7 : Digit(btn7, e)
Case Keys.NumPad8, Keys.D8 : Digit(btn8, e)
Case Keys.NumPad9, Keys.D9 : Digit(btn9, e)
Case Keys.NumPad0, Keys.D0 : Digit(btn0, e)

End Select

End Sub
End Class
Any help please?
Ali Hamza Mohammed

View the full article
 
Back
Top