Question

I have 2 textboxes, one where I can enter a value and the "£" character is inside the textbox and in the second textbox it retrievs a value in the same format with the "£" character from mysql table which needs to be added to the first value.

What happens is when I enter values in the first textbox it doesn't get added to the second textbox, they both just stay the same. Can't see what is wrong with my code:

Private Sub txtsurcharges_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsurcharges.TextChanged
    Dim c As Integer
    c = Val(txtsurcharges.Text) + Val(txttotal.Text)
    txttotal.Text = c
End Sub
Was it helpful?

Solution

Try something like this:

Private Sub txtsurcharges_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtsurcharges.TextChanged
    Dim c As Double 'It is bad to use integers for currency, as integers cut off decimals.'
    Dim surcharges As Double
    Dim total As Double    'We also need to remove the £ char.'
    If Double.TryParse(txtsurcharges.Text.Replace("£", ""), surcharges) = False Then
        'It looks like it isnt possible to parse the string, probably because there is some special character or letter.'
        'Some type of error handling will go here. (MsgBox or something else)'
    End If
    If Double.TryParse(txttotal.Text.Replace("£", ""), total) = False Then
        'It looks like it isnt possible to parse the string, probably because there is some special character or letter.'
        'Some type of error handling will go here. (MsgBox or something else)'
    End If
    c = surcharges + total
    txttotal.Text = "£" & CStr(c)
End Sub

Hope this helps.

OTHER TIPS

Also make sure you have AutoPostBack=true on txtsurcharges or your code will not fire.

See my answer in your previous q.... Adding the values of 2 textboxes to display result in one of them

MDTech.us_MAN also makes the same point of using integers to add currency as I did. But then you may only have whole £ to deal with - you did not specify.

Finally I would add a filter that would limit input into txtsurcharges to numerics only.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top