Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 9:48am
icecode346
Edited 22-Sep-12 9:58am
v3
Comments
Wes Aday at 22-Sep-12 14:49pm
   
Use TryParse instead.
Kenneth Haugland at 22-Sep-12 14:59pm
   
Do you know what a texas box is? Is it related to guns in any way? ;-)
Wes Aday at 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 at 22-Sep-12 22:55pm
   
If multiple texas boxes don't work, use multiple oklahoma boxes, the more the better.
--SA

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 305
1 OriginalGriff 225
2 DamithSL 130
3 Kornfeld Eliyahu Peter 130
4 Peter Leow 95
0 OriginalGriff 7,355
1 DamithSL 5,199
2 Sergey Alexandrovich Kryukov 4,942
3 Maciej Los 4,906
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100