C# 学习自定义控件 再做一个ip地址控件(经过测试的)

来源:互联网 发布:编程一小时怎么登陆 编辑:程序博客网 时间:2024/05/29 01:54

4个textBox,3个label 设置适当的风格后,一个外观不错的ip地址控件就呈现出来了
ip地址控件

和平时大家使用的ip地址控件一样,输入.就进入下一文本框.每个文本框的值都不能大于255.
代码如下:
(创建.net控件的步骤参见 C# 学习自定义控件,从一个简单实用的日期控件开始。  )

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

namespace IPaddressControl
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void UserControl1_Load(object sender, EventArgs e)
        {
            txtPartA.Text = "";
            txtPartB.Text = "";
            txtPartC.Text = "";
            txtPartD.Text = "";
        }

        /// <summary>
        /// 获取完整的ip值,如"192.168.0.1".
        /// </summary>
        /// <returns></returns>
        public string GetIpAddress()
        {
            string ipValue = string.Empty ;

            ipValue = ipValue + txtPartA.Text.Trim() + ".";
            ipValue = ipValue + txtPartB.Text.Trim() + ".";
            ipValue = ipValue + txtPartC.Text.Trim() + ".";
            ipValue = ipValue + txtPartD.Text.Trim();

            return ipValue;

        }

        /// <summary>
        /// 赋值.
        /// </summary>
        /// <param name="ipValue"></param>
        /// <returns>如果ipvalue的值不合法,返回错误.</returns>
        public bool SetIpAddress(string ipValue)
        {
            txtPartA.Text = string.Empty;
            txtPartB.Text = string.Empty;
            txtPartC.Text = string.Empty;
            txtPartD.Text = string.Empty;

            //判断ip地址是否合法.
            if (ipValue.Length < 7)
            {
                return false;
            }

            int index = 0;

            for (int i = 0; i < 3; i++)
            {
                index = ipValue.IndexOf(".", index + 1);

                if (index == -1)
                {
                    return false;
                }
            }


            //解析ip地址.
            string[] ipValues =  new string[4];

            ipValues = ipValue.Split('.');
            try
            {
                txtPartA.Text = Convert.ToInt16(ipValues[0]).ToString();
                txtPartB.Text = Convert.ToInt16(ipValues[1]).ToString();
                txtPartC.Text = Convert.ToInt16(ipValues[2]).ToString();
                txtPartD.Text = Convert.ToInt16(ipValues[3]).ToString();
            }
            catch
            {
                return false;
            }

            return true;
        }


        private void txtPartA_KeyPress(object sender, KeyPressEventArgs e)
        {
            InputRegular(e);

            //接收的字符为符号.时,则跳跃到下一textbox.
            if (e.KeyChar == 46)
            {
                txtPartB.Focus();
            }

        }

        private void txtPartB_KeyPress(object sender, KeyPressEventArgs e)
        {
            InputRegular(e);

            //接收的字符为符号.时,则跳跃到下一textbox.
            if (e.KeyChar == 46)
            {
                txtPartC.Focus();
            }
        }

        private void txtPartC_KeyPress(object sender, KeyPressEventArgs e)
        {
            InputRegular(e);

            //接收的字符为符号.时,则跳跃到下一textbox.
            if (e.KeyChar == 46)
            {
                txtPartD.Focus();
            }
        }

        private void txtPartD_KeyPress(object sender, KeyPressEventArgs e)
        {
            InputRegular(e);
        }

        /// <summary>
        /// 控制textbox只能输入数字.
        /// </summary>
        /// <param name="textBox"></param>
        /// <param name="e"></param>
        private void InputRegular(KeyPressEventArgs e)
        {
            //只能输入数字,退格键.
            if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 8)
            {

            }
            else
            {
                e.Handled = true;
            }
        }


        private void txtPartA_TextChanged(object sender, EventArgs e)
        {
            InputRange(txtPartA);
        }

        private void txtPartB_TextChanged(object sender, EventArgs e)
        {
            InputRange(txtPartB);
        }

        private void txtPartC_TextChanged(object sender, EventArgs e)
        {
            InputRange(txtPartC);
        }

        private void txtPartD_TextChanged(object sender, EventArgs e)
        {
            InputRange(txtPartD);
        }

        /// <summary>
        /// 控制最大值不能超过255.
        /// </summary>
        /// <param name="textBox"></param>
        private void InputRange(System.Windows.Forms.TextBox textBox)
        {
            //控制最大值不能超过255.
            if (textBox.Text.Length == 0)
            {
                return;
            }

            if (Convert.ToInt16(textBox.Text) > 255)
            {
                textBox.Text = "255";
            }
        }


    }
}

 

--需要源码的请你留下email即可。
如果您发现有什么错误,或则有更好的想法,也请您联系我!
谢谢!

原创粉丝点击