my first program

hy

ive made my first program today after watching some of the videos on this site.

Now i made a calculation program and it works fine until u type in numbers that deal with a , . like 0.00675.

first my val1 and val2 were integers so they cant have , so i maked then double.

but it still dont work.

heres my code for the calculate button:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'make vars val1 and val2

Dim val1 As Double

Dim val2 As Double

Dim opr As String

'set vars val1 and val2 and opr equal to textbox 1,3 and 2 text

Try

val1 = TextBox1.Text

Catch ex As Exception

MessageBox.Show("First number is not a number.")

GoTo empty

End Try

Try

val2 = TextBox2.Text

Catch ex As Exception

MessageBox.Show("Second number is not a number.")

GoTo empty

End Try

Try

opr = tbopr.Text

Catch ex As Exception

MessageBox.Show("Operation must be a string")

GoTo empty

End Try

'show message box with val1+val2

Try

Dim answer As Double

If opr = "+" Then

answer = val1 + val2

Else

If opr = "-" Then

answer = val1 - val2

Else

If opr = "*" Then

answer = val1 * val2

Else

If opr = "/" Then

answer = val1 / val2

Else

MessageBox.Show("false operation")

GoTo empty

End If

End If

End If

End If

TextBox3.Text = answer

Catch ex As Exception

MessageBox.Show("false operation")

GoTo empty

End Try

Exit Sub

empty:

'empty textbox 1 and 2

TextBox1.Text = ""

TextBox2.Text = ""

tbopr.Text = ""

End Sub

ive also have a download link for my program:

http://host-a.net/getfile.php usern=ozakigames&file=calc.zip

thanks in advance

bye




Answer this question

