当前位置:编程学习 > C#/ASP.NET >>

[C#]一个关于TextBox的问题

小弟近日想做一个支持ip输入的TextBox控件 
实现细节省略,见附件 
这里有一个问题:当输入文本框不支持的字符(数字以外的字符和使ip大于255的数字)时,它就能接受‘.’(句号)字符的输入,如果你输入了几个文本框不支持的字符,它就能接受几个句号的输入。可是我的句号输入是让文本框切入下一个文本框用的。 
 
例如:在一段文本框内的内容为‘55’时,你再输入‘5’(这是不接受的),然后输入句号,它是能接受的(这是错误的),你摁两次‘5’,它就能接受两个句号,三次则三个句号。。。 
 
我自己调试了一下程序,发现几个问题。就是有错误出现时,句号的输入不会进入keypress函数中!!!而且我将文本框切入下一个文本框换成‘m’时,它是不会出现这个错误的,可见句号输入对于TextBox这个控件有一定的特殊性。 
 
求哪位大牛帮忙分析一下这个问题。下面是主要代码 
 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Data; 
using System.Text; 
using System.Windows.Forms; 
 
namespace IPTextBox 

    public partial class IPTextBox : UserControl 
    { 
        public IPTextBox() 
        { 
            InitializeComponent(); 
        } 
 
        private TextBox[] Textboxes = new TextBox[4]; 
 
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
        { 
            // 只接受数字,退格和点输入 
            if ((int)e.KeyChar == 46) 
            { 
                e.Handled = true; 
                NextFocus((TextBox)sender); 
                return; 
            } 
            if (e.KeyChar >= '0' && e.KeyChar <= '9') 
            { 
                String curText = ((TextBox)sender).Text; 
                if (curText != "") 
                { 
                    if (int.Parse(curText) * 10 < 255) 
                    { 
                        e.Handled = false; 
                        return; 
                    } 
                } 
                else 
                { 
                    e.Handled = false; 
                    return; 
                } 
            } 
 
            if ((int)e.KeyChar == 8) 
            { 
                e.Handled = false; 
                return; 
            } 
 
            e.Handled = true; 
        } 
 
        private void NextFocus(TextBox sender) 
        { 
            for(int i = 0; i < 4; i++) 
            { 
                if(ReferenceEquals(sender, Textboxes[i])) 
                { 
                    if(i != 3) 
                        Textboxes[i + 1].Focus(); 
                } 
            } 
             
        } 
 
        public override String Text 
        { 
            get 
            { 
                return textBox1.Text + '.' + textBox2.Text + '.' + textBox3.Text + '.' + textBox4.Text; 
            } 
        } 
 
        private void UserControl1_Load(object sender, EventArgs e) 
        { 
            Textboxes[0] = textBox1; 
            Textboxes[1] = textBox2; 
            Textboxes[2] = textBox3; 
            Textboxes[3] = textBox4; 
            Rectangle rect = this.ClientRectangle; 
            if(rect.Width - 33 > 0) 
            { 
                textBox1.Size = new Size((rect.Width - 33) / 4, rect.Height); 
            } 
        } 
 
        private void IPTextBox_SizeChanged(object sender, EventArgs e) 
        { 
            Rectangle rect = ClientRectangle; 
            if(Size.Width - 33 > 0) 
            { 
                textBox1.Size = new Size((rect.Width - 33) / 4, rect.Height); 
            } 
            else 
            { 
                this.Size = new Size(200, 30); 
            } 
        } 
 
        private void textBox1_SizeChanged(object sender, EventArgs e) 
        { 
            Rectangle rect = this.ClientRectangle; 
            int Incr = (rect.Width - 33) / 4; 
            textBox1.Location = new Point(0, 0); 
            textBox2.Size = textBox1.Size; 
            textBox2.Location = new Point(textBox1.Location.X + Incr + 11, textBox1.Location.Y); 
            textBox3.Size = textBox1.Size; 
            textBox3.Location = new Point(textBox2.Location.X + Incr + 11, textBox2.Location.Y); 
            textBox4.Size = textBox1.Size; 
            textBox4.Location = new Point(textBox3.Location.X + Incr + 11, textBox3.Location.Y); 
            label1.Location = new Point(textBox1.Location.X + Incr, textBox1.Location.Y + rect.Height - 12); 
            label2.Location = new Point(textBox2.Location.X + Incr, textBox1.Location.Y + rect.Height - 12); 
            label3.Location = new Point(textBox3.Location.X + Incr, textBox1.Location.Y + rect.Height - 12); 
        } 
    } 

--------------------编程问答-------------------- 没仔细看代码
不知道你是不是要实现这样的功能
[].[].[].[] ([]为textbox)
然后输入的问题
你在第一个[]输入的时候输入一个字符可以判断输入的字符是否是数字而且整个[]是否小于256
是的话接受输入
否则的话 假如输入的不是数字的话 不接受
输入的数字让整个[]大于255的话 把这个数字跳到第二个[] --------------------编程问答-------------------- 建议初学者,把问题先精简,再提问,没时间看啊 --------------------编程问答-------------------- 我试过你的代码了,没有问题,当在textbox1中输入句号时,光标可以跳转到第二个文本框,达到了你的初衷。
可见你的代码没有问题,是不是在使用这个控件的过程中,参数设置哪儿出了问题?你再查查吧,我建议你换一台开发机器,然后重新将这个控件写一遍,做一个C/S测试环境,嵌入你新写的控件,这样也许就能执行成功。 --------------------编程问答-------------------- 不好意思 代码可能比较繁琐 但是为了大家方便测试 就将它全部贴出来了
如果可以,我希望将源代码全部贴出来

主要问题就是:基本功能都正确,但是输入不能接受的字符后,就能接受句号输入,这个问题很奇怪,我百思不得其解 --------------------编程问答-------------------- 功能是[].[].[].[]
2楼说的前一部分是对的,不过跳到下一个[]只支持句号的输入
输入的数字让[]大于255的话,只是简单的不接受

问题在于输入的数字让[]大于255的话,下一次输入句号就能接受了,本来句号只是用来让光标跳到下一个[]的,而且输入几个这种不接受的数字,接下来就能接受几个句号的输入,然后句号的输入才能实现光标跳转的目的。 --------------------编程问答-------------------- 没有太搞懂事什么意思

正则表达式可以判断不? --------------------编程问答-------------------- 正则或许能完成这个功能
这个问题我也不是很懂
描述的不好你可以测试一下代码

输入句号是可以光标可以跳转到下一个文本框,当你在一个文本框输入了数字‘55’,在输入‘5’,然后再输入‘.’,句号就会显示出来,不会跳到下一个文本框,然后输入‘.’才能跳到下一个文本框 --------------------编程问答-------------------- 操作系统原因?
我的操作系统server 2003
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,