仅允许数字和字符"p"的文本框 [英] textbox which allow only numbers and character 'p'

查看:90
本文介绍了仅允许数字和字符"p"的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我在Windows应用程序中需要一个仅接受数字且仅包含一个字符"p"的文本框,并且字符"p"应仅出现一次.
我该怎么办?
粘贴到文本框中时,不允许输入其他字符.

谢谢
Jiju

hello,
i need a textbox in my windows application which accept only numbers and only one character ''p'', and the character ''p'' should occur only one time.
How can i do this?
when paste into the textbox, it should not allow to enter other characters.

Thanks
Jiju

推荐答案

一种方法是使用
One way is to use the Validating[^] event.


您可以使用KeyPress事件,并检查正确的字符,然后单击args.Handled = true 来过滤字符:请参见 http://www.programcall.com/36 /csnet/example-for--winforms-keypress-event-in-dotnet.aspx [
You can use the KeyPress event, and check for the correct characters, then args.Handled = true to filter the character: see http://www.programcall.com/36/csnet/example-for--winforms-keypress-event-in-dotnet.aspx[^]


private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) || e.KeyChar == 'p')
            {

            }
            else
            {
                e.Handled = true;
            }
        }


这篇关于仅允许数字和字符"p"的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