携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第15天,点击查看活动详情
C# WinForm控件 IP输入框
前言:
我们在做Winform窗体的时候,需要Ip地址框,为此简单做一个IP地址框,方便我们下次使用调用我们自己的Ip地址框,我会把代码和文件放在文末方便大家二次开发,创作不易,大家点个赞收藏评论关注,你们的点赞评论关注是我创作的动力,让我们一起学习,一起进步,文章有什么问题评论指教,我们一起学习。
一、创建用户控件
博主使用的是直接在winform里面创建用户控件,你们也可以使用用户控件库创建并写代码,到时候生成DLL文件,在你们的项目工具箱内添加引用,然后就有你库里面的控件了。博主使用的是直接在项目里面创建,生成就自动出现在工具箱了。
二、设计界面
这个界面就用了三个lable标签和四个TextBox组成
三、代码编写
在后面的代码区编写代码。博主直接把代码贴出来给你们复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace TestIC00
{
public partial class IPAddress : UserControl
{
public IPAddress()
{
InitializeComponent();
}
TextBox IC00Txt;
private void IPInput_Load(object sender, EventArgs e)
{
IC00Txt = txt_1;
}
public void KeyDown(object sender, KeyEventArgs e)
{
IC00Txt = (TextBox)sender;
if (e.KeyCode == Keys.Left)
{
switch (IC00Txt.Name.Split('_')[1])
{
case "1":
break;
case "2":
if (IC00Txt.SelectionStart == 0 && IC00Txt.Text != "")
{
if (int.Parse(IC00Txt.Text) > 255)
{
MessageBox.Show(IC00Txt.Text + "输入IP数值有误:请输入1和255之间的值", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
IC00Txt.Text = "255";
IC00Txt.SelectionStart = 0;
}
else
{
txt_1.Focus();
}
}
else if (IC00Txt.Text == "")
{
txt_1.Focus();
}
break;
case "3":
if (IC00Txt.SelectionStart == 0 && IC00Txt.Text != "")
{
if (int.Parse(IC00Txt.Text) > 255)
{
MessageBox.Show(IC00Txt.Text + "输入IP数值有误:请输入1和255之间的值", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
IC00Txt.Text = "255";
IC00Txt.SelectionStart = 0;
}
else
{
txt_2.Focus();
}
}
else if (IC00Txt.Text == "")
{
txt_2.Focus();
}
break;
case "4":
if (IC00Txt.SelectionStart == 0 && IC00Txt.Text != "")
{
if (int.Parse(IC00Txt.Text) > 255)
{
MessageBox.Show(IC00Txt.Text + "输入IP数值有误:请输入1和255之间的值", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
IC00Txt.Text = "255";
IC00Txt.SelectionStart = 0;
}
else
{
txt_3.Focus();
}
}
else if (IC00Txt.Text == "")
{
txt_3.Focus();
}
break;
}
}
else if (e.KeyCode == Keys.Right)
{
switch (IC00Txt.Name.Split('_')[1])
{
case "1":
if (IC00Txt.SelectionStart == IC00Txt.Text.Length && IC00Txt.Text != "")
{
if (int.Parse(IC00Txt.Text) > 255)
{
MessageBox.Show(IC00Txt.Text + "输入IP数值有误:请输入1和255之间的值", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
IC00Txt.Text = "255";
IC00Txt.SelectionStart = IC00Txt.Text.Length;
}
else
{
txt_2.Focus();
}
}
else if (IC00Txt.Text == "")
{
txt_2.Focus();
}
break;
case "2":
if (IC00Txt.SelectionStart == IC00Txt.Text.Length && IC00Txt.Text != "")
{
if (int.Parse(IC00Txt.Text) > 255)
{
MessageBox.Show(IC00Txt.Text + "输入IP数值有误:请输入1和255之间的值", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
IC00Txt.Text = "255";
IC00Txt.SelectionStart = IC00Txt.Text.Length;
}
else
{
txt_3.Focus();
}
}
else if (IC00Txt.Text == "")
{
txt_3.Focus();
}
break;
case "3":
if (IC00Txt.SelectionStart == IC00Txt.Text.Length && IC00Txt.Text != "")
{
if (int.Parse(IC00Txt.Text) > 255)
{
MessageBox.Show(IC00Txt.Text + "输入IP数值有误:请输入1和255之间的值", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
IC00Txt.Text = "255";
IC00Txt.SelectionStart = IC00Txt.Text.Length;
}
else
{
txt_4.Focus();
}
}
else if (IC00Txt.Text == "")
{
txt_4.Focus();
}
break;
case "4":
break;
}
}
}
public void KeyPress(object sender, KeyPressEventArgs e)
{
IC00Txt = (TextBox)sender;
Regex regex = new Regex(@"^[0-9]+$");
if (!regex.IsMatch(e.KeyChar.ToString()) && e.KeyChar != (Char)Keys.Back)
{
e.Handled = true;
}
else if (e.KeyChar == (Char)Keys.Back)
{
e.Handled = false;
switch (IC00Txt.Name.Split('_')[1])
{
case "1":
break;
case "2":
if (IC00Txt.SelectionStart == 0)
{
txt_1.Focus();
if (txt_1.Text != "")
{
txt_1.Text = txt_1.Text.Substring(0, txt_1.Text.Length - 1);
}
txt_1.SelectionStart = txt_1.Text.Length;
}
break;
case "3":
if (IC00Txt.SelectionStart == 0)
{
txt_2.Focus();
if (txt_2.Text != "")
{
txt_2.Text = txt_2.Text.Substring(0, txt_2.Text.Length - 1);
}
txt_2.SelectionStart = txt_2.Text.Length;
}
break;
case "4":
if (IC00Txt.SelectionStart == 0)
{
txt_3.Focus();
if (txt_3.Text != "")
{
txt_3.Text = txt_3.Text.Substring(0, txt_3.Text.Length - 1);
}
txt_3.SelectionStart = txt_3.Text.Length;
}
break;
}
}
else
{
switch (IC00Txt.Name.Split('_')[1])
{
case "1":
if (IC00Txt.SelectionStart == IC00Txt.Text.Length)
{
if (int.Parse(IC00Txt.Text + e.KeyChar.ToString()) > 255)
{
MessageBox.Show(IC00Txt.Text + e.KeyChar.ToString() + "输入IP数值有误:请输入1和255之间的值", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
e.Handled = true;
IC00Txt.Text = "255";
}
else
{
e.Handled = false;
}
}
else if(IC00Txt.Text.Length != 3)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
break;
default:
if (IC00Txt.SelectionStart == IC00Txt.Text.Length)
{
if (int.Parse(IC00Txt.Text + e.KeyChar.ToString()) > 255)
{
MessageBox.Show(IC00Txt.Text + e.KeyChar.ToString() + "输入IP数值有误:请输入1和255之间的值", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
e.Handled = true;
IC00Txt.Text = "255";
}
else
{
e.Handled = false;
}
}
else if (IC00Txt.Text.Length != 3)
{
e.Handled = false;
}
else
{
e.Handled = true;
}
break;
}
}
}
public void TextChanged(object sender, EventArgs e)
{
if (IC00Txt.Text.Length == 3)
{
switch (IC00Txt.Name.Split('_')[1])
{
case "1":
if (IC00Txt.SelectionStart == IC00Txt.Text.Length)
{
txt_2.Focus();
}
break;
case "2":
if (IC00Txt.SelectionStart == IC00Txt.Text.Length)
{
txt_3.Focus();
}
break;
case "3":
if (IC00Txt.SelectionStart == IC00Txt.Text.Length)
{
txt_4.Focus();
}
break;
case "4":
break;
}
}
}
public string GetIPAddressText()//当你项目需要获取这ip地址框的值
{
return txt_1.Text + " " + txt_2.Text + " " + txt_3.Text + " " + txt_4.Text + " ";
}
public void SetIPAddressText(string value1,string value2,string value3,string value4)//也可以直接给IP地址框写值
{
this.txt_1.Text = value1;
this.txt_2.Text = value2;
this.txt_3.Text = value3;
this.txt_4.Text = value4;
}
}
}
四、效果展示及使用
注:在导入文件之后需要将你的命名改了改为你项目名,你可以看看你winform的namespace是什么直接复制过来改了就可以了
五、文件下载链接
大家可以拿着代码继续二次开发,让我们一起学习,一起进步
链接:(pan.baidu.com/s/1d06mkzRb…) 提取码:IC00
六、总结
这篇文章是一篇比较简单的的文章,我会进一步的学习并分享给大家一起学习,希望大家多多点赞评论,哈哈哈,你的点赞是我创作的动力,谢谢大家啦,让我们一起加油,努力吧!!!!
今天的文章C# WinForm控件 IP输入框分享到此就结束了,感谢您的阅读。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/21210.html