none
How to add two numbers from two text box? VB.net

    Question


  • Please read and help...

    Hi

    I have 8 tabs in windows form. I have a text box (test1) in tab5 and textbox (test2) in tab8. Both accepts only numbers.

    If the total value of the two box is greater than 99, then the messge box display and focus on one of the control box. it works fine only in the following condition:

    1. Run the windows form and click the first textbox in tab5 and then click on the textbox on tab8 - works fine if the total reaches 99.

    It fails and crash under the following condition.
     
    1. Run the windows form and click directly the textbox in tab8 - it crash when I leave the control.

    The code I am using is given below.

    Function
    PurchNoAddition() As Boolean

    If (Test2.Text) + +(Test2.Text) > 99 Then
    MsgBox(Test1.Text+ +Test2.Text)
    Test2.Focus
    End If
    End Function

    Private
    Sub PurchNo_leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Test2.Leave
    PurchNoAddition()
    End Sub

    Tuesday, December 19, 2006 10:55 PM

Answers

  • First off there appears to be a logic error in your code.  I believe

    If (Test2.Text) + +(Test2.Text) > 99 Then

    should be

    If (Test1.Text) + +(Test2.Text) > 99 Then 

    This will not fix your problem though.

    You get the error as test1 contains "" which can not be converted to a double.  You will also get the same error if you type in a character instead of a number in either of these text boxes.

    If you require these to be numeric fields only then i would recommend using the MaskedTextBox so that you can enforce numbers only.

    using either boxes will require you to include code like this though:

    Dim iTest1, iTest2 As Int16

    If Int16.TryParse(Test1.Text, iTest1) AndAlso Int16.TryParse(Test2.Text, iTest2) Then

    If (iTest1) + +(iTest2) > 99 Then

    MsgBox(iTest1 + +iTest2)

    Test2.Focus()

    End If

    End If

     

    Tuesday, December 19, 2006 11:58 PM

All replies

  • First off there appears to be a logic error in your code.  I believe

    If (Test2.Text) + +(Test2.Text) > 99 Then

    should be

    If (Test1.Text) + +(Test2.Text) > 99 Then 

    This will not fix your problem though.

    You get the error as test1 contains "" which can not be converted to a double.  You will also get the same error if you type in a character instead of a number in either of these text boxes.

    If you require these to be numeric fields only then i would recommend using the MaskedTextBox so that you can enforce numbers only.

    using either boxes will require you to include code like this though:

    Dim iTest1, iTest2 As Int16

    If Int16.TryParse(Test1.Text, iTest1) AndAlso Int16.TryParse(Test2.Text, iTest2) Then

    If (iTest1) + +(iTest2) > 99 Then

    MsgBox(iTest1 + +iTest2)

    Test2.Focus()

    End If

    End If

     

    Tuesday, December 19, 2006 11:58 PM
  • Hi

     

    Thanks a lot. Problem solved.

    THANKS

    Wednesday, December 20, 2006 3:01 PM