Click here to Skip to main content
Sign Up to vote bad
good
See more: VB.NET
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.
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 22 Sep '12 - 8:48
icecode313
Edited 22 Sep '12 - 8:58

Comments
Wes Aday - 22 Sep '12 - 14:49
Use TryParse instead.
Kenneth Haugland - 22 Sep '12 - 14:59
Do you know what a texas box is? Is it related to guns in any way? ;-)
Wes Aday - 22 Sep '12 - 15:04
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:55
If multiple texas boxes don't work, use multiple oklahoma boxes, the more the better. --SA

1 solution

Something like this should work:
    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
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 365
1 OriginalGriff 315
2 Slacker007 240
3 Dave Kreskowiak 212
4 Aarti Meswania 210
0 Sergey Alexandrovich Kryukov 8,893
1 OriginalGriff 7,134
2 CPallini 3,678
3 Rohan Leuva 3,036
4 Maciej Los 2,428


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid