ITPUB??ì3
ITPUB论坛 » Web开发 » ASP.NET与AJAX » 限制TextBox输入暂定内容

新一届的微软MVP评选已经开始,欢迎各位推荐!

标题: 限制TextBox输入暂定内容
离线 dotnetworker
一般会员



精华贴数 0
个人空间 0
技术积分 4698 (291)
社区积分 0 (1442376)
注册日期 2007-6-21
论坛徽章:33
开发板块每日发贴之星开发板块每日发贴之星开发板块每日发贴之星生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠
生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠

发表于 2008-7-3 19:30 
限制TextBox输入暂定内容

1、如何限制TextBox只能输入0-255之间的数字,要求向Windows一样一输入错误马上就能给出提示
2、如何判断在TextBox中输入的子网掩码格式是否正确(即转换为二进制后,前几位全为1,后几位全为0)


只看该作者    顶部
离线 juanpeng
中级会员



精华贴数 0
个人空间 0
技术积分 4666 (294)
社区积分 0 (1441387)
注册日期 2007-6-21
论坛徽章:27
开发板块每日发贴之星开发板块每日发贴之星开发板块每日发贴之星生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠
生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠

发表于 2008-7-3 19:32 
TextBox的TextChange事件里
判断
int txt;
try
{
txt = Convert.ToInt(TextBox.Text);
}
catch
{
//输入的不是数字
}

if(txt < 0 ¦ ¦ txt >255)
//只能输入0-255之间的数字


只看该作者    顶部
离线 smartpig
老会员



精华贴数 1
个人空间 160
技术积分 6333 (210)
社区积分 0 (1065439)
注册日期 2006-8-9
论坛徽章:33
会员2007贡献徽章开发板块每日发贴之星开发板块每日发贴之星开发板块每日发贴之星生肖徽章2007版:鼠生肖徽章2007版:鼠
生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠

发表于 2008-7-3 19:32 
用验证控件


只看该作者    顶部
离线 juan002
中级会员



精华贴数 0
个人空间 0
技术积分 4216 (334)
社区积分 0 (1441388)
注册日期 2007-6-21
论坛徽章:19
开发板块每日发贴之星生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠
生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠生肖徽章2007版:鼠

发表于 2008-7-3 19:33 
做一个判断函数,用来检查是不是符合要求的

看看MSDN上的类似代码吧:

下面的代码示例使用 KeyPress 事件来禁止向控件输入字符。
// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;

// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    // Initialize the flag to false.
    nonNumberEntered = false;

    // Determine whether the keystroke is a number from the top of the keyboard.
    if (e.KeyCode < Keys.D0 ¦ ¦ e.KeyCode > Keys.D9)
    {
        // Determine whether the keystroke is a number from the keypad.
        if (e.KeyCode < Keys.NumPad0 ¦ ¦ e.KeyCode > Keys.NumPad9)
        {
            // Determine whether the keystroke is a backspace.
            if(e.KeyCode != Keys.Back)
            {
                // A non-numerical keystroke was pressed.
                // Set the flag to true and evaluate in KeyPress event.
                nonNumberEntered = true;
            }
        }
    }
}

// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered == true)
    {
        // Stop the character from being entered into the control since it is non-numerical.
        e.Handled = true;
    }
}


只看该作者    顶部
离线 382564549
danil


来自 蘇州
精华贴数 0
个人空间 0
技术积分 308 (6404)
社区积分 16 (8588)
注册日期 2008-7-17
论坛徽章:0
      
      

发表于 2008-7-18 09:58 
用ajax验证控件


__________________
既來之,則求問之~
只看该作者    顶部
离线 X二
支原体


精华贴数 3
个人空间 0
技术积分 293 (6702)
社区积分 378 (1655)
注册日期 2008-7-18
论坛徽章:3
月度精华徽章2008北京奥运纪念徽章:跳水2008北京奥运纪念徽章:现代五项   
      

发表于 2008-7-18 22:14 
1、RichTextBox可以设置正则表达式
2、有验证控件可以做出错误提示


只看该作者    顶部
 
    

相关内容


CopyRight 1999-2006 itpub.net All Right Reserved.
北京皓辰广域网络信息技术有限公司. 版权所有
E-mail:Webmaster@itpub.net
京ICP证:010037号 联系我们 法律顾问