如何在文本框中插入多个IP地址? [英] How to insert multiple ip address in text box?

查看:113
本文介绍了如何在文本框中插入多个IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在一个文本框控件中输入两个以上的ip地址使用c#(使用ip地址格式)



我尝试了什么:



i可以插入单个IP地址。

how can input more than two ip address in one text box control using c#(with ip address format)

What I have tried:

i can inserted single ip address.

推荐答案

MyTextBox.Text = "192.168.0.1";
MyTextBox.Text += ",";
MyTextBox.Text += "65.39.148.34";


在这两个扩展方法之一处抛出一个字符串集合,然后将MyTextBox.Text设置为返回的值。



Throw a string collection at one of these two extension methods, and then set MyTextBox.Text to the returned value.

public static class Extender
{
    public static string Delimited(this IEnumerable<string> list, string separator)
    {
        // sanity check to make sure separator has been specified. If you don't care, comment out 
        // this statement. If you're rather throw an exception, change the code appropriately.
        separator = (string.IsNullOrEmpty(separator)) ? "," : separator;
      
        StringBuilder result = new StringBuilder();
        foreach (string item in list)
        {
            result.AppendFormat("{0}{1}", (result.Length == 0) ? "" : separator, item);
        }
        return result.ToString();
    }

    public static string Delimited(this IEnumerable<string> list, char separator)
    {
        return list.Delimited(separator.ToString());
    }
}





示例用法:





Example usage:

string[] stringArray = new string[] { "1", "2", "3" };
List<string> stringList = new List<string>() { "4", "5", "6" };

string x = stringArray.Delimited(",");
string y = stringList.Delimited(';');





您可以进一步修改方法以接受任何类型,包括复杂类型,甚至指定哪个要分隔的对象中的属性。我把它作为程序员的练习。



啊,天哪,我也会给你通用版。请记住,如果在这些版本的方法中抛出除内在类型之外的任何东西,您将只返回类名称(当然,除非您的对象覆盖 ToString()方法)。





You can further modify the methods to accept any type, including complex types, and even specify which property in the object that is to be delimited. I leave that as an exercise for the programmer.

Ah hell, I'll give you the generic version as well. Keep in mind that if you throw anything other than intrinsic types at these versions of the method, you'll only get back the class name (unless, of course, your object overrides the ToString() method).

public static string Delimited<T>(this IEnumerable<T> list, string separator)
{
    StringBuilder result = new StringBuilder();
    foreach (T item in list)
    {
        result.AppendFormat("{0}{1}", (result.Length == 0) ? "" : separator, item.ToString());
    }
    return result.ToString();
}

public static string Delimited<T>(this IEnumerable<T> list, char separator)
{
    return list.Delimited(separator.ToString());
}





如果你想真正喜欢复杂的物体,你可以用反射来检查一个对象的属性,并处理嵌套复杂对象所需的所有令人讨厌的递归,或简单地将对象反序列化为json格式,并以这种方式迭代属性。



编程是好玩!



If you wanted to get really fancy with complex objects, you could either use reflection to examine an object's properties and do all the nasty recursion required to handle nested complex objects, or simply deserialize the object into json format, and iterate the properties that way.

Programming is fun!


也许你可以使用这个格式化的ip文本框控件: AC#IP地址控制 [ ^ ]

如果您想要多个IP地址,可以使用多个控件,或者根据自己的喜好调整控件......
Maybe you can use this formatted ip textbox control: A C# IP Address Control[^]
If you want multiple ip addresses you could use more than one control, or adapt the control to your liking ...


这篇关于如何在文本框中插入多个IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