0

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
1
  • dont use Val it does work like it used to in QBx-VB6. If you are displaying currency, saving to Integer will loose the decimal part, use a Decimal or Double variable. Since these appear to be user input, use Decimal.TryParse to get the values. Commented Feb 17, 2014 at 16:07

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

No problem. :-) Also, please note, that using "£" & somevar is cleaner then String.Format("£{0}", somevar)
1

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.

5 Comments

I used the code above and it worked, but thanks for your help regardless
That's ok... in that case you must have had decimal currency values and the code above would be OK.
@Mych: The reason I suggested to use doubles, is because you really never know how this program may be used in the long run. If this is for a business-level purpose, someone may notice that the values are rounded (up or down) and who will they go to? You, the developer (or the product support team). So, it is best to just write a couple extra characters and never come back to it. That is my opinion.
@MDTech.us_MAN I totally agree. I pointed this out in the originators original question but left as integer as in the past have been 'penalised' for changing types as the originator in that instance was adamant that the value would always be whole £'s. I also suggested as you see that the textboxes could be made to contain numbers only ( a £ could be added as a lable to the left of the box). This would reduce the code having to take care of any no numerics as they would not exist.
You would probably be able to force only numbers in the KeyDown or TextChanged events. I would probably use KeyDown, then you can actually allow £s and .s but no other characters except numbers. In the KeyDown event, you would probably need to check if the keycode is a number, '.' , '$' or '£' and if not, cancel the keydown.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.