Managing User Input

Managing User Input

Global applications pose several special considerations when planning for the manipulation and validation of user input. When attempting to validate non-Latin character user input, it may be necessary to extract single characters in sequence through string indexing, whereas the comparison and sort order of a simple alphabetic sort routine may produce different results depending on the culture used.

String Indexing

The Unicode standard supports 32-bit extended characters (surrogate characters) and 32-bit characters that are formed by combining two 16-bit Unicode characters (combined characters), such as an accent mark and the base character to which it applies. In order to evaluate these characters amid other normal input characters, it is sometimes necessary to parse an input string, character by character, for validation, taking into account the fact that some characters may be 16 bit and others 32 bit.

The .NET Framework provides support for this type of string manipulation through the System.Globalization.StringInfo class. You can use this class to iterate through the elements in a string by repeatedly calling its MoveNext method. This method returns a zero value when it has exhausted all values in a character string, as shown in this example:

  1. Open Visual Studio .NET and create a new Visual Basic .NET application with a form.

  2. Place a TextBox control named txtTest , a Button control named btnIterate , and a ListBox control named lbIterate on the form.

  3. Add the references at the top of the form's code module:

     Imports System.Globalization 
  4. Add code to the Click event of the form's button:

     Private Sub btnIterate_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles btnIterate.Click     lbIterate.Items.Clear()     ' Get an iterator for the entered text     Dim iter As TextElementEnumerator = _      StringInfo.GetTextElementEnumerator(txtTest.Text)     ' The iterator starts before the string, have to move     ' it forward once to reach the first element     iter.MoveNext()     Do         lbIterate.Items.Add("Element " & iter.ElementIndex & _          ": " & iter.Current)     Loop While (iter.MoveNext) End Sub 
  5. Set the form as the startup object for the project and then run the project. Paste or enter text in the text box and click the Button control to display the string one character at a time, as shown in Figure 8.8.

    Figure 8.8. An example of indexed string output.

    graphics/08fig08.jpg

Comparing and Sorting Data

Cultures sort alphabetical lists in different ways, such as placing numeric characters either before or after alphabetic characters and ordering a character followed by a space as falling either before or after the same character followed by another character. In order to support culture-aware sorting, the .NET Framework includes several features:

  • The String.Compare method Used to compare strings according to the rules of CultureInfo , referenced by the CurrentCulture property.

  • The CultureInfo.CompareInfo object Supports substring searching according to the rules referenced by the CurrentCulture property.

  • The Array.Sort method Used to order the members of an array by the alphabetization rules referenced by the CurrentCulture property.

  • The SortKey.Compare method Another method used to order the members of an array by the alphabetization rules referenced by the CurrentCulture property.



Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 188

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net