问题解决马上结贴(小问题)

Mither 2007-06-11 09:34:49
我是刚刚用C#的
如果我有一个textbox,我怎么才能在里面输入合法IP地址呢.
我想要代码.
希望大家帮个忙
...全文
180 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
AhJo 2007-06-11
  • 打赏
  • 举报
回复
还是不太懂什么意思………………


string ip;
int port;


IPAddress ipAddress = IPAddress.Parse(ip);

ipAddress
IPEndPoint endpoint = new IPEndPoint(ipAddress, port);
LeoMaya 2007-06-11
  • 打赏
  • 举报
回复
你像这个用上面的Method, 不行么?


private void textBox2_Validated(object sender, EventArgs e)
{
if (IPValidationCheck(textBox2.Text))
{
MessageBox.Show("Good IP...", "IP Checking", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Invalid IP...", "IP Checking", MessageBoxButtons.OK, MessageBoxIcon.Stop);
textBox2.Text = "";
textBox2.Focus();
}
}
Mither 2007-06-11
  • 打赏
  • 举报
回复
我希望大家帮我的忙,我现在去睡觉了.
明天一早来结.

谢谢大家了.
Mither 2007-06-11
  • 打赏
  • 举报
回复
我用的是vs2005
Mither 2007-06-11
  • 打赏
  • 举报
回复


我的意思,在运行后是我可以在textBox中任意写上ip地址.而且是合法的.

希望给一个完整的CODE
LeoMaya 2007-06-11
  • 打赏
  • 举报
回复
或者

try
{
System.Net.Sockets.AddressFamily lIp = IPAddress.Parse(YourTestIP).AddressFamily;
MessageBox.Show("Good IP..." + lIp.ToString(), "IP Checking", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch
{
MessageBox.Show("Invalid IP...", "IP Checking", MessageBoxButtons.OK, MessageBoxIcon.Stop);
}
rononwang 2007-06-11
  • 打赏
  • 举报
回复
if.... 好办法……
LeoMaya 2007-06-11
  • 打赏
  • 举报
回复
private bool IPValidationCheck(string ip)
{
bool ret = false;

string pattern = @"^\d{1,3}(\.\d{1,3}){3}$";
Match m = new Regex(pattern).Match(ip);
if (m.Success)
{
string[] pieces = ip.Split('.');
foreach (string piece in pieces)
{
if (int.Parse(piece) <= 255)
{
ret = true;
}
else
{
ret = false;
break;
}
}
}

return ret;
}
birdinsea 2007-06-11
  • 打赏
  • 举报
回复
楼上的感觉可行!
rononwang 2007-06-11
  • 打赏
  • 举报
回复
/d[1-254]./d[1-254]./d[1-254]./d[1-254]
rononwang 2007-06-11
  • 打赏
  • 举报
回复
写个正则表达式!

110,539

社区成员

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

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

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