tags:

views:

20

answers:

1

Using WPF(with C# and LinqToXml), I am reproducing a paperless version of an existing paper form. The existing form includes some one-character-per-box text strings. I have already duplicated the appearance of the paper form using XAML. How can I add code to a one-character TextBox to automatically send control to the next TextBox once it has been filled(without requiring the user to tab to the next TextBox)? Also, these TextBox sequences facilitate the input of key field values. Once the last one-character TextBox receives a value from the keyboard, how could I code an event trigger to automatically retrieve the appropriate data record field values from the Xml data file? Will I need to include a button on the form, or can I code the form so that the retrieval automatically occurs when the last one-character TextBox has been filled?

A: 

The neatest way to achieve what you want to do is probably to create an class representing the code that exposes the digits via properties that are bound to the textboxes (or as a string via one property and then use a ValueConverter to update the appropriate digits) and implements the INotifyPropertyChanged interface (throwing a PropertyChanged event each time the property/properties are set. You can then either create a handler that listens for PropertyChanged events from the code object, checks that all of the digits are filled in and if so loads the data from XML, or alternatively you could do that checking inside the object and raise some other event to let the application know a full code is entered.

As for how to move to the next textbox, you could create a TextChanged event handler that calls the UIElement.MoveFocus() method and register it with all of the textboxes, like so:

    private void textChanged(object sender, TextChangedEventArgs e)
    {
        TextBox textBox = sender as TextBox;

        if (textBox != null && textBox.Text.Length == 1)
        {
            TraversalRequest tr = new TraversalRequest(FocusNavigationDirection.Next);
            textBox.MoveFocus(tr);
        }
    }

You may also want to set the MaxLength of each textbox to 1 to prevent copy and pasting of text, etc. and you could also look into things like ValidationRules for checking for illegal characters, etc.

Hope this helps (just ask if you need help with any of that).

Regards, James

Moonshield