masktextbox IP控件

2020-02-21 10:50:18 浏览数 (1)

ip控件

添加事件

代码语言:javascript复制
            maskedTextBox1.KeyPress  = MaskedTextBox_KeyPress;
            maskedTextBox2.KeyPress  = MaskedTextBox_KeyPress;
            maskedTextBox3.KeyPress  = MaskedTextBox_KeyPress;
             private void MaskedTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            MaskedTextBox texBox = sender as MaskedTextBox;
            int ret = SetValidData(texBox, texBox.SelectionStart, e.KeyChar);
            texBox.SelectionStart = ret;
        }

设置光标位置函数

代码语言:javascript复制
private int SetValidData(MaskedTextBox textBox,int start,char input)
        {
            try
            {
                string text = textBox.Text.PadRight(15),ipText = text;
                if (input >= '0' && input <= '9')
                {
                    //ipText = ipText.Remove(start - 1);
                    ipText = ipText.Insert(start, input.ToString());
                }
                int index 

0 人点赞