开发者

How to get a textbox to only display Uppercase letters?

开发者 https://www.devze.com 2022-12-26 12:57 出处:网络
Using Windows Mobile 6.5 and C# The CharacterCasing property seems to be missing from WinMo 6.5 and I decide to just catch the textchanged event and set the text with ToU开发者_开发问答pper.

Using Windows Mobile 6.5 and C#

The CharacterCasing property seems to be missing from WinMo 6.5 and I decide to just catch the textchanged event and set the text with ToU开发者_开发问答pper.

This works - but on every keypress, it sends the cursor back to the beginning of the string, not the end.


I know this is old, but hopefully this can help somebody else. I implemented the KeyPress event like the following.

    private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.KeyChar = Char.ToUpper(e.KeyChar);
    }


Ritu, just to comment on your answer. You should keep in mind that this might be confusing for a user if the user has positioned the caret in the middle of the string to perform some edit and then the caret jumps to the end of the string on the key press.

An alternative might be to change the text to upper case when the edit control loses focus.


Try here such implementation.

 public MainForm()
    {
        InitializeComponent();
        InitializeUpperCaseTextBox();

    }
    private void InitializeUpperCaseTextBox()
    {
        txtbox.CharacterCasing = CharacterCasing.Upper;
        //... etc.
    }


The soluttion of setting the text position to the end of the string seems like would be a hassle if you ever need to edit text that you have already entered.

It's been a while since I thought about the C# event model but, one alternative might be to catch the KeyPress event and change any lowercase KeyChar values to uppercase before passing them on to the next handler.


The way you are approaching seems to be wrong.
There are so many different ways to insert data into that textbox. What about copy & paste for instance?

Just perform a .Text.ToUpper() when accessing the value of the Textbox


Save the SelectionStart and SelectionLength before changing the text. The ToUpper should make no changes to the length, so you can simply set the SelctionStart and SelectionLength back to what they had been.

Also, I would expect to get a changed event again when you set it ToUpper. I'm not sure if you also need to check that the ToUpper actually changed anything before you set Text again. It may be smart enough to check that for you when you assign the text and avoiding giving you an infinite recursive loop of change events. However, you probably don't want to alter the selection in the event handler call for the case where you aren't making a further change, only in the outer call where you are assigning back to Text. So you might as well guard recursion directly.

Something like:

bool m_InMyTextChanged = false;  

private void txtMyText_TextChanged(object sender, EventArgs e)
{
    if (m_InMyTextChanged)
        return; // Recursive!  We can bail quickly.  

    m_InMyTextChanged = true; // Prevent recursion when we change it.
    int selectionStart = txtMyText.SelectionStart;
    int selectionLength = txtMyText.SelectionLength;
    string originalText = txtMyText.Text;
    string newText = originalText.ToUpper();
    if (newText != originalText)
    {
        txtMyText.Text = newText; // Will cause a new TextChanged event.
        // Set the selection back *after* the assignment, which has reset them.
        txtMyText.SelectionStart = selectionStart;
        txtMyText.SelectionLength = selectionLength;
    }
    m_InMyTextChanged = false; // Allow it for next time.
}

could work. I haven't worked in Windows Mobile, but I would think this would work the same as in general for .NET.


I figured it out. So on the textChanged event, I replace the entered text with the ToUpper version. Then I set the SelectionStart property to the Text.Length to move the cursor to the end.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号