用写C#写的一个IP输入框控件

WinForm中使用MaskedTextBox制作IP地址输入框 1.实现的功能:输入IP地址,形如000.000.000.000的格式,并设置keydown事件,当输入点(.)的时候,自动跳至下一栏。从工具箱中拖入一个MaskedTextBox,命名为MaskedTextBox_IP。3.在PromptChar属性中,将_换为空格,如果你喜欢_的话,也可以不用换。2.在Mask属性中,输入:000.000.000.000。 阅读详情

using System;
using System.Collections.Generic;
using System.Text;

namespace IpBox
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;

    public class IpBox : System.Windows.Forms.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 = "";
        
        private System.ComponentModel.Container components = null;

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

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

        }

        
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        #region 组件设计器生成的代码
     
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.textBox4 = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.textBox3 = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.White;
            this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.panel1.Controls.Add(this.textBox4);
            this.panel1.Controls.Add(this.label3);
            this.panel1.Controls.Add(this.textBox3);
            this.panel1.Controls.Add(this.label2);
            this.panel1.Controls.Add(this.textBox2);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Controls.Add(this.textBox1);
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(136, 20);
            this.panel1.TabIndex = 1;
            // 
            // textBox4
            // 
            this.textBox4.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.textBox4.Location = new System.Drawing.Point(104, 0);
            this.textBox4.MaxLength = 3;
            this.textBox4.Name = "textBox4";
            this.textBox4.Size = new System.Drawing.Size(20, 14);
            this.textBox4.TabIndex = 6;
            this.textBox4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.textBox4.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox4_KeyPress);
            // 
            // label3
            // 
            this.label3.Location = new System.Drawing.Point(96, 0);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(8, 14);
            this.label3.TabIndex = 5;
            this.label3.Text = ".";
            this.label3.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // textBox3
            // 
            this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.textBox3.Location = new System.Drawing.Point(72, 0);
            this.textBox3.MaxLength = 3;
            this.textBox3.Name = "textBox3";
            this.textBox3.Size = new System.Drawing.Size(20, 14);
            this.textBox3.TabIndex = 4;
            this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.textBox3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox3_KeyPress);
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(64, 0);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(8, 14);
            this.label2.TabIndex = 3;
            this.label2.Text = ".";
            this.label2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // textBox2
            // 
            this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.textBox2.Location = new System.Drawing.Point(40, 0);
            this.textBox2.MaxLength = 3;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(20, 14);
            this.textBox2.TabIndex = 2;
            this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(32, 0);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(8, 14);
            this.label1.TabIndex = 1;
            this.label1.Text = ".";
            this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
            // 
            // textBox1
            // 
            this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.textBox1.Location = new System.Drawing.Point(8, 0);
            this.textBox1.MaxLength = 3;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(20, 14);
            this.textBox1.TabIndex = 0;
            this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
            // 
            // IpBox
            // 
            this.Controls.Add(this.panel1);
            this.Name = "IpBox";
            this.Size = new System.Drawing.Size(136, 20);
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);

        }
        #endregion

        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)
        {
            MaskIpAddr(textBox1, e);
        }

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

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

        private void textBox4_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            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) > 223) // 进行验证
                            {
                                MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 223 之间的数值。");
                                textBox.Text = "223";
                                textBox.Focus();
                                return;
                            }

                            textBox2.Focus();
                            textBox2.SelectAll();

                        }
                        else if (textBox.Name == "textBox2")
                        {
                            if (Int32.Parse(tmp) > 255)
                            {
                                MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
                                textBox.Text = "255";
                                textBox.Focus();
                                return;
                            }
                            textBox3.Focus();
                            textBox3.SelectAll();
                        }
                        else if (textBox.Name == "textBox3")
                        {
                            if (Int32.Parse(tmp) > 255)
                            {
                                MessageBox.Show(tmp + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
                                textBox.Text = "255";
                                textBox.Focus();
                                return;
                            }
                            textBox4.Focus();
                            textBox4.SelectAll();
                        }
                        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();
                            textBox2.SelectAll();
                        }
                        if (textBox.Name == "textBox2" && textBox.Text != "")
                        {
                            textBox3.Focus();
                            textBox3.SelectAll();
                        }
                        if (textBox.Name == "textBox3" && textBox.Text != "")
                        {
                            textBox4.Focus();
                            textBox4.SelectAll();
                        }
                        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  // 
             {
              if(value != null)
              {
               // 255-255-255-255
               //string pattern = @"^([1-9]|/d{2}|1[0-9]{1,2}|2[0-5]{2})/.(/d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})/.(/d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})/.(/d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})$";
               // 223-255-255-255
               string pattern = @"^([1-9]|/d{2}|1[0-9]{1,2}|2[1-2][1-3])/.(/d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})/.(/d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})/.(/d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})$";

               if( !Regex.IsMatch(value , pattern) )
               {
                _text = "";
                throw new Exception("IP 地址格式错误!");
               }
               else
               {
                string[] ipnum = value.Split('.');
                textBox1.Text = ipnum[0];
                textBox2.Text = ipnum[1];
                textBox3.Text = ipnum[2];
                textBox4.Text = ipnum[3];
               }
              }

              _text = value;

             }
            */

        }

    }
}

 
C# 学习自定义控件 再做一个ip地址控件(经过测试的) 4个textBox,3个label 设置适当的风格后,一个外观不错的ip地址控件就呈现出来了和平时大家使用的ip地址控件一样,输入.就进入下一文本框.每个文本框的值都不能大于255.代码如下:(创建.net控件的步骤参见 C# 学习自定义控件,从一个简单实用的日期控件开始。  )using System;using System.Collections.Generic;using System 阅读详情

