Thursday, May 22, 2008

Draft A Letter For Short-term Traineeship

VSS VB.net and the decimal point in the textbox

Hello

The temita Eastern cultural references and combined with what he says the truth locale that is more than a headache for At least for me I'm no guru on this. One of the few things I miss the dbase was the picture command with a simple model very easily limited data entries above the numbers. Here in vb.net give us the "easy" to program for different cultures so that the result can be much more friendly to the end user and the system will behave according to what he says the locale of the computer, the Burrera is that programming is very complicated, well ... either case.

Here is an recetita to handle the textbox with numbers so as to use the dot "." decimal separator regardless of what you set on the control panel, Assume that the machine has to eat, "as the decimal point and thousands separator (as is usually in Latin America) Place a

textbox, a button in a winform and paste the following code in the codebehind

Private Sub Form1_Load ( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase .
Load Dim x As Decimal
x = 25,356 'Look how the assignment is to point
I . TextBox1 . Text = x
End Sub Private

Sub Button1_Click (ByVal sender As System.Object, ByVal and As System.EventArgs) Handles
Button1.Click Dim x As Decimal
x = I . TextBox1.Text 'From here comes with coma and almost automatically becomes
MsgBox (x)
End Sub

So far everything works very well as placing a number with a decimal point textbox becomes comatose as says the locale and then passing a textbox to a number become the comma point.

Well now assume that the user has the "silly idea" to use for a decimal point. that it is a "complacent" who wants to enter numbers with speed and comfort using the keypad on your keyboard. make the attempt in the textbox we already have on the screen and type " 25,356 ", then press and see that they get: "25356 " what happened?? it turns out that the very intelligent VB thought that we are using the thousands separator "." and discarded. Well

to give the user the convenience of using your numeric keypad can build a user control and monitor each press of keys to bind or set point using the events of textbox to do the same or we can make a simpler validation in time to retrieve and assign values \u200b\u200bof textbox to numbers and vice versa. While the latter to extend a textbox and a button on the winform and replace the previous code as follows:

Private Sub Form1_Load ( ByVal sender As System.Object, ByVal and As System.EventArgs) Handles MyBase .

Load

Dim x As Decimal
x = 25,356 'Look how the assignment is to point
I . TextBox1.Text = x
I . TextBox2.Text = x.ToString (System.Globalization.CultureInfo.InvariantCulture)

End Sub

Private

Sub Button1_Click (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles

Button1.Click Dim x As Decimal

x = I . TextBox1.Text 'From here comes to eat and almost automatically becomes

If x.ToString \u0026lt;> I . TextBox1.Text Then

MsgBox (

"Not a valid number, use the decimal point" , MsgBoxStyle.Critical)
Return

End If
MsgBox(x)
End Sub

Private Sub Button2_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

Dim x As Decimal = Decimal .Parse( Me . TextBox2.Text, System.Globalization.CultureInfo.InvariantCulture)

If x.ToString (System.Globalization.CultureInfo.InvariantCulture) \u0026lt;> I . TextBox2.Text . Trim
Then MsgBox

(

"Not a valid number, be sure to use decimal point" , MsgBoxStyle.Critical)
Return


End If
MsgBox (x)
End Sub

Well now I will explain the changes I made to the form: let's look at the load event the following statement:

I . TextBox2.Text = x.ToString (System.Globalization.CultureInfo.InvariantCulture)

This operation it does is get a reference to "cultural body of all languages" which is based on English culture, as you know runs the point like a decimal point, and therefore what we are accomplishing is ignore the locale!! For this reason you will see that on the screen (textbox2) appears as a number with a decimal point, just what I wanted to achieve.

Now we will see the representative of buuton2 where we recover the value of the textbox and place it in the numeric variable

Dim x As Decimal = Decimal . Parse ( Me . TextBox2.Text, System.Globalization.CultureInfo.InvariantCulture)

In this line of textbox convert the string through Decimal Parse method of repeating the request for the independent of the locale, this is because we assume that the text is decimal separator. Finally

analyze validation that ensures we are using the dot as thousands

If x.ToString (System.Globalization.CultureInfo.InvariantCulture) \u0026lt;> I . TextBox2.Text.Trim Then

MsgBox (

"Not a valid number, be sure to use decimal point" , MsgBoxStyle.Critical)

Return

End If

Validation is as simple as comparing the string obtained from the variable x against the text stored in which we did textbox2 conversion. If they are different is that the user used the comma if they are equal is that the user used the point and everything is fine.

This example will you put the 2 options that are driving around with regional settings (managed by textbox1 and button1) and management with point as decimal separator (managed by textbox2 and button2)

I hope they can

Salu2
SergioT

0 comments:

Post a Comment