Question

I have a textbox in a VB.Net form binded to a Double.
The problem is that on binding validation it changes numbers like "11.7" to "117" because the Double conversion doesn't accept the dot.

How can I solve this problem without changing the dot to a comma?

Was it helpful?

Solution

You need the set the proper culture info so that the framework can know the correct decimal separator.

Imports System.Threading
Imports System.Globalization

Add the following code in the contructor (Sub New) of your start up form.

Dim ci As New CultureInfo("it-IT")

ci.NumberFormat.NumberDecimalSeparator = "."

Thread.CurrentThread.CurrentCulture = ci
Thread.CurrentThread.CurrentUICulture = ci

Here's a list of culture codes where you can find yours:

http://msdn.microsoft.com/en-us/library/ee825488%28v=cs.20%29.aspx

Edit

Another way is to subscribe to the parse event of the binding.

Dim b As New Binding("Text", TheDataSource, "TheDoubleField")

AddHandler b.Parse, Sub(s As Object, e As ConvertEventArgs) e.Value = CStr(e.Value).Replace(".", ",")

Me.TextBox1.DataBindings.Add(b)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top