相关推荐

C# IP地址控件

思路在现有的textbox控件中,添加4个子textbox(IP各段)、3个label(3个点)、1个panel;以达到IP输入控件的效果。实现效果不会出现选中父IP控件的情况不会出现子IP控件始终在顶部的情况不会出现子控件遮罩父控件的情况IP地址第一位不大于223,其余不大于255(这个在下列代码中未呈现);子网掩码均不大于255当一段IP输入等于3位数时,自动跳至下一段当按下.时自动跳至下一...

fonaix的博客 1366

C# WinForm自定义IP地址输入框控件

最近在做使用网络的软件时,涉及了输入IP地址和获取IP地址,WinForm界面中直接用TextBox或四个TextBox控件总觉不象回事,后来改用WinForm自带的MaskedTextBox控件,虽然通过设置掩码可以实现IP地址输入,但是发现与Windows操作系统中设置IP有很大差别,比如在第一个IP段删除地址,后面三个IP段的地址会跟着向左移动,另外,假如某一段IP地址只有一位,它显示的不居中,感觉很别扭,所以MaskedTextBox控件体验感不好。

b07lxh的博客 4044

制作一个可以输入 IP 地址的控件

 准备工作:          1. 首先,打开vs.net,新建一个C#类库项目。如叫 IpBoxControl。     2. 在vs.net 的菜单,“项目”,“添加用户控件”,把这个用户控件取名为IpBox。     3. 把一个Panel从工具箱拖到IpBox的设计视图,取名为panel1,设置panel1的Location为(0,0),Size为(136, 20),BorderSt

萝卜青菜 1285

Winform / WPF 自定义控件 —— IPV4 地址输入框

仿 Window 原生风格 IPV4 地址输入框。

YMGogre的博客 1483

C# 编写自定义 IP 输入框控件

场景: 做一些网络设备应用时,需要有ip地址的输入,这就需要ip地址输入框控件 思路: 1 重写TextBox 为IpInputBox。 2 重写TextBox为SubIpInputBox 3 一个IpInputBox 添加4个SubIpInputBox 和3个Label 控件图:

遇见--专栏 8874

创建智能C# IP输入控件

C#(发音为 "C Sharp")是一种简单、现代、面向对象的编程语言,由微软公司开发。它被设计为具备简洁、类型安全、多用途等特点,适用于开发广泛的应用程序类型,包括但不限于Windows应用程序、Web服务、分布式组件以及游戏开发。C#的设计受到了Java、C++以及Delphi语言的影响,并且是.NET框架的核心编程语言之一。.NET框架是由微软开发的一个软件框架,提供了编写、部署和运行.NET应用程序所需的基础结构。

