2

I am writing a Silverlight 4 business application and have run into an issue. I need the text input in TextBoxes to be forced to UpperCase. What I understand from various forums is Silverlight does not Implement CharacterCasing and CSS Styling.

Is there any other way to achieve this?

VRamesh
  • 25
  • 1
  • 6

3 Answers3

10

You can achieve this by creating a behavior, like this:

public class UpperCaseAction : TriggerAction<TextBox>
{

    protected override void Invoke(object parameter)
    {
        var selectionStart = AssociatedObject.SelectionStart;
        var selectionLenght = AssociatedObject.SelectionLength;
        AssociatedObject.Text = AssociatedObject.Text.ToUpper();
        AssociatedObject.SelectionStart = selectionStart;
        AssociatedObject.SelectionLength = selectionLenght;
    }
}

Then, use it in your TextBox, like this:

<Grid x:Name="LayoutRoot" Background="White">
    <TextBox TextWrapping="Wrap" VerticalAlignment="Top" Margin="10">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="TextChanged">
                <ASD_Answer009_Behaviors:UpperCaseAction/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TextBox>
</Grid>

Where i: is a namespace for

clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity

Code behind:

System.Windows.Interactivity.EventTrigger eventTrigger = new System.Windows.Interactivity.EventTrigger("TextChanged");
eventTrigger.Actions.Add(new UpperCaseAction());   
System.Windows.Interactivity.Interaction.GetTriggers(myTextBox).Add(eventTrigger);

In order to create and use behaviors, you need to download and install the Expression Blend SDK for Silverlight 4 and add a reference to System.Windows.Interactivity.dll.

Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
Murven
  • 2,377
  • 17
  • 23
1

Try this:

private void txt2_KeyDown(object sender, KeyEventArgs e)
{
    e.Handled = MakeUpperCase((TextBox)sender, e);
}

bool MakeUpperCase(TextBox txt, KeyEventArgs e)
{
    if (Keyboard.Modifiers != ModifierKeys.None || (e.Key < Key.A) || (e.Key > Key.Z))  //do not handle ModifierKeys (work for shift key)
    {
        return false;
    }
    else
    {
        string n = new string(new char[] { (char)e.PlatformKeyCode });
        int nSelStart = txt.SelectionStart;

        txt.Text = txt.Text.Remove(nSelStart, txt.SelectionLength); //remove character from the start to end selection
        txt.Text = txt.Text.Insert(nSelStart, n); //insert value n
        txt.Select(nSelStart + 1, 0); //for cursortext

        return true; //stop to write in txt2
    }

}
sth
  • 222,467
  • 53
  • 283
  • 367
lelah
  • 11
  • 1
0
    private void txt2_KeyDown(object sender, KeyEventArgs e)
    {

        if (Keyboard.Modifiers != ModifierKeys.None) return; //do not handle ModifierKeys (work for shift key)

        string n = new string(new char[] { (char)e.PlatformKeyCode });
        int nSelStart = txt2.SelectionStart;

        txt2.Text = txt2.Text.Remove(nSelStart, txt2.SelectionLength); //remove character from the start to end selection
        txt2.Text = txt2.Text.Insert(nSelStart, n); //insert value n
        txt2.Select(nSelStart + 1, 0); //for cursortext

        e.Handled = true; //stop to write in txt2

    }
animuson
  • 53,861
  • 28
  • 137
  • 147
lelah
  • 11
  • 1