C#创建IP地址输入框

来源:互联网 发布:js解决跨域问题 编辑:程序博客网 时间:2024/06/08 00:56

本文欲开发一个控件名为IPAddressTool的IP地址输入框

1.思路:一个IPAddressTool由四个TextBox和三个Label组成,在窗口中做好布局,将四个TextBox和三个Label排列规整。

2.控件样式


3.源码

using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Ling;using System.Text;using System.Windows.Forms;namespaces IPv4TextBox{  public partial class IPAddressTool:UserControl   {     bool textboxValid = true;     public IPAddressTool()     {        InitializeComponent();     }     private void textBox1_TextChanged(object sender, EventArgs e)     {        int len = textBox1.Text.Length;        if(len == 3)        {           string currentStr = textBox1.Text;           int currentNum = Convert.ToInt32(CurrentStr);           if(currentNum > 223)           {               MessageBox.Show(currentNum +"Is Not Valid. Please Enter A Value Between 1 And223.","Error");               textBoxValid = false;               textBox1.Text = "223";           }           else if(currentNum == 0)           {                              MessageBox.Show(currentNum +"Is Not Valid. Please Enter A Value Between 1 And223.","Error");               textBoxValid = false;               textBox1.Text = "1";           }           else           {                if(textBoxValid)                {  SendKeys.Send("{Tab}");  }                    else                {                    textBox1.SelectionStart =0;                    textBoxValid = true;                 }            }         }     }        private void textBox2_TextChanged(object sender, EventArgs e)     {                 textBoxOnTextChanged(sender, e);     }     private void textBox3_TextChanged(object sender, EventArgs e)     {                  textBoxOnTextChanged(sender, e);     }     private void textBox4_TextChanged(object sender, EventArgs e)     {         textBoxOnTextChanged(sender, e);     }     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)           {               MessageBox.Show(currentNum +"Is Not Valid. Please Enter A Value Between 1 And223.","Error");               textBoxValid = false;               textBox.Text = "255";           }                    else           {                if(textBoxValid)                {                   if(textBox ==textBox4)                   { return; }                   else                   {SendKeys.Send("{Tab}"); }                }                else                {                    textBox.SelectionStart = 0;                    textBoxValid = true;                 }           }         }     }     private void textBox1_KeyPress(object sender, KeyPressEventArgs e)     {        textBoxOnKeyPress(e);     }     private void textBox2_KeyPress(object sender, KeyPressEventArgs e)     {        textBoxOnKeyPress(e);     }     private void textBox3_KeyPress(object sender, KeyPressEventArgs e)     {        textBoxOnKeyPress(e);     }     private void textBox4_KeyPress(object sender, KeyPressEventArgs e)     {        if((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar ==(char)8)        {            if(e.KeyChar == (char)8)            {                e.Handled = false;               return;            }            if(textBox4.TextLength == 3)            {                e.Handled = true;            }        }        else        {           e.Handled = true;        }     }     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 textBoxOnKeyDown(object sender, KeyEventArgs e)     {         TextBox textbox = sender as TextBox;         if(e.KeyValue == 37 || e.KeyValue == 38 || e.KeyValue == 8)         {            if(textbox.SelectionStart == 0)            {                SendKeys.Send("{Tab}");             }         }                  else if(e.KeyValue == 39 || e.KeyValue == 40)         {             string currentStr =textbox.Text;              if(textbox.SelectionStart ==currentStr.Length)              {                  SendKeys.Send("+{Tab}{End}");              }         }     }     private void textBox1_KeyDown(object sender, KeyEventArgs e)     {         textBoxOnKeyDown(sender, e);     }     private void textBox2_KeyDown(object sender, KeyEventArgs e)     {         textBoxOnKeyDown(sender, e);     }       private void textBox3_KeyDown(object sender, KeyEventArgs e)     {         textBoxOnKeyDown(sender, e);     }     private void textBox4_KeyDown(object sender, KeyEventArgs e)     {         textBoxOnKeyDown(sender, e);     }      public string ipAddressString()     {         string ipAddress = textBox1.Text.Trim() + "." +textBox2.Text.Trim() + "." + textBox3.Text.Trim() + "." +textBox4.Text.Trim();         retuan ipAddress;     }          public bool ipAddressNotNull()     {                if(textBox1.Text.Trim() != string.Empty&& textBox2.Text.Trim() != string.Empty &&              textBox3.Text.Trim() !=string.Empty && textBox4.Text.Trim() != string.Empty)         {              return true;         }         else         {            false;         }     }      public void setIpAddress(string ipAddressStr)     {         string[] textIpAddress = ipAddressStr.Split('.');         try         {              textBox1.Text = textIpAddress[0];              textBox2.Text = textIpAddress[1];              textBox3.Text = textIpAddress[2];              textBox4.Text = textIpAddress[3];         }         catch{}     }     }}


原创粉丝点击