想做一个输入ip地址的框框 (c/s结构 c#环境)

argent10 2010-02-04 04:49:36


1、每个框输入数字不得超过255,如果超过255,则只显示255.

2、如果框中输入的是3位数字,则自动跳转到下一框(最后一个框除外)

3、如果框中输入的是两位数字,并且按了“.”键,也跳转到下一框(最后一个框除外)

请问如何实现?
...全文
257 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
argent10 2010-02-05
  • 打赏
  • 举报
回复
错误提示:Error 1 Missing partial modifier on declaration of type 'NetIP.IPAddressControl'; another partial declaration of this type

出现了这个错误,我还不知道怎么解决...是什么原因啊?
rqx110 2010-02-05
  • 打赏
  • 举报
回复
这个问题还在纠结?
qlzf11140820 2010-02-05
  • 打赏
  • 举报
回复
[Quote=引用 20 楼 argent10 的回复:]
自带了?是哪一个?
[/Quote]lz说的是 maskedTextBox控件 Mask属性设置为999.999.999.999就可以了
但感觉还是自己做一个好
argent10 2010-02-05
  • 打赏
  • 举报
回复
自带了?是哪一个?
MicroSoftor 2010-02-05
  • 打赏
  • 举报
回复
C#中自带了这样的控件的呀
lude8880 2010-02-05
  • 打赏
  • 举报
回复
ding

argent10 2010-02-05
  • 打赏
  • 举报
回复
我怎么就报错了呢?



namespace NetIPSwitch_V1._0
{
using System;
using System.Net;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Drawing;

public class IPAddressControl : TextBox
{
private const int WM_NOTIFY = 0x004E,
WM_USER = 0x0400,
WM_REFLECT = WM_USER + 0x1C00,
IPN_FIRST = -860,
IPM_SETRANGE = (WM_USER + 103),
IPM_GETADDRESS = (WM_USER + 102),
IPM_SETADDRESS = (WM_USER + 101),
IPM_CLEARADDRESS = (WM_USER + 100),
IPM_ISBLANK = (WM_USER + 105),
ICC_INTERNET_CLASSES = 0x00000800,
CS_VREDRAW = 0x0001,
CS_HREDRAW = 0x0002,
CS_DBLCLKS = 0x0008,
CS_GLOBALCLASS = 0x4000,
WS_CHILD = 0x40000000,
WS_VISIBLE = 0x10000000,
WS_TABSTOP = 0x00010000,
WS_EX_RIGHT = 0x00001000,
WS_EX_LEFT = 0x00000000,
WS_EX_RTLREADING = 0x00002000,
WS_EX_LTRREADING = 0x00000000,
WS_EX_LEFTSCROLLBAR = 0x00004000,
WS_EX_RIGHTSCROLLBAR = 0x00000000,
WS_EX_NOPARENTNOTIFY = 0x00000004,
WS_EX_CLIENTEDGE = 0x00000200;
private int[] values = new int[4];
bool initialized = false;
public event FieldChangedHandler FieldChanged;
public IPAddressControl()
: base()
{
for (int i = 0; i < 4; i++)
values[i] = 0;
}
[StructLayout(LayoutKind.Sequential)]
public struct Nmhdr
{
public IntPtr HWndFrom;
public UIntPtr IdFrom;
public int Code;
}
[StructLayout(LayoutKind.Sequential)]
public struct NmIPAddress
{
public Nmhdr Hdr;
public int Field;
public int Value;
}
[StructLayout(LayoutKind.Sequential)]
public struct InitCommonControlsEX
{
public int Size;
public int Icc;
}
public enum IPField { OctetOne = 0, OctetTwo = 1, OctetThree = 2, OctetFour = 3 }
public delegate void FieldChangedHandler(object sender, FieldChangedEventArgs e);
public class FieldChangedEventArgs : EventArgs
{
private int _field, _value;
public int Field
{
get { return _field; }
}
public int Value
{
get { return _value; }
}
public FieldChangedEventArgs(int field, int value)
: base()
{
_field = field;
_value = value;
}
}

[DllImport("comctl32")]
static extern bool InitCommonControlsEx(ref InitCommonControlsEX lpInitCtrls);

protected virtual void OnFieldChanged(FieldChangedEventArgs e)
{
if (FieldChanged != null) FieldChanged(this, e);
}
protected override CreateParams CreateParams
{
get
{
if (!initialized)
{
InitCommonControlsEX ic = new InitCommonControlsEX();
ic.Size = Marshal.SizeOf(typeof(InitCommonControlsEX));
ic.Icc = ICC_INTERNET_CLASSES;
initialized = InitCommonControlsEx(ref ic);
}
if (initialized)
{
CreateParams cp = base.CreateParams;
cp.ClassName = "SysIPAddress32";
cp.Height = 23;
cp.ClassStyle = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS | CS_GLOBALCLASS;
cp.Style = WS_CHILD | WS_VISIBLE | WS_TABSTOP | 0x80;
cp.ExStyle = WS_EX_NOPARENTNOTIFY | WS_EX_CLIENTEDGE;
if (RightToLeft == RightToLeft.No
|| (RightToLeft == RightToLeft.Inherit
&& Parent.RightToLeft == RightToLeft.No))
{
cp.ExStyle |= WS_EX_LEFT | WS_EX_LTRREADING | WS_EX_RIGHTSCROLLBAR;
}
else
{
cp.ExStyle |= WS_EX_RIGHT | WS_EX_RTLREADING | WS_EX_LEFTSCROLLBAR;
}
return cp;
}
else
{
return base.CreateParams;
}
}
}
public bool SetIPRange(IPField field, byte lowValue, byte highValue)
{
if (!initialized) return false;
Message m = Message.Create(Handle, IPM_SETRANGE, (IntPtr)((int)field), MakeRange(lowValue, highValue));
WndProc(ref m);
return m.Result.ToInt32() > 0;
}
public System.Net.IPAddress IPAddress
{
get
{
if (!initialized) return IPAddress.None;
return IPAddress.Parse(base.Text);
}
}
public bool IsBlank
{
get
{
if (!initialized) return !(base.Text.Length > 0);
Message m = Message.Create(Handle, IPM_ISBLANK, IntPtr.Zero, IntPtr.Zero);
WndProc(ref m);
return m.Result.ToInt32() > 0;
}
}
new public void Clear()
{
if (!initialized)
{
base.Clear();
return;
}
Message m = Message.Create(Handle, IPM_CLEARADDRESS, IntPtr.Zero, IntPtr.Zero);
WndProc(ref m);
}
private System.Net.IPAddress GetIpAddress(IntPtr ip)
{
if (!initialized) return IPAddress.None;
return new IPAddress(ip.ToInt64());
}
private IntPtr MakeRange(byte low, byte high)
{
return (IntPtr)((int)((high << 8) + low));
}
protected override void WndProc(ref Message m)
{
if (m.Msg == (WM_REFLECT + WM_NOTIFY))
{
NmIPAddress ipInfo = (NmIPAddress)Marshal.PtrToStructure(m.LParam, typeof(NmIPAddress));
if (ipInfo.Hdr.Code == IPN_FIRST)
{
if (values[ipInfo.Field] != ipInfo.Value)
{
values[ipInfo.Field] = ipInfo.Value;
OnFieldChanged(new FieldChangedEventArgs(ipInfo.Field, ipInfo.Value));
}
}
}
base.WndProc(ref m);
}
}
}




错误提示:Error 1 Missing partial modifier on declaration of type 'NetIP.IPAddressControl'; another partial declaration of this type
vip__888 2010-02-04
  • 打赏
  • 举报
回复
一个一个判断
然后onfocus跳一下
wxm3630478 2010-02-04
  • 打赏
  • 举报
回复

/*恩 3楼的很好..........可以用

运行时不出错,但是会有一个提示:IPAddressControl 不是第一个类*/

//所有只要把

public class IPAddressControl : TextBox
{
//.........
//这个类写在最上面
}

[StructLayout(LayoutKind.Sequential)]
public struct Nmhdr {
public IntPtr HWndFrom;
public UIntPtr IdFrom;
public int Code;
}
[StructLayout(LayoutKind.Sequential)]
public struct NmIPAddress {
public Nmhdr Hdr;
public int Field;
public int Value;
}
....................................
.....................................
.................................
....................................
...................................
nashina 2010-02-04
  • 打赏
  • 举报
回复
以前自己做过,现在用的第三方控件直接就有这种控件了;
我那时做都是用最基本的判断,都没有用正则表达式;

好怀念啊
argent10 2010-02-04
  • 打赏
  • 举报
回复
Copy过去后,报了这个错,是什么原因啊?
argent10 2010-02-04
  • 打赏
  • 举报
回复
Error 1 Missing partial modifier on declaration of type 'NetIPSwitch_V1._0.IPAddressControl'; another partial declaration of this type exists C:\Users\Administrator\documents\visual studio 2010\Projects\NetIPSwitch V1.0\NetIPSwitch V1.0\IPAddressControl.Designer.cs 51 18 NetIPSwitch V1.0
rqx110 2010-02-04
  • 打赏
  • 举报
回复
直接copy过去 保存为.cs,放你项目里,编译一下,拖进窗体,over
argent10 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 hxmhh 的回复:]
本来准备写个代码的,看了楼主的结贴率才 60% ,
算了,还是不写了。
[/Quote]