weixin_42527589的博客 1113

C#创建IP地址输入框

本文欲开发一个控件名为IPAddressTool的IP地址输入框 1.思路:一个IPAddressTool由四个TextBox和三个Label组成,在窗口中做好布局,将四个TextBox和三个Label排列规整。

nj2011zy的博客 6656

C# WinForm自定义的IP地址输入框控件(.Net Framework 4.5.2)

可以设置和读取IP地址,具有跟Window的IP地址输入框一样的功能,箭头方向键、空格、句点、删除键可以在四个IP地址段内连续移动,可拉伸控件,改变字体颜色及大小,改变背景颜色及IP地址框的背景颜色。对输入的IP地址自动校验是否正确,输入IP地址错误会提醒,同时IP地址赋值错误会自动采用默认地址127.0.0.1。VS引用该控件的dll后,会自动显示在VS的工具箱中,控件的属性窗口内新增IP地址输入框背景和IP地址设置两个属性,在事件窗口中新增改变IP地址框背景后触发的一个事件。开发于.Net Framework 4.5.2,更高版本请测试使用,或者留言索取更高版本。

C#使用TextBox控件制作IP输入控件

摘要:C#源码,控件类库,TextBox 运行环境:Visual Studio2010

自定义IP地址输入控件以及devExpress的IP输入控件

1、自定义IP地址输入框控件,将IP用“.”分隔。满三位焦点自动移动到下一字段,可按左右箭头移动输入焦点。 2、另一个是devEXpress的IP控件,如果打开后提示缺少dll,可重新引用DevExpress文件夹下面的dll 3、使用正则表达式判断IP的合法性

基于c#自定义封装控件(IP地址填写控件)

基于c#自定义封装控件(IP地址填写控件)

C#自定义IP输入框控件

场景: 做一些网络设备应用时,需要有ip地址的输入,这就需要ip地址输入框控件 思路:        1 重写TextBox 为IpInputBox。         2 重写TextBox为SubIpInputBox         3 一个IpInputBox 添加4个SubIpInputBox 和3个Label 控件图: 具体代码: 主窗口文件   Dialog.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.

WinForm IP地址输入框控件实现

主要为大家详细介绍了WinForm IP地址输入框控件的实现代码,基于VS2010模拟windows系统自带IP输入框,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

C#winform中IP输入框控件

可以通过自定义控件的方式做一个IP输入框,目前这个是一个IP输入控件的类,还不能在工具箱中拖放,希望以后做一个可以放在工具箱中的,这个类如下: public class IpInputBox : Label { private bool _isNetmask = false; public bool IsNetmask { ...

zxhadolph的博客 1万+

THUCNews新闻文本分类-tfidf+sklearn

本文使用的是和鲸社区现有数据集,代码也在和鲸社区公开,fork之后可以直接运行。 本文中所用的数据集清华NLP组提供的THUCNews新闻文本分类数据集的一个子集(原始的数据集大约74万篇文档,训练起来需要花较长的时间)。 本次训练使用了其中的体育, 财经, 房产, 家居, 教育, 科技, 时尚, 时政, 游戏, 娱乐10个分类,每个分类6500条,总共65000条新闻数据。项目在和鲸社区的平台上跑的,数据集直接引用了和鲸的数据集,每个分类6500条,总共65000条新闻数据。 数据集划分如下: cnews

wmq104的博客 4596

数据仓库实践:从ITR流程讲指标体系建设

从 ITR 流程或者其他客服反馈流程的考核过程可见,实际工作中不可能只通过单个指标评价一个业务过程,需要从多个维度梳理业务,并把多个高度相关的指标组织起来,以避免只考虑其中某个指标从而摧毁整个业务过程的结果,指标体系=指标+体系。

the_singing_towers的博客 2281
上一篇: 实现udp打洞的代码
下一篇: DevExpress 的 ASPxGridView 控件显示主从表
rainbowsoftware
博客等级 码龄20年 21粉丝 19原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值