Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
The following code snippet works absolutely great when run on Windows 7 professional BUT throws an Invalid Cast Exception when run on Windows 10.
Any idea what Microsoft have changed and what I need to do to get it working on Win10?
It's a friend of mine running it on a Win10 pc & I don't have it on my pc :-)

Many thanks,
Tim

************** Exception Text **************
System.InvalidCastException: Conversion from string "£0.00" to type 'Decimal' is not valid.
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToDecimal(String Value, NumberFormatInfo NumberFormat)
   at Microsoft.VisualBasic.CompilerServices.Conversions.ToDecimal(String Value)
   at GtracsX4.gtac1b.tBox_Validating(Object sender, CancelEventArgs e)


The Code
VB
Private Sub tBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles _
      amtInp.Validating, DescInp.Validating, RefInp.Validating
    Dim mtb As String = sender.tag : Dim mtbx As TextBox = sender : Dim ta As Decimal
    If mtb = "dec" Then
        If (String.IsNullOrEmpty(mtbx.Text)) Then mtbx.Text = "0"
        If IsNumeric(mtbx.Text) = False Then
            msgM = "Only NUMBERS allowed.  Please re-enter"
            msg = New gtzMess(1, "", msgM, "OK", "", "", "S")   '   flags = ISQW
            msg.ShowDialog()
            mtbx.Text = 0
            e.Cancel = True
            Exit Sub
        End If
        ta = CDec(mtbx.Text)
        mtbx.Text = "£" & ta.ToString("N2")
        If mtbx.Name = "amtInp" Then tAmt.Text = CDec(mtbx.Text)
    End If
End Sub
Posted
Comments
Michael_Davies 7-Sep-15 14:45pm    
Off the question topic...Why copy the sender.tag to a new variable when all you do is test it = "dec"? Save time and stack just test sender.tag.

Second, why not use the KeyPress event and block non number keys so you never have to verify anything other than empty.

Simple example:

Public Const DecimalStrings As String = ".0123456789" + ChrW(8)

Private Sub DecimalKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles yourtextbox.KeyPress
'
' Ignore non-numeric keys, allow 0-9 and decimal stop
'
If DecimalStrings.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
ElseIf e.KeyChar = "." Then
'
' If there already is a decimal stop in the box ignore duplicates
'
If CType(sender, TextBox).Text.IndexOf(".") <> -1 Then
e.Handled = True
End If
End If

If e.KeyChar = ChrW(13) Then
Me.SelectNextControl(sender, True, True, True, True)
End If

etc.

Why are you doing that?
Look at your code:
VB
ta = CDec(mtbx.Text)
mtbx.Text = "£" & ta.ToString("N2")
If mtbx.Name = "amtInp" Then tAmt.Text = CDec(mtbx.Text)

VB
ta = CDec(mtbx.Text)

Take a Textbox value, and convert it to a Decimal. This is a poor idea: you should be using Decimal.TryParse and reporting problems to the user.
VB
mtbx.Text = "£" & ta.ToString("N2")

Having converted it to a decimal value, you immediately convert it back to a tring, and tack a currency symbol on the front.
VB
If mtbx.Name = "amtInp" Then tAmt.Text = CDec(mtbx.Text)

You then convert that back to a decimal (which will throw away the currency, if it doesn't cause it to fail, which it does) in order to convert it back into a string, to put it in a textbox.

That's...um...very odd.
Use TryParse to convert it (with reporting), and look at what you are trying to store where - you should need at most two type conversions: one to a dec, and one to a string. And that's only because your specify a format for the string value!
And your problem will go away at the same time.
 
Share this answer
 
Comments
Member 11890082 8-Sep-15 5:37am    
Whilst I very much appreciate your responses I would like to make the following points.

I realise you all think my code is not that good which is hardly surprising as I'm not a programmer.

The code works perfectly on Windows 7 but run the upgrade to Windows 10 and it starts causing errors so downgrade back to Windows 7 and hey presto it all works fine again.

IMHO this sort of proves that Microsoft did something on the upgrade.

So I'll close this question as everyone here thinks it's my code. It's just that I don't.

Thanks.
Tim
It is not related to Windows version, it's related to the different default culture set up in two different cases you described. Your code should either be culture-neutral, or you have to force certain fixed culture in the application itself, which is a per-thread option:
https://msdn.microsoft.com/en-us/library/system.threading.thread.currentculture(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.threading.thread.currentuiculture(v=vs.110).aspx[^].

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900