我是没时间上网所以才结得少,一般都会找个时间一起结的...
argent10 2010-02-04
  • 打赏
  • 举报
回复

rqx110的好复杂,本人比较菜,看了都不知道怎样使用...可以给个使用说明书吗...
hxmhh 2010-02-04
  • 打赏
  • 举报
回复
本来准备写个代码的,看了楼主的结贴率才 60% ,
算了,还是不写了。
argent10 2010-02-04
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 baesky 的回复:]
直接有控件。。。。
[/Quote]

直接有这样的控件?哪一个啊?
Hamsic 2010-02-04
  • 打赏
  • 举报
回复
string pattern = @"^(0|[1-9][0-9]?|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.(0|[1-9][0-9]?|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.(0|[1-9][0-9]?|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.(0|[1-9][0-9]?|1[0-9]{2}|2[0-4][0-9]|25[0-5])$";


if (System.Text.RegularExpressions.Regex.IsMatch(this.txtServerIP.Text, pattern) && System.Text.RegularExpressions.Regex.IsMatch(this.txtClientIP.Text, pattern))

{

MessageBox.Show("合法的IP地址!");

}

else
{
MessageBox.Show("非法的IP地址!");

}
argent10 2010-02-04
  • 打赏
  • 举报
回复
没做过,可以给个例子吗?
Baesky 2010-02-04
  • 打赏
  • 举报
回复
直接有控件。。。。
加载更多回复(3)

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