Okay, im trying to write a simple calculator. Heres my code:

Public Class Form1
    Dim value1 As Double
    Dim value2 As Double
    Dim sign As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = " " Then TextBox1.Text = "1" Else 
        TextBox1.Text = TextBox1.Text & "1"
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        value1 = textbox1.text
        If TextBox1.Text = " " Then TextBox1.Text = "2" Else 
        TextBox1.Text = TextBox1.Text & "2"
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If TextBox1.Text = " " Then TextBox1.Text = "+" Else 
        TextBox1.Text = TextBox1.Text & "+"
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        value2= textbox1.Text        
        If sign = "-" Then TextBox1.Text = value1 - value2 Else 
        If sign = "+" Then TextBox1.Text = value1 + value2 Else 
        If sign = "*" Then TextBox1.Text = value1 * value2 Else 
        If sign = "/" Then TextBox1.Text = value1 / value2
        If TextBox1.Text = " " Then TextBox1.Text = "=" Else 
        TextBox1.Text = TextBox1.Text & "="
    End Sub
End Class

the problem i have is that the "value1= textbox1.Text" and "value2= textbox1.Text" don't work.

any help would be appreciated =)

Recommended Answers

All 2 Replies

it cause if textbox1 or textbox2 empty and it's empty string. value1 and value2 is double, so you cannot assign value directly.
you can change your assignment with this code :

value1 = Val(TextBox1.Text)

k this is my new script:

Public Class Form1
    Dim value1 As Double
    Dim value2 As Double
    Dim sign As String

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = " " Then
            TextBox1.Text = Button1.Text
        Else
            TextBox1.Text = TextBox1.Text & Button1.Text
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click 
        If TextBox1.Text = " " Then
            TextBox1.Text = Button2.Text
        Else
            TextBox1.Text = TextBox1.Text & Button2.Text
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        value1 = TextBox1.Text
        If TextBox1.Text = " " Then
            TextBox1.Text = "+"
        Else
            TextBox1.Text = TextBox1.Text & "+"
        End If
        sign = Button3.Text
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        value2 = TextBox1.Text
        Select Case sign
            Case "-"
                TextBox1.Text = value1 - value2
            Case "+"
                TextBox1.Text = value1 + value2
            Case "*"
                TextBox1.Text = value1 * value2
            Case "/"
                TextBox1.Text = value1 / value2
        End Select
    End Sub
End Class

but when i press "equal" the program crashes =(

plz help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.