更改文本框的颜色并限制输入 [英] Change colour of textbox and restrict input

查看:79
本文介绍了更改文本框的颜色并限制输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更改任何输入字母的文本框的颜色,并且只允许数字值....

[edit]将标题从帮助我"更改为更具描述性的[/edit]

I want to change the color of my text box of any one enters the alphabet and want to allow only numeric value....

[edit]Changed title from "Help me" to more descriptive[/edit]

推荐答案

您需要验证输入的 onKeyPress .

根据需要处理事件,更改颜色并限制用户.
样本:
You need to validate what is entered onKeyPress.

Handle the event, change the color and restrict user as per you want.
Sample:
private void txtBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    // Validates integers n float
    if ((e.KeyChar > 47 && e.KeyChar < 58) || (e.KeyChar == 46) || (e.KeyChar == 8))
    {
      //Your logic here if needed
    }
    else
    {
      // Change the Color or show message 
      // MessageBox.Show("Please enter valid integer.");
    }
}


请不要发布带有帮助我"或需要帮助"之类的标题的问题-请尽量描述性,因为这会鼓励了解您问题的人回答,并且不会浪费那些没有的人的时间!
我更改了您的姓名以显示您的意思.
Please do not post questions with titles like "help me" or "help needed" - try to be descriptive as it both encourages those who know about your problem to answer, and doesn''t waste the time of those who don''t!
I changed yours to show you what I mean.


这篇关于更改文本框的颜色并限制输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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