using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
namespace System.IPCtrl
{
/// <summary>
/// IP地址输入控件 written by haipeng
/// </summary>
/// <summary>
/// 表示IP地址的类型为A、B、C或D类地址
/// </summary>
public enum IPType:byte{A,B,C,D};
public class IPbox : System.Windows.Forms.UserControl
{
private System.Windows.Forms.TextBox txt1;
private System.Windows.Forms.TextBox txt2;
private System.Windows.Forms.TextBox txt3;
private System.Windows.Forms.TextBox txt4;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public IPbox()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
}
#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.txt1 = new System.Windows.Forms.TextBox();
this.txt2 = new System.Windows.Forms.TextBox();
this.txt3 = new System.Windows.Forms.TextBox();
this.txt4 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// txt1
//
this.txt1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txt1.Font = new System.Drawing.Font("SimSun", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.txt1.Location = new System.Drawing.Point(2, 2);
this.txt1.MaxLength = 3;
this.txt1.Name = "txt1";
this.txt1.Size = new System.Drawing.Size(24, 16);
this.txt1.TabIndex = 0;
this.txt1.Text = "";
this.txt1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.txt1.LostFocus += new System.EventHandler(this.txt_LostFocus);
this.txt1.GotFocus += new System.EventHandler(this.txt_LostFocus);
this.txt1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt1_KeyPress);
//
// txt2
//
this.txt2.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txt2.Font = new System.Drawing.Font("SimSun", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.txt2.Location = new System.Drawing.Point(34, 2);
this.txt2.MaxLength = 3;
this.txt2.Name = "txt2";
this.txt2.Size = new System.Drawing.Size(24, 16);
this.txt2.TabIndex = 1;
this.txt2.Text = "";
this.txt2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.txt2.LostFocus += new System.EventHandler(this.txt_LostFocus);
this.txt2.GotFocus += new System.EventHandler(this.txt_LostFocus);
this.txt2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt2_KeyPress);
//
// txt3
//
this.txt3.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txt3.Font = new System.Drawing.Font("SimSun", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.txt3.Location = new System.Drawing.Point(66, 2);
this.txt3.MaxLength = 3;
this.txt3.Name = "txt3";
this.txt3.Size = new System.Drawing.Size(24, 16);
this.txt3.TabIndex = 2;
this.txt3.Text = "";
this.txt3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.txt3.LostFocus += new System.EventHandler(this.txt_LostFocus);
this.txt3.GotFocus += new System.EventHandler(this.txt_LostFocus);
this.txt3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt3_KeyPress);
//
// txt4
//
this.txt4.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.txt4.Font = new System.Drawing.Font("SimSun", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.txt4.Location = new System.Drawing.Point(98, 2);
this.txt4.MaxLength = 3;
this.txt4.Name = "txt4";
this.txt4.Size = new System.Drawing.Size(24, 16);
this.txt4.TabIndex = 3;
this.txt4.Text = "";
this.txt4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
this.txt4.LostFocus += new System.EventHandler(this.txt_LostFocus);
this.txt4.GotFocus += new System.EventHandler(this.txt_LostFocus);
this.txt4.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txt4_KeyPress);
//
// label1
//
this.label1.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.label1.Font = new System.Drawing.Font("SimSun", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(26, 2);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(8, 16);
this.label1.TabIndex = 7;
this.label1.Text = ".";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label2
//
this.label2.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.label2.Font = new System.Drawing.Font("SimSun", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.Location = new System.Drawing.Point(90, 2);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(8, 16);
this.label2.TabIndex = 8;
this.label2.Text = ".";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label3
//
this.label3.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.label3.Font = new System.Drawing.Font("SimSun", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label3.Location = new System.Drawing.Point(58, 2);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(8, 16);
this.label3.TabIndex = 9;
this.label3.Text = ".";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label4
//
this.label4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label4.Font = new System.Drawing.Font("SimSun", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.label4.Location = new System.Drawing.Point(0, 0);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(122, 18);
this.label4.TabIndex = 10;
//
// IPbox
//
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.txt4);
this.Controls.Add(this.txt3);
this.Controls.Add(this.txt2);
this.Controls.Add(this.txt1);
this.Controls.Add(this.label4);
this.Name = "IPbox";
this.Size = new System.Drawing.Size(124, 20);
this.Load += new System.EventHandler(this.UserControl1_Load);
this.ResumeLayout(false);
}
#endregion
private void UserControl1_Load(object sender, System.EventArgs e)
{
}
public void Clear()
{
this.txt1.Text="";
this.txt2.Text="";
this.txt3.Text="";
this.txt4.Text="";
}
private void txt1_KeyPress(object sender, KeyPressEventArgs e)
{
try
{
string str=e.KeyChar.ToString();
if(e.KeyChar.ToString()=="." || e.KeyChar.ToString()=="。"||e.KeyChar.ToString()==" ")
{
if (txt1.SelectedText.ToString()=="" && txt1.Text.ToString()!="")
{
SendKeys.Send("{Tab}");
}
e.Handled=true;
return;
}
if (Regex.Match(e.KeyChar.ToString(),"[0-9]").Success)
{
if (txt1.Text.Length==2)
{
if(int.Parse(txt1.Text.ToString()+e.KeyChar.ToString())>255)
{
e.Handled=true;
}
else
{
txt2.Focus();
}
}
}
else
{
if(e.KeyChar.ToString()=="\b")
{
}
else
{
e.Handled=true;
}
}
}
catch
{
e.Handled=true;
}
}
private void txt2_KeyPress(object sender, KeyPressEventArgs e)
{
try
{
if(e.KeyChar.ToString()=="." || e.KeyChar.ToString()=="。"||e.KeyChar.ToString()==" ")
{
if (txt2.SelectedText.ToString()=="" && txt2.Text.ToString()!="")
{
SendKeys.Send("{Tab}");
}
e.Handled=true;return;
}
if (Regex.Match(e.KeyChar.ToString(),"[0-9]").Success)
{
if (txt2.Text.Length==2)
{
if(int.Parse(txt2.Text.ToString()+e.KeyChar.ToString())>255)
{
e.Handled=true;
}
else
{
txt3.Focus();
}
}
}
else
{
if(e.KeyChar.ToString()=="\b")
{
if(txt2.Text.ToString()=="")
{
txt1.Focus();
SendKeys.Send("{End}");
}
}
else
{
e.Handled=true;
}
}
}
catch
{
e.Handled=true;
}
}
private void txt3_KeyPress(object sender, KeyPressEventArgs e)
{
try
{
if(e.KeyChar.ToString()=="." || e.KeyChar.ToString()=="。"||e.KeyChar.ToString()==" ")
{
if (txt3.SelectedText.ToString()=="" && txt3.Text.ToString()!="")
{
SendKeys.Send("{Tab}");
}
e.Handled=true;return;
}
if (Regex.Match(e.KeyChar.ToString(),"[0-9]").Success)
{
if (txt3.Text.Length==2)
{
if(int.Parse(txt3.Text.ToString()+e.KeyChar.ToString())>255)
{
e.Handled=true;
}
else
{
txt4.Focus();
}
}
}
else
{
if(e.KeyChar.ToString()=="\b")
{
if(txt3.Text.ToString()=="")
{
txt2.Focus();
SendKeys.Send("{End}");
}
}
else
{
e.Handled=true;
}
}
}
catch
{
e.Handled=true;
}
}
private void txt4_KeyPress(object sender, KeyPressEventArgs e)
{
try
{
if(e.KeyChar.ToString()=="." || e.KeyChar.ToString()=="。"||e.KeyChar.ToString()==" ")
{
e.Handled=true;
}
if (Regex.Match(e.KeyChar.ToString(),"[0-9]").Success)
{
if (txt4.Text.Length==2)
{
if(int.Parse(txt4.Text.ToString()+e.KeyChar.ToString())>255)
{
e.Handled=true;
}
}
}
else
{
if(e.KeyChar.ToString()=="\b")
{
if(txt4.Text.ToString()=="")
{
txt3.Focus();
SendKeys.Send("{End}");
}
}
else
{
e.Handled=true;
}
}
}
catch
{
e.Handled=true;
}
}
private void txt_LostFocus(object sender, EventArgs e)
{
while(txt1.Text.Length>1 && txt1.Text.ToString().Substring(0,1)=="0")
{
txt1.Text=txt1.Text.ToString().Remove(0,1);
}
while(txt2.Text.Length>1 && txt2.Text.ToString().Substring(0,1)=="0")
{
txt2.Text=txt2.Text.ToString().Remove(0,1);
}
while(txt3.Text.Length>1 && txt3.Text.ToString().Substring(0,1)=="0")
{
txt3.Text=txt3.Text.ToString().Remove(0,1);
}
while(txt4.Text.Length>1 && txt4.Text.ToString().Substring(0,1)=="0")
{
txt4.Text=txt4.Text.ToString().Remove(0,1);
}
}
/// <summary>
/// 返回string类型的用户在IPBox控件中输入的IP地址
/// </summary>
override public string Text
{
get
{
return txt1.Text.ToString().Trim()+"."+txt2.Text.ToString().Trim()+"."+txt3.Text.ToString().Trim()+"."+txt4.Text.ToString().Trim();
}
set
{
try
{
string[] str=new string[4];
str=value.Split(char.Parse("."));
for(int i=0;i<4;i++)
{
if(int.Parse(str[i])>-1 && int.Parse(str[i])<256)
{
txt1.Text=str[0];
txt2.Text=str[1];
txt3.Text=str[2];
txt4.Text=str[3];
}
}
}
catch
{
}
}
}
/// <summary>
/// 返回System.Net.IPAddress类型的IP地址
/// </summary>
public IPAddress IPaddress
{
get
{
try
{
return IPAddress.Parse(this.Text.ToString());
}
catch
{
return IPAddress.Parse("0.0.0.0");
}
}
}
/// <summary>
/// 返回IP地址的类型(A,B,C,D),返回值为IPType类型
/// </summary>
public IPType Type
{
get
{
if (int.Parse(this.txt1.Text.ToString().Trim())<128)
{
return IPType.A;
}
else
{
if (int.Parse(this.txt1.Text.ToString().Trim())<192)
{
return IPType.B;
}
else
{
if(int.Parse(this.txt1.Text.ToString().Trim())<224)
{
return IPType.C;
}
else
{
return IPType.D;
}
}
}
}
}
public BorderStyle BorderStyle
{
get
{
return this.label4.BorderStyle;
}
set
{
try
{
this.label4.BorderStyle=value;
}
catch
{
}
}
}
}
}
相关推荐
在IT领域,网络编程是不可或缺的一部分,而与之相关的用户界面设计中,"输入IP地址的编辑框"是一个常见的元素。这种控件主要用于允许用户在应用程序中输入IPv4地址,通常由四个点分隔的数字段组成,每个段代表0到255...
在处理IP地址时,可能会用到文本框控件来接收用户输入,或者用复选框或下拉菜单来让用户选择是否允许特定的IP地址范围。 6. **源码**:在这个主题中,"源码"可能指的是一个实现IP地址分析功能的程序代码,可能包含...
其中,`CIPAddressCtrl`是专门用于处理IP地址输入的控件,它允许用户以标准的IPv4格式(四组0到255之间的数字,每组之间用点分隔)输入或查看IP地址。本实例将深入探讨`CIPAddressCtrl`的使用方法,以及如何在MFC...
本示例聚焦于如何在MFC应用中使用IP地址控件来显示和设置IP地址,这对于网络编程尤其重要,因为它允许用户直观地输入和查看网络设备的IP配置。 IP地址控件(CIPAddressCtrl)是Windows API提供的一种特殊控件,用于...
`IPAddress`可能是主程序类,而`IPAddressDlg`可能是对话框类,负责与用户交互,显示和获取IP地址。 2. `IPAddressDlg.h` 和 `IPAddress.h`:这些是头文件,包含类的声明和可能的数据结构定义。`IPAddressDlg.h`...
在C#.NET编程环境中,弹出窗口是一种常见且重要的用户交互元素。它们通常用于提示信息、获取用户输入或执行特定操作。在这个毕业设计项目中,我们深入研究了如何有效地使用和自定义弹出窗口,以提升用户体验。下面将...
在提供的资源中,"InfoTextBox.cs"可能是自定义的文本框类,它扩展了.NET Framework中的TextBox控件,添加了输入提示功能。"MainWindow.xaml.cs"是主窗口的后台代码,而"MainWindow.xaml"是相应的UI设计文件,其中...
4. ADO.NET:.NET框架下的数据库访问技术,如SqlConnection、SqlCommand、DataSet和DataTable等。 5. 面向对象编程:在.NET环境中实现继承、多态和接口。 6. 林、应用集、程序集和类型:理解.NET中的这些概念,它们...
【标题】"IP地址输入框.zip"所包含的项目是一个基于Qt框架的示例程序,主要目的是实现一个用户友好的IP地址输入控件。在Qt中,开发人员可以利用其丰富的库函数和类来创建图形用户界面(GUI),这个项目正是展示了...
confirm对话框类,msgshow(ByVal handle As System.Web.UI.Page, ByVal js_fuc_name As String, ByVal btn_ok As String, ByVal btn_cancel As String);handle调用该类的页面句柄,js_fuc_name 自动生成JS函数的...
"易语言源码IP编辑框类模块源码.rar" 是一个包含易语言编程中用于处理IP地址输入的类模块源码的压缩包。这个类模块可能是为了方便开发者创建能够输入和验证IPv4地址的编辑框控件。 在易语言中,类模块是一种可重用...
在.NET Framework中,对话框是一种常用的用户界面元素,用于向用户提供交互式的信息输入或者选择功能。本文将详细介绍Visual C#中常用的几种对话框,包括文件对话框(`FileDialog`)、字体对话框(`FontDialog`)、...
在C# WinForm中实现这样的弹窗,我们可以利用`MessageBox`类,这是一个内置的对话框类,能方便地弹出带有信息和按钮的窗口。例如,使用`MessageBox.Show()`方法,可以简单地展示一个包含文字的弹窗: ```csharp ...
通过熟练掌握这些方法,开发者可以在VC++应用程序中轻松地实现IP地址的输入和管理功能。 在提供的压缩包文件中,"IPAddress"可能包含了示例代码或者库文件,用于演示如何在实际项目中应用`IP Address`控件。建议...
在VC++编程环境中,开发人员经常需要自定义控件以满足特定的需求,比如在这个场景中,我们有一个专门用于输入浮点数的编辑框类。这个类是对标准的`CEdit`类进行派生,旨在限制用户只能输入有效的浮点数值,从而避免...
5. **实现函数**: 在源文件中,实现`OnBnClickedGetIp`函数,编写获取IP地址的逻辑: ```cpp void CMfcGetIPDlg::OnBnClickedGetIp() { // 初始化Winsock WSADATA wsaData; if (WSAStartup(MAKEWORD(2, 2), &...
一个ip地址格式输入框类,可以直接调用new 对象,IpAddressField ipAddrFld = new IpAddressField();
标题“不重启修改IP地址(vc6.0)”指的是在Visual C++ 6.0环境下,通过编程实现对网络适配器的IP地址进行修改,而无需重启计算机。这个功能对于系统管理员或者开发者来说非常实用,因为频繁的重启会降低工作效率。 ...
在源代码中,`AdapterIPConfig`和`AdapterIPConfigDlg`类很可能提供了用户界面,允许用户选择网卡并输入新的IP地址和子网掩码。`AdapterIPConfig.cpp`中的`OnBnClickedOk`等函数可能是处理用户输入并执行读写注册表...
"安卓简单IP地址对话框"就是这样一个组件,它允许开发者创建一个对话框,用户可以在其中输入IPv4地址。这个功能对于网络相关的应用程序特别有用,比如连接到指定服务器或者配置Wi-Fi设置等。 创建自定义对话框是...