⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ipbox.cs

📁 快速切换网关
💻 CS
字号:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace IpBox
{
    public partial class IpBox : UserControl
    {
        private System.Windows.Forms.Panel panel1; 
        private System.Windows.Forms.TextBox textBox4; 
        private System.Windows.Forms.Label label3; 
        private System.Windows.Forms.TextBox textBox3; 
        private System.Windows.Forms.Label label2; 
        private System.Windows.Forms.TextBox textBox2; 
        private System.Windows.Forms.Label label1; 
        private System.Windows.Forms.TextBox textBox1; 
        private string _text = "";  

        public IpBox() 
        { 
            // 该调用是 Windows.Forms 窗体设计器所必需的。 
            InitializeComponent(); 

            // TODO: 在 InitializeComponent 调用后添加任何初始化 

        } 
                 

        private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e) 
        { 
            //System.Text.Encoding.GetEncoding(0).GetString(); 
        } 

        private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
        {
            textBox1.SelectedText = "";
            MaskIpAddr(textBox1, e); 
        } 

        private void textBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
        {
            textBox2.SelectedText = "";
            MaskIpAddr(textBox2, e); 
        } 

        private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
        {
            textBox3.SelectedText = "";
            MaskIpAddr(textBox3, e); 
        } 

        private void textBox4_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) 
        {
            textBox4.SelectedText = "";
            MaskIpAddr(textBox4, e); 
        } 

        private void MaskIpAddr(System.Windows.Forms.TextBox textBox, KeyPressEventArgs e) 
        { 
            int len = textBox.Text.Length; 

            if (Char.IsDigit(e.KeyChar) || e.KeyChar == '.' || e.KeyChar == 8) 
            { 
                if (e.KeyChar != 8) 
                { 
                    if (len == 2 && e.KeyChar != '.') 
                    { 
                        string tmp = textBox.Text + e.KeyChar; 
                        if (textBox.Name == "textBox1") 
                        { 
                            if (Int32.Parse(tmp) > 255) // 进行验证 
                            { 
                                MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。"); 
                                textBox.Text = "255"; 
                                textBox.Focus(); 
                                return; 
                            } 

                            textBox2.Focus();                             

                        } 
                        else if (textBox.Name == "textBox2") 
                        { 
                            if (Int32.Parse(tmp) > 255) 
                            { 
                                MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。"); 
                                textBox.Text = "255"; 
                                textBox.Focus(); 
                                return; 
                            } 
                            textBox3.Focus();
                            
                        } 
                        else if (textBox.Name == "textBox3") 
                        { 
                            if (Int32.Parse(tmp) > 255) 
                            { 
                                MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。"); 
                                textBox.Text = "255"; 
                                textBox.Focus(); 
                                return; 
                            } 
                            textBox4.Focus(); 
                            
                        } 
                        else if (textBox.Name == "textBox4") 
                        { 
                            if (Int32.Parse(tmp) > 255) 
                            { 
                                MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。"); 
                                textBox.Text = "255"; 
                                textBox.Focus(); 
                                return; 
                            } 

                        } 

                    } 
                    if (e.KeyChar == '.') 
                    { 
                        if (textBox.Name == "textBox1" && textBox.Text != "") 
                        { 
                            textBox2.Focus(); 
                            
                        } 
                        if (textBox.Name == "textBox2" && textBox.Text != "") 
                        { 
                            textBox3.Focus(); 
                            
                        } 
                        if (textBox.Name == "textBox3" && textBox.Text != "") 
                        { 
                            textBox4.Focus(); 
                            
                        } 
                        if (textBox.Name == "textBox4" && textBox.Text != "") 
                        { 

                        } 
                        e.Handled = true; 
                    } 
                } 
                else 
                { 
                    if (textBox.Name == "textBox1" && textBox.Text == "") 
                    { 

                    } 
                    if (textBox.Name == "textBox2" && textBox.Text == "") 
                    { 
                        textBox1.Focus(); 
                        textBox1.SelectionStart = textBox1.Text.Length; 
                    } 
                    if (textBox.Name == "textBox3" && textBox.Text == "") 
                    { 
                        textBox2.Focus(); 
                        textBox2.SelectionStart = textBox2.Text.Length; 
                    } 
                    if (textBox.Name == "textBox4" && textBox.Text == "") 
                    { 
                        textBox3.Focus(); 
                        textBox3.SelectionStart = textBox3.Text.Length; 
                    } 
                    e.Handled = false; 
                } 
            } 
            else 
                e.Handled = true; 
        } 


        [Browsable(true)] 
         
        // 获取 IpBox 的文本。 
         
        public new string Text 
        { 
            get 
            { 
                if (textBox1.Text == "" 
                 || textBox2.Text == "" 
                 || textBox3.Text == "" 
                 || textBox4.Text == "") 
                { 
                    _text = ""; 
                    return _text; 
                } 
                else 
                { 
                    _text = Convert.ToInt32(textBox1.Text).ToString() + "." + Convert.ToInt32(textBox2.Text).ToString() + "." + Convert.ToInt32(textBox3.Text).ToString() + "." + Convert.ToInt32(textBox4.Text).ToString(); 
                    return _text; 
                } 

            } 
             set
             {
                 _text = value; 
                 try
                 {
                     string[] ipnum = value.Split('.');
                     textBox1.Text = ipnum[0];
                     textBox2.Text = ipnum[1];
                     textBox3.Text = ipnum[2];
                     textBox4.Text = ipnum[3];
                 }
                 catch
                 {
                     _text = "";
                     textBox1.Text = "";
                     textBox2.Text = "";
                     textBox3.Text = "";
                     textBox4.Text = "";
                 }                

             }

        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            textBox1.SelectAll();
        }  

        private void textBox1_Enter(object sender, EventArgs e)
        {
            textBox1.SelectAll();
        }

        private void textBox2_Enter(object sender, EventArgs e)
        {
            textBox2.SelectAll();
        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            textBox2.SelectAll();
        }

        private void textBox3_Enter(object sender, EventArgs e)
        {
            textBox3.SelectAll();
        }

        private void textBox3_Click(object sender, EventArgs e)
        {
            textBox3.SelectAll();
        }

        private void textBox4_Enter(object sender, EventArgs e)
        {
            textBox4.SelectAll();
        }

        private void textBox4_Click(object sender, EventArgs e)
        {
            textBox4.SelectAll();
        }

        

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -