Click here to Skip to main content
12,396,461 members (42,345 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB 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:48am
icecode346
Updated 22-Sep-12 8:58am
v3
Comments
Wes Aday 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? ;-)
Wes Aday 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

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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160721.1 | Last Updated 22 Sep 2012
Copyright © CodeProject, 1999-2016
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