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{} } }}
阅读全文
0 0
- C#创建IP地址输入框
- IP地址输入框
- IP地址输入框
- C#实现的IP地址输入框类 kewords: IP地址框 C# .Net
- C#创建用户控件:IPv4地址输入框
- C#创建用户控件:IPv4地址输入框
- IP地址输入框制作
- 实现IP地址输入框
- 只能输入IP地址的输入框
- JavaScript写的IP 地址输入框
- VC 制作IP地址输入框
- 用MaskedTextBox制作 IP地址输入框
- QT:IP地址输入框的实现
- [Qt] IP地址输入框实现
- QT 自定义IP地址输入框
- IP地址快速输入
- Qt编程3--IP地址、MAC地址输入框
- 输入地址信息 C#
- html 基础篇
- 仿微信
- 文件系统的制作遇到的问题(genext2fs: couldn't allocate a block (no free space))
- virtualbox全屏显示
- 测试
- C#创建IP地址输入框
- iOS程序员的命令行使用
- 时间选择框
- ENVI二次开发函数汇总
- lua-FileUtils文件工具类
- Labview使用的初体验
- spring 获取Bean的几种方式
- 生产环境常见的HTTP状态码列表
- Websocket原理