登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天道酬勤 玩物丧志

用勇气去改变可以改变的事情,用胸怀去包容无法改变的事情,用智慧去判断两者的区别

 
 
 

日志

 
 

【引用】C#实现IP地址控件  

2011-04-29 18:10:31|  分类: C#文件网络传输 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
本文转载自mervyn807《C#实现IP地址控件》

转自:http://hi.baidu.com/tllfyj/blog/item/7765863d4d8b16cc9f3d62df.html

.net控件库中没有为我们提供标准的IP地址输入这个控件,只能我们自己写了。下面是我写的一个IP地址空间(主要是改网上的后得到了,主要是改了Text属性和添加了TextChanged事件)。该控件继承至UserControl,下面说说该控件的界面部分。现在UserControl上添加一个Panel控件,然后再在Panel上添加四个TextBox控件和三个Label控件。将三个Label控件的Text属性设为“.”,四个Textbox控件的BorderStyle属性都设为“None”,Panel控件的BorderStyle属性设为Fixed3D,仔细调整个控件相互之间的位置,这样我们就可以做出IP地址控件的外观了。其他的我就不说了,直接看代码。

效果图:

C实现IP地址控件 - mervyn807 - mervyn807的博客

主类实现部分代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace TLControl
{
    public partial class IPAddressBox : UserControl
    {
        [Category("属性已更改")]
        [Browsable(true)]
        public event EventHandler TextChanged ;

        public IPAddressBox()
        {
            InitializeComponent();
        }

        private string _text;

        [Category("外观")]
        [Description("与空间关联的文本")]
        [Browsable(true)]
        public string Text
        {
            get
            {
                if (this.textBox1.Text.Length == 0
                    || this.textBox2.Text.Length == 0
                    || this.textBox3.Text.Length == 0
                    || this.textBox4.Text.Length == 0)
                {
                    _text = “”;
                    return _text;
                }
                else
                {
                    _text = Convert.ToInt32(this.textBox1.Text).ToString() + “.” +
                            Convert.ToInt32(this.textBox2.Text).ToString() + “.” +
                            Convert.ToInt32(this.textBox3.Text).ToString() + “.” +
                            Convert.ToInt32(this.textBox4.Text).ToString();
                    return _text;
                }
             }
            set
            {
                if (value != null)
                {
                    string[] strs = value.Split(’.');
                    Int32[] num = new Int32[4];
                    if (strs.Length == 4)
                    {
                        bool result = true;
                        for (int i = 0; i < 4; i++)
                        {
                            result = result && Int32.TryParse(strs[i], out num[i]);
                        }
                        if (result && num[0] <= 223 && num[1] <= 255
                            && num[2] <= 255 && num[3] <= 255)
                        {
                            this.textBox1.Text = strs[0];
                            this.textBox2.Text = strs[1];
                            this.textBox3.Text = strs[2];
                            this.textBox4.Text = strs[3];
                        }
                    }
                    else
                    {
                        this.textBox1.Text = “”;
                        this.textBox2.Text = “”;
                        this.textBox3.Text = “”;
                        this.textBox4.Text = “”;
                        _text = “”;
                    }
                }
                _text = value;
            }
        }

        private void MaskIPAddr(TextBox textBox,KeyPressEventArgs e)
        {
            //判断输入的值是否为数字或删除键
            if (char.IsDigit(e.KeyChar) || e.KeyChar == 8)
            {

                if (e.KeyChar!=8 && textBox.Text.Length == 2)
                {
                    string tempStr = textBox.Text + e.KeyChar;
                    if (textBox.Name == “textBox1″)
                    {
                        if (Int32.Parse(tempStr) > 223)
                        {
                            MessageBox.Show(tempStr + ” 不是一个有效项目。请指定一个介于 1 和 223 之间的数值。”);
                            textBox.Text = “223″;
                            textBox.Focus();
                            return;
                        }
                        this.textBox2.Focus();
                        this.textBox2.SelectAll();
                    }
                    else if (textBox.Name == “textBox2″)
                    {
                        if (Int32.Parse(tempStr)>255)
                        {
                            MessageBox.Show(tempStr + ” 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。”);
                            textBox.Text = “255″;
                            textBox.Focus();
                            return;
                        }
                        this.textBox3.Focus();
                        this.textBox3.SelectAll();
                    }
                    else if (textBox.Name == “textBox3″)
                    {
                        if (Int32.Parse(tempStr) > 255)
                        {
                            MessageBox.Show(tempStr + “ 不是一个有效项目。请指定一个介于 1 和 223 之间的数值。”);
                            textBox.Text = “255″;
                            textBox.Focus();
                            return;
                        }
                        this.textBox4.Focus();
                        this.textBox4.SelectAll();
                    }
                    else if (textBox.Name == “textBox4″)
                    {
                        if (Int32.Parse(tempStr) > 255)
                        {
                            MessageBox.Show(tempStr + ” 不是一个有效项目。请指定一个介于 1 和 223 之间的数值。”);
                            textBox.Text = “255″;
                            textBox.Focus();
                            return;
                        }
                    }
                }
                else if (e.KeyChar == 8)
                {
                    if (textBox.Name == “textBox4″ && textBox.Text.Length == 0)
                    {
                        this.textBox3.Focus();
                    }
                    else if (textBox.Name == “textBox3″ && textBox.Text.Length == 0)
                    {
                        this.textBox2.Focus();
                    }
                    else if (textBox.Name == “textBox2″ && textBox.Text.Length == 0)
                    {
                        this.textBox1.Focus();
                    }
                }
            }
            else
            {
                e.Handled = true;
            }
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            MaskIPAddr(this.textBox1, e);
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            MaskIPAddr(this.textBox2, e);
        }

        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            MaskIPAddr(this.textBox3, e);
        }

        private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
        {
            MaskIPAddr(this.textBox4, e);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox1.Text.Length != 0 && Int32.Parse(this.textBox1.Text) > 223)
            {
                MessageBox.Show(this.textBox1.Text + “不是一个有效项目。请指定一个介于 1 和 223 之间的数值。”);
                this.textBox1.Text = “223″;
                this.textBox1.Focus();
            }
            OnResize(this, new EventArgs());
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox2.Text.Length != 0 && Int32.Parse(this.textBox2.Text) > 255)
            {
                MessageBox.Show(this.textBox2.Text + “不是一个有效项目。请指定一个介于 1 和 255 之间的数值。”);
                this.textBox2.Text = “255″;
                this.textBox2.Focus();
            }
            OnResize(this, new EventArgs());
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox3.Text.Length != 0 && Int32.Parse(this.textBox3.Text) > 255)
            {
                MessageBox.Show(this.textBox3.Text + “不是一个有效项目。请指定一个介于 1 和 255 之间的数值。”);
                this.textBox3.Text = “255″;
                this.textBox3.Focus();
            }
            OnResize(this, new EventArgs());
        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            if (this.textBox4.Text.Length != 0 && Int32.Parse(this.textBox4.Text) > 255)
            {
                MessageBox.Show(this.textBox4.Text + “不是一个有效项目。请指定一个介于 1 和 255 之间的数值。”);
                this.textBox4.Text = “255″;
                this.textBox4.Focus();
            }
            OnResize(this, new EventArgs());
        }

        protected void OnResize(object sender,EventArgs e)
        {
            if (TextChanged != null)
            {
                TextChanged(sender, e);
            }
        }
    }
}

visual Studio 自动生成设计部分代码就不贴出出来了

  评论这张
 
阅读(325)| 评论(0)

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018