LightSwitch – Automatically Upper Case TextBox Text

Here’s a quick post in reference to question on how to automatically upper case the characters whilst typing into a TextBox.

LightSwitch includes a changed event but the problem with using this is that it only fires after leaving the TextBox. You therefore don’t see the characters being upper cased as you type into the control.

One possible workaround is to handle the KeyUp event of the TextBox control. In keeping with the standard practice of handling Silverlight control events, the ControlAvailable event is used to attach the KeyUp event of the Textbox.

Assuming that you have a TextBox named ‘Surname’, here is some sample code that automatically upper cases the characters as you type into the control. The ‘SelectionStart’ property is used to set the cursor location to the end of the TextBox, otherwise you end up with some strange behaviour as subsequent text will be inserted into the start of the TextBox.


Private Sub MyScreen_Activated()
    AddHandler Me.FindControl("Surname").ControlAvailable, AddressOf TextBoxAvailable
End Sub

Private Sub TextBoxAvailable(sender As Object, e As ControlAvailableEventArgs)
    AddHandler CType(e.Control, System.Windows.Controls.TextBox).KeyUp, AddressOf TextBoxKeyUp
End Sub

Private Sub TextBoxKeyUp(sender As Object, e As System.Windows.RoutedEventArgs)
    Dim strTextUpper As String = CType(sender, System.Windows.Controls.TextBox).Text.ToUpper
    CType(sender, System.Windows.Controls.TextBox).Text = strTextUpper
    CType(sender, System.Windows.Controls.TextBox).SelectionStart = strTextUpper.Length
End Sub

Here’s the C# version.


private void CreateNew_Activated()
    this.FindControl("Property1").ControlAvailable += TextBoxAvailable;

private void TextBoxAvailable(object sender, ControlAvailableEventArgs e)
    ((System.Windows.Controls.TextBox)e.Control).KeyUp += TextBoxKeyUp;

private void TextBoxKeyUp(object sender, System.Windows.RoutedEventArgs e)
    string strTextUpper = ((System.Windows.Controls.TextBox)sender).Text.ToUpper;
    ((System.Windows.Controls.TextBox)sender).Text = strTextUpper;
    ((System.Windows.Controls.TextBox)sender).SelectionStart = strTextUpper.Length;

About dotnettim

Tim Leung is a Microsoft .Net / SQL Server developer based in England.
This entry was posted in LightSwitch. Bookmark the permalink.

2 Responses to LightSwitch – Automatically Upper Case TextBox Text

  1. This works OK providing the user is typing at the end of the string.
    If they use the cursor keys or mouse to position the insertion point in the middle of the string then typing a character would reposition the insertion point to the end causing their next character to be entered at the end.
    To avoid this, you shoud remember where SelectionStart is before you replace the text and set it back to that value rather than to the length of the string.

  2. Otis Ranger says:

    I just want to thank you for helping me solve a completely different problem ( with your calls to the textboxavailiable event. It has enabled me to rest easy & once I create a walkthrough to prove the concept I will give you kudos points in helping me 🙂
    Many, many thanks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s