C#-制作IP地址栏

本文介绍了如何使用C#编程实现一个IP地址输入栏,主要涉及textBox和Label控件的运用。通过示例代码展示了界面效果,并引用了SendKeys用法、TextBox.SelectionStart属性及e.keyvalue值的相关资料。

前言:

由于项目需要使用C#来制作一个IP地址栏,制作的IP地址栏使用了textBox和Label控件。

界面效果:

在这里插入图片描述

具体代码:

 #region ip1段限制输入1-233
        private void txtip1_TextChanged(object sender, EventArgs e)
        {
            int len = txtip1.Text.Length;
            if (len == 3)
            {
                string currentstr = txtip1.Text;
                int currentNum = Convert.ToInt32(currentstr);
                if (currentNum > 223)
                {
                    //无效
                    textboxValid = false;
                    txtip1.Text = "223";
                    SendKeys.Send("{Tab}");
                }
                else if (currentNum == 0)
                {
                    //无效
                    textboxValid = false;
                    txtip1.Text = "1";
                }
                else
                {
                    if (textboxValid)
                    {
                        SendKeys.Send("{Tab}");
                    }
                    else
                    {
                        txtip1.SelectionStart = 0;
                        textboxValid = true;
                    }
                }
            }
           
        }
        #endregion
 #region ip段2,3,4限制输入到255
        //ip段2,3,4限制输入到255,公用方法
        private void textBoxOnTextChanged(object sender, EventArgs e)
        {
            TextBox textBox = sender as TextBox;
            int len = textBox.Text.Length;
            if (len == 3)
            {
                string currentStr = textBox.Text;
                int currentNum = Convert.ToInt32(currentStr);
                if (currentNum > 255)
                {
                    //无效
                    textboxValid = false;
                    textBox.Text = "255";
                }
                else
                {
                    if (textboxValid)
                    {
                        if(textBox == textBox4)
                        { return; }
                        else
                        { SendKeys.Send("{Tab}"); }
                    }
                    else
                    {
                        textBox.SelectionStart = 0;
                        textboxValid = true;
                    }
                }
            }
        }

        private void txtip2_TextChanged(object sender, EventArgs e)
        {
            textBoxOnTextChanged(sender, e);
        }

        private void txtip3_TextChanged(object sender, EventArgs e)
        {
            textBoxOnTextChanged(sender, e);
        }

        private void txtip4_TextChanged(object sender, EventArgs e)
        {
            textBoxOnTextChanged(sender, e);
        }
        #endregion
  #region ip限制输入内容为数字

        //限制输入内容给数字,公用方法
        private void textBoxOnKeyPress(KeyPressEventArgs e)
        {
            if ((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == (char)8)
            {
                if (e.KeyChar == (char)8)
                {
                    e.Handled = false;
                    return;
                }
            }
            else
            {
                e.Handled = true;
            }
        }

        private void txtip1_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxOnKeyPress(e);
        }

        private void txtip2_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxOnKeyPress(e);
        }

        private void txtip3_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxOnKeyPress(e);
        }        
        private void txtip4_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBoxOnKeyPress(e);
        }
        #endregion

      #region 对输入的上、下、左、右箭头设置
        private void textBoxOnKeyDown(object sender, KeyEventArgs e)
        {
            TextBox textbox = sender as TextBox;
            if (e.KeyValue == 37 || e.KeyValue == 38 || e.KeyValue == 8)//37:左箭头 38:上箭头 8:BACKSPACE 键
            {
                if (textbox.SelectionStart == 0)//光标后字符索引,从0开始
                {
                    SendKeys.Send("{Tab}");//效果输入Tab键
                }
            }
            else if (e.KeyValue == 39 || e.KeyValue == 40)//39:右箭头 40:下箭头
            {
                string currentStr = textbox.Text;
                if (textbox.SelectionStart == currentStr.Length)
                {
                    SendKeys.Send("+{Tab}{End}");
                }
            }
        }

        private void txtip1_KeyDown(object sender, KeyEventArgs e)
        {
            textBoxOnKeyDown(sender, e);
        }

        private void txtip2_KeyDown(object sender, KeyEventArgs e)
        {
            textBoxOnKeyDown(sender, e);
        }

        private void txtip3_KeyDown(object sender, KeyEventArgs e)
        {
            textBoxOnKeyDown(sender, e);
        }

        private void txtip4_KeyDown(object sender, KeyEventArgs e)
        {
            textBoxOnKeyDown(sender, e);
        }
        #endregion
#region 获得ip
        public string ipAddressString()
        {
            string ipAddress = txtip1.Text.Trim() + "." + txtip2.Text.Trim() + "." + txtip3.Text.Trim() 
            + "." + txtip4.Text.Trim();
            return ipAddress;
        }
        #endregion

参考资料:代码中的一些相关内容的学习
C# SendKeys用法
TextBox.SelectionStart Property
e.keyvalue的值的含义

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值