【问题标题】:textbox only allow ip address in textbox c#文本框仅允许文本框中的 IP 地址 c#
【发布时间】:2016-10-14 21:51:02
【问题描述】:

我正在尝试使文本框仅允许 IP 地址,而无需使用 Internet 进行验证。我将有一个“private void textBox3_TextChanged”或一个“timer1_Tick”来完成这项工作。每次我输入或打勾时,它都会检查它是否有效。这就是为什么我希望它快速,并且只使用简单的本地代码来检查它是否有效,即 0.0.0.0 - 255.255.255.255。

首先,它不应该做任何事情,但是当一个 ip 被写入时,它会启动一个计时器,然后检查该 ip 是否可以访问。这样做的目的是,当写入IP后,如果IP在大约4秒后无法访问,图片框将变为红色,如果可以访问,则变为绿色,然后停止直到“textbox3_TextChanged”

我尝试了类似 ping 的方法,但如果没有输入任何内容,它就会崩溃,如果无法访问 ip,它就会滞后:

private void timer1_Tick(object sender, EventArgs e)
    {
        Ping pingSender = new Ping();
        PingOptions options = new PingOptions();

        options.DontFragment = false;


        // Create a buffer of 32 bytes of data to be transmitted.
        string data = "ping";
        byte[] buffer = Encoding.ASCII.GetBytes(data);
        int timeout = 120;
        PingReply reply = pingSender.Send(textBox3.Text, timeout, buffer, options);
        if (reply.Status == IPStatus.Success)
        {
            pictureBox4.BackColor = Color.LimeGreen;
        }
        else
            pictureBox4.BackColor = Color.Red;
    }

这是截图:http://imgur.com/Cvix2Tr

请帮忙:)

【问题讨论】:

  • 可能你应该等待用户输入整个 ip,然后检查格式有效性和可达性。这样可以避免崩溃。滞后是 ping 方法发现 ip 不可达的常用方法。
  • 您可以使用textBoxKeyDown 事件并在那里检查是否已按下ENTER,然后进行检查
  • 只需将所有代码放入 try catch 块中
  • 我认为最好的做法是在用户按下回车键或单击按钮后检查 IP 地址。这样你就可以克服很多问题了。
  • IP 地址有 1 到 3 个数字段,以句点分隔,因此您可以编写一个方法来检查整个段是否有效并立即发送 ping 命令。如果发生异常,catch 块应该处理并显示“destination IP unreachable”错误。

标签: c# textbox ip


【解决方案1】:

您可以尝试将textbox3_TextChanged 替换为以下内容:

(对于这个例子,我的界面有一个名为 textBox 的 TextBox 和一个名为 textBlock 的 TextBlock)

//async to not freeze the UI
private async void TextBox_OnTextChanged(object sender, TextChangedEventArgs e)
{
    Ping pingSender = new Ping();
    var tb = (TextBox)sender;

    //a little regex to check if the texbox contains a valid ip adress (ipv4 only).
    //This way you limit the number of useless calls to ping.
    Regex rgx = new Regex(@"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$");
    if (rgx.IsMatch(tb.Text))
    {
        int timeout = 120;
        try
        {
            var reply = await pingSender.SendPingAsync(tb.Text, timeout);
            textBlock.Text = reply.Status == IPStatus.Success ? "OK" : "KO";
        }          
        catch (Exception ex) when (ex is TimeoutException || ex is PingException)
        {
            textBlock.Text = "KO";
        }
    }
    else
    {
        if (textBlock != null)
        {
            textBlock.Text = "Not valid ip";
        }
    }
}

【讨论】:

  • 我不明白你使用TextBlock是什么意思。它不在工具箱中,也不在事件中。请指定。
  • 我的意思是你在你的代码中使用了一个图片框,所以你可以用你的图片框替换我的文本块。 f.e: textBlock.Text = "KO"; -> pictureBox4.BackColor = Color.Red;
【解决方案2】:
   private void TxtEthStaticIP_Leave(object sender, EventArgs e)
        {
            Regex regex = new Regex(@"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");
            Match match = regex.Match(TxtEthStaticIP.Text);
            if (match.Success == false)
            {
                TxtEthStaticIP.Text = approvedethernetip;
            }
            else
            {
                approvedethernetip = TxtEthStaticIP.Text;
            }
        }

这是让 TextBox 像这样工作的另一个想法。您必须为默认文本声明一个已批准的字符串,这可能有助于某些看起来像这样的情况。

一个更好的解决方案可能是文本框的验证和验证事件

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多