my first program

  • jfyfe

    In support of DMan's question, I'd ask the poster to read the suggestions at the top of the forum.



  • Gleesha

    hi,

    it will not work because of GoTo's and your lable so if anything was wrong your program will enter an endless recursion, your can use

    var = Double.parse(textbox1.text)

    it will be better

    hope this helps



  • Ravi Kumar p

    thanks!!

    it works now

    and i learned something

    bya!



  • lponiew

    When you say it doesn't work, what is happening Are you getting an error What is the error message

  • Martín Misol

    hi,

    actualy it doesn't handle more than 2 values , just 2 value is enough like any other calculater one value entered by the user and the other one is saved in the memory(variable) like for example

    textbox to capture the user entry and to display results

    when user type int the textbox 123 and hit a sign(+ - * /) the memory will save 123 when the user type a new value and hit (=) the operation will done memoryVar + textbox.text = value then the memory will hold the new value memroy var = value so if the user make other operation it will be performed againest total value in the memory and the textbox.text

    hope this helps



  • SteveInMA

    hi,

    i don't understand why do you keep all those goto statment, and why do you parse things twice

    you have to check your values once , and calculate it in the next step if its truevalue

    1) you can use double.tryparse(variablethatcontainvalue,variabletoholdvalue) for example your textbox1, var1 if this function was able to parse textbox1 it will assign the result to var1, and this function return true if successfull or false if it failed

    2) to make your result textbox appear as double you have to use string.format method

    so your code will look something like this


    Public Class Form1
    Private Sub btequal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btequal.Click
    Dim checkResult As Boolean

    Dim var1 As Double

    Dim var2 As Double

    checkResult = Double.TryParse(Me.TBvar1.Text, var1)
    If Not checkResult Then

    MessageBox.Show("var1 doesn't contain number")
    Me.TBvar1.Focus()
    Return

    End If

    checkResult = Double.TryParse(Me.TBvar2.Text, var2)
    If Not checkResult Then

    MessageBox.Show("var2 doesn't contain number")
    Me.TBvar2.Focus()
    Return

    End If

    Me.TBresult.Text = String.Format("{0:f2}", operate(var1, Me.tboperation.Text, var2))
    End Sub

    Private Function operate(ByVal var1 As Double, ByVal sign As String, ByVal var2 As Double) As Double

    Select Case sign
    Case "+"

    Return var1 + var2
    Case "-"

    Return var1 - var2
    Case "*"

    Return var1 * var2
    Case "/"

    Return var1 / var2
    End Select

    End Function
    End
    Class


    hope this helps



  • Paddy Mac

    2cond question:

    how u make it so that it handles more than 2 values behind the comma

    thanks in advance!



  • DogGuts

    hy

    i changed my code but stil it dont sees dont commas

    my code:

     

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'make vars val1 and val2

    Dim val1 As Double

    Dim val2 As Double

    Dim opr As String

    'set vars val1 and val2 and opr equal to textbox 1,3 and 2 text

    Try

    val1 = Double.Parse(TextBox1.Text)

    Catch ex As Exception

    MessageBox.Show("First number is not a number.")

    GoTo empty

    End Try

    Try

    val2 = Double.Parse(TextBox2.Text)

    Catch ex As Exception

    MessageBox.Show("Second number is not a number.")

    GoTo empty

    End Try

    Try

    opr = tbopr.Text

    Catch ex As Exception

    MessageBox.Show("Operation must be a string")

    GoTo empty

    End Try

    'show message box with val1+val2

    Try

    Dim answer As Double

    If opr = "+" Then

    answer = Double.Parse(val1) + Double.Parse(val2)

    Else

    If opr = "-" Then

    answer = Double.Parse(val1) - Double.Parse(val2)

    Else

    If opr = "*" Then

    answer = Double.Parse(val1) * Double.Parse(val2)

    Else

    If opr = "/" Then

    answer = Double.Parse(val1) / Double.Parse(val2)

    Else

    MessageBox.Show("false operation")

    GoTo empty

    End If

    End If

    End If

    End If

    TextBox3.Text = answer

    Catch ex As Exception

    MessageBox.Show("false operation")

    GoTo empty

    End Try

    Exit Sub

    empty:

    'empty textbox 1 and 2

    TextBox1.Text = ""

    TextBox2.Text = ""

    tbopr.Text = ""

    Exit Sub

    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click

    Dim gtform As New SplashScreen1

    gtform.Show()

    End Sub

    Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Me.Hide()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    'make vars val1 and val2

    Dim val1 As Double

    Dim val2 As Double

    Dim opr As String

    'set vars val1 and val2 and opr equal to textbox 1,3 and 2 text

    Try

    val1 = Double.Parse(TextBox1.Text)

    Catch ex As Exception

    MessageBox.Show("First number is not a number.")

    GoTo empty

    End Try

    Try

    val2 = Double.Parse(TextBox2.Text)

    Catch ex As Exception

    MessageBox.Show("Second number is not a number.")

    GoTo empty

    End Try

    Try

    opr = tbopr.Text

    Catch ex As Exception

    MessageBox.Show("Operation must be a +, -, / or *")

    GoTo empty

    End Try

    'show message box with val1+val2

    Try

    Dim answer As Double

    If opr = "+" Then

    answer = Double.Parse(val1) + Double.Parse(val2)

    Else

    If opr = "-" Then

    answer = Double.Parse(val1) - Double.Parse(val2)

    Else

    If opr = "*" Then

    answer = Double.Parse(val1) * Double.Parse(val2)

    Else

    If opr = "/" Then

    answer = Double.Parse(val1) / Double.Parse(val2)

    Else

    MessageBox.Show("false operation")

    GoTo empty

    End If

    End If

    End If

    End If

    Dim anscalc As Double

    anscalc = TextBox3.Text + answer

    TextBox3.Text = anscalc

    Catch ex As Exception

    MessageBox.Show("false operation")

    GoTo empty

    End Try

    Exit Sub

    empty:

    'empty textbox 1 and 2

    TextBox1.Text = ""

    TextBox2.Text = ""

    tbopr.Text = ""

    Exit Sub

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    TextBox1.Text = "0"

    tbopr.Text = ""

    TextBox2.Text = "0"

    TextBox3.Text = "0"

    End Sub

    End Class

     

     

    i dont understand it anymore

     

    if i do 0.002 + 0.002 it says 4

    if i do 1.02 + 1.301 it says 2321

     

    thanks for repleis!!

    bya



  • my first program