开发者

TextBox that limits the number of allowed characters in the string (and ensures they are all capital letters)

开发者 https://www.devze.com 2023-02-23 04:09 出处:网络
I want to get a set of some number of letters (default of 7) from the user on a WP7 app. I\'m trying to use a TextBox, though I am not married to that idea. My current plan is to use the technique des

I want to get a set of some number of letters (default of 7) from the user on a WP7 app. I'm trying to use a TextBox, though I am not married to that idea. My current plan is to use the technique desribed at Making TextBox Numbers Only for Windows Phone 7 to restrict the characters to letters (and convert them to capitals) on KeyUp event. Near as I can tell, there's not another good way to do that.

I'm doing the same thing for limiting the number of characters--if the length of the text in the TextBox is > 7, I remove the character.

I've already come accross on bug in my implementation (how do I handle backspace, particularly of the first characte开发者_JAVA百科r in the TextBox), which makes me assume there are lots of problems with this implementation.

Is there a better way to limit the length of text in the TextBox?


Use the MaxLength property on the TextBox to limit the text to a specific length. You can bind it to a property on your view model if you need to change it.

Use key up event handler to change the case.


I realize this is a rather old posting. Maxlength was not working for me on a Windows Phone 8.1 project, so I created an event handler below:

private void txtBox_TextChanged(object sender, TextChangedEventArgs e)
{
    if (txtBox.Text.Length > 7)
    {
        txtBox.Text = txtBox.Text.Substring(0, 7);
        // the cursor is now at the beginning of the box
        // set it back to the end so there is no overwriting
        txtBox.SelectionStart = txtBox.Text.Length;
        txtBox.SelectionLength = 0;
    }
}
0

精彩评论

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