Click here to Skip to main content
15,908,166 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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.
Posted
Updated 21-Mar-18 8:18am
v2
Comments
#realJSOP 21-Mar-18 9:56am    
SHOW.US.YOUR.CODE.
Bryian Tan 21-Mar-18 14:53pm    
Is this a Windows Forms application?

C#
MyTextBox.Text = "192.168.0.1";
MyTextBox.Text += ",";
MyTextBox.Text += "65.39.148.34";
 
Share this answer
 
Comments
#realJSOP 21-Mar-18 11:43am    
I have an extension method that takes an IEnumerable collection (of strings) that produces a single delimited string from the values in the list.
Throw a string collection at one of these two extension methods, and then set MyTextBox.Text to the returned value.

C#
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:

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

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


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).

C#
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());
}


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!
 
Share this answer
 
v5
Comments
Richard Deeming 21-Mar-18 16:57pm    
If you're using a recent version of .NET, you can simplify those extension methods:
public static string Delimited<T>(this IEnumerable<T> list, string separator = ",")
{
    return string.Join(separator, list);
}

public static string Delimited(this IEnumerable<string> list, string separator = ",")
{
    return string.Join(separator, list);
}
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 ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900