Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to do baic subtraction using texbox21.text - texbox60.text = output to Label1. The math and function works.

This is for multiple rows of the same kind of equation but with different texboxes,
Simple math. But if the values are blank I get an exception Error. Here is my code please enlighten me on what I am missing.
VB
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim Net1 As Double
        Dim Net2 As Double
        Dim Loaded1 As Double
        Dim End1 As Double
        Loaded1 = Double.Parse(TextBox21.Text)
        End1 = Double.Parse(TextBox60.Text)
        Dim Loaded2 As Double
        Dim End2 As Double
        
        If TextBox21.Text And TextBox60.Text = False Then
            Net1 = False
        End If

        If TextBox21.Text And TextBox60.Text = True Then
            Loaded1 = Double.Parse(TextBox21.Text)
            End1 = Double.Parse(TextBox60.Text)
            Net1 = Loaded1 - End1
            Label2.Text = Net1.ToString
        End If
    End Sub
Posted
Updated 22-Sep-12 8:58am
v3
Comments
[no name] 22-Sep-12 14:49pm    
Use TryParse instead.
Kenneth Haugland 22-Sep-12 14:59pm    
Do you know what a texas box is? Is it related to guns in any way? ;-)
[no name] 22-Sep-12 15:04pm    
I think that it might be the batter's box for the Texas Rangers (baseball team) or the casket for someone named "Tex"
Sergey Alexandrovich Kryukov 22-Sep-12 22:55pm    
If multiple texas boxes don't work, use multiple oklahoma boxes, the more the better.
--SA

1 solution

Something like this should work:
VB
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    If TextBox1.Text = "" Or TextBox2.Text = "" Then
        Exit Sub
    End If

    Dim Net1 As Double
    Dim Net2 As Double

    If Double.TryParse(TextBox1.Text, Net1) And Double.TryParse(TextBox2.Text, Net2) Then
        Label2.Text = Net1 - Net2
    Else
        If Not Double.TryParse(TextBox1.Text, Net1) Then
            MessageBox.Show("Value in first textbox cannot be converted to a Double")
            Exit Sub
        Else
            MessageBox.Show("Value in second textbox cannot be converted to a Double")
            Exit Sub
        End If
    End If
End Sub
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900