locked
C# .NET CF Forcing uppercase in textbox. RRS feed

  • Question

  • I am trying to force uppercase input into a textbox on windows mobile using C# .NET. MSDN reference articles suggest the following might be possible:

     

     

    Code Snippet

    private void TextControl_KeyPress(object sender, KeyPressEventArgs e)

    {

    e.KeyChar = Char.ToUpper(e.KeyChar);

    }

     

     

    However I get an compile error that says KeyChar is read only. The only other solution I could find was to use CharacterCasing, however that is not supported in the framework I am using for the windows mobile. Does anyone know a working solution for .NET Compact Framework 2.0?
    Thursday, March 27, 2008 6:16 PM

Answers

  •  

    That did the trick. I found something that's a variation of what you posted, only it does it onkeypress instead of textchanged. The resulting behavior is slightly cleaner, but still clunky. For anyone googling this thread:

     

     

    Code Snippet

     

    public class UCTextBox : TextBox

    {

    public UCTextBox()

    {

    }

     

    protected override void OnKeyPress(KeyPressEventArgs e)

    {

    if (Char.IsLetter(e.KeyChar))

    {

    // save the current caret position

    int pos = this.SelectionStart;

    // insert the upper case character

     

    this.Text = this.Text.Insert(this.SelectionStart,

    Char.ToUpper(e.KeyChar).ToString());

     

    // and update the current caret position

    this.SelectionStart = pos + 1;

    e.Handled = true;

    }

     

    base.OnKeyPress(e);

    }

     

    }

     

     

     

    Tuesday, April 1, 2008 11:54 AM
  • Try this:

     

    Code Snippet

    private bool isFirstChange = true;

    private void textBox1_TextChanged(object sender, EventArgs e)

    {

     if (isFirstChange)

     {

      isFirstChange = false;

      this.textBox1.Text = this.textBox1.Text.ToUpper();

      this.textBox1.SelectionStart = this.textBox1.Text.Length;

      isFirstChange = true;

     }

    }

     

     

    Thursday, March 27, 2008 7:18 PM

All replies

  • Yjou can inspect e.KeyChar to see if it is uppercase and accept the entry if it is and reject it if it is not

     

     

    Thursday, March 27, 2008 7:17 PM
  • Try this:

     

    Code Snippet

    private bool isFirstChange = true;

    private void textBox1_TextChanged(object sender, EventArgs e)

    {

     if (isFirstChange)

     {

      isFirstChange = false;

      this.textBox1.Text = this.textBox1.Text.ToUpper();

      this.textBox1.SelectionStart = this.textBox1.Text.Length;

      isFirstChange = true;

     }

    }

     

     

    Thursday, March 27, 2008 7:18 PM
  • The TextBox2 class available in the free OpenNETCF SDF has a CharacterCasing property.

     

    Greetings.

    Friday, March 28, 2008 8:28 AM
  •  

    That did the trick. I found something that's a variation of what you posted, only it does it onkeypress instead of textchanged. The resulting behavior is slightly cleaner, but still clunky. For anyone googling this thread:

     

     

    Code Snippet

     

    public class UCTextBox : TextBox

    {

    public UCTextBox()

    {

    }

     

    protected override void OnKeyPress(KeyPressEventArgs e)

    {

    if (Char.IsLetter(e.KeyChar))

    {

    // save the current caret position

    int pos = this.SelectionStart;

    // insert the upper case character

     

    this.Text = this.Text.Insert(this.SelectionStart,

    Char.ToUpper(e.KeyChar).ToString());

     

    // and update the current caret position

    this.SelectionStart = pos + 1;

    e.Handled = true;

    }

     

    base.OnKeyPress(e);

    }

     

    }

     

     

     

    Tuesday, April 1, 2008 11:54 AM
  • You just need to change CharacterChasing property to Upper.

            textBox1.CharacterCasing = CharacterCasing.Upper
    • Proposed as answer by Big Jiant Thursday, March 12, 2015 1:35 PM
    Monday, January 13, 2014 1:24 PM
  • Why is your answer at the bottom of this post?!?  That is soooo much easier than writing a method and having each textbox call it!
    Friday, June 27, 2014 8:32 PM
  • Because that property is not supported in .Net Compact Framework. Unfortunately, that answer doesn't apply.
    Thursday, April 11, 2019 1:26 PM