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

Thursday, May 15, 2008

Cubefield Armour Games

Variables postbacks persistent sources

One problem that we face as we program in asp.net is the fact that the variables declared in a webform "die" after the first postback to save variables between postbacks there are several alternatives, the use of Session, Application or ViewState, which is used depends on what you want to do, very simple I detail when to use each of the 3 options Session

  • : You must use if you want to store variables and objects throughout the session, a session means a connection to the web server from a browser. The variables placed in Session live while the explorer is opened and interacted with the site. to put variables here just to do the following: dim

    xVar
    as integer = 199 Session ("myvariable") = xVar

    and to recover

    dim z as integer = session ("myvariable")
  • Application: Variables stored here while living application live on the web server, usually are placed here and Obet variables to be used by all users using the application, So yes, if one changes the value of the variable stored in application, this change is reflected to all connected users (all sessions). To save variables or objects becomes:

    xVar dim as integer = 199
    Application ("myvariable") = xVar

    and retrieve

    dim z as integer = Application ("myvariable") ViewState
  • : The variables or stored objects that live here live a webform, and ViewState is ASP.Net mechanism to save web control values \u200b\u200bin postbacks, it is actually a hidden variable that the customer comes and goes with the values \u200b\u200bof WebForm controls. What makes this mechanism usually take the opportunity here to save themselves the form variables are important only in the webform, it is therefore not advisable to store them in Session or Application. ViewSat To do it:


    dim as integer = 199 xVar
    Me.ViewState ("myvariable") = xVar

    and retrieve

    dim z as integer = Me.ViewState ("myvariable")

In either the 3 mechanisms have to be careful not to try to retrieve a nonexistent variable that can cause a runtime error and returned the 3 mechanisms are nothing if not variable

For the next installment I'll show you how to save their own objects in these using the serialization mechanisms, something very basic and easy to use. Salu2


Sergio