Problem using CInt() with textbox

I have the following code, to calculate, in real time, the sum of tree textboxes.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged

Dim tx1 As Integer

tx1 = Me.TextBox1.Text

Dim tx2 As Integer

tx2 = Me.TextBox2.Text

Dim tx3 As Integer

tx3 = Me.TextBox3.Text

Me.Label1.Text = tx1 + tx2 + tx3

End Sub

But I get an error: The conversion of the caracter sequence " to type 'Integer' is not valid.

I feel this has to be really simple, but I'm stuck with this problem!

Thanks for your attention.

Lucas



Answer this question

Problem using CInt() with textbox

  • spotl

    I just noticed, you places that on the textchanged event so everytime a textchanged occurs, it wont work. You are best to do this on a button click. Also be sure that all the textboxes do have text in them (of integers).

  • kid_kaneda

    The following should work for you using the textchanged event but validating the textbox fields to ensure that we have three numeric fields - I'd changed the type to double to make it a little more flexible allowing any text to be entered but you could always restrict the textboxes to allow numbers of control character keypress only.

    Public Class Form1
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
    Try
    Me.Label1.Text = VerifyNumber(Me.TextBox1.Text) + VerifyNumber(Me.TextBox2.Text) + VerifyNumber(Me.TextBox3.Text)
    Catch ex As Exception
    Me.Label1.Text = ""
    MsgBox(ex.Message)
    End Try
    End Sub

    Function VerifyNumber(ByVal x As String) As Double
    If x.Length = 0 Then
    Return 0
    Else
    If IsNumeric(x) Then
    Return CType(x, Double)
    Else
    Throw New Exception("Is not numeric or blank")
    End If
    End If
    End Function
    End Class


  • Kevin Rodgers

    As stated, you have to test the value of the Text property and ensure that it can be converted to a decimal value. But it should be noted that the TextChanged event will be raised while the form is initially loading so you have to be ready to handle an empty string.

    The code for validating the input and doing the calculation can be placed on the TextChanged event, and when trimmed down, looks something like:

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged

    Dim val As Decimal

    Dim total As Decimal = 0

    If Decimal.TryParse(TextBox1.Text, val) Then

    total += val

    End If

    If Decimal.TryParse(TextBox2.Text, val) Then

    total += val

    End If

    If Decimal.TryParse(TextBox3.Text, val) Then

    total += val

    End If

    Me.Label1.Text = total.ToString

    End Sub

    Personally, I would add some indicator that appears when the text entered cannot be converted into a decimal - something like changing the color of the TextBox. The modified code might look like:

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged

    Dim val As Decimal

    Dim total As Decimal = 0

    If Decimal.TryParse(TextBox1.Text, val) Then

    total += val

    TextBox1.BackColor = SystemColors.Window

    TextBox1.ForeColor = SystemColors.WindowText

    Else

    TextBox1.BackColor = Color.Crimson

    TextBox1.ForeColor = Color.White

    End If

    If Decimal.TryParse(TextBox2.Text, val) Then

    total += val

    TextBox2.BackColor = SystemColors.Window

    TextBox2.ForeColor = SystemColors.WindowText

    Else

    TextBox2.BackColor = Color.Crimson

    TextBox2.ForeColor = Color.White

    End If

    If Decimal.TryParse(TextBox3.Text, val) Then

    total += val

    TextBox3.BackColor = SystemColors.Window

    TextBox3.ForeColor = SystemColors.WindowText

    Else

    TextBox3.BackColor = Color.Crimson

    TextBox3.ForeColor = Color.White

    End If

    Me.Label1.Text = total.ToString

    End Sub

    Hope that helps!



  • varunsagii

    you need to convert the values to an integer then do your calc and finally display it. You need to however be sure that the text is numeric otherwise it will fail so be sure to do some validation.

    this should work from your code snippet:

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged

    Dim tx1 As Integer = Convert.ToInt32(Me.TextBox1.Text)

    Dim tx2 As Integer = Convert.ToInt32(Me.TextBox2.Text)

    Dim tx3 As Integer = Convert.ToInt32(Me.TextBox3.Text)

    Dim totalCalc as Integer = tx1 + tx2 + tx3

    Me.Label1.Text = totalCalc.ToString()

    End Sub

    does this work



  • No-spam Sam

    To insure a numeric value and allow for decimals adjust the given code as follows

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged

    Dim tx1 As decimal

    Dim tx2 As decimal

    Dim tx3 As decimal

    If isNumeric(Me.TextBox1.Text) then

    tx1= Convert.Todecimal (Me.TextBox1.Text)

    else

    tx1=0

    end if

    if isNumeric((Me.TextBox2.Text)then

    tx2= Convert.Todecimal (Me.TextBox2.Text)

    else tx2 = 0

    end if

    if IsNumeric((Me.TextBox3.Text) then

    tx3= Convert.Todecimal (Me.TextBox3.Text)

    else

    tx3 = 0

    end if

    Dim totalCalc as Integer = tx1 + tx2 + tx3

    Me.Label1.Text = totalCalc.ToString()

    End Sub



  • Vl.K

    No... I have the text property of both textboxes set to 0, initially.

    But running the code, results the error: Input sequence was not in an incorrect format.

    What is this Not in an incorrect format

    Thanks,

    Lucas


  • Problem using CInt() with textbox