Search code examples
c#ipmaskedtextbox

C# masked text box


How can I change masked text box properties for IP address input? For example

private void Interneta_savienojums_Load(object sender, EventArgs e)
{
    maskedTextBox1.Text = "   .   .   .   ";
    maskedTextBox1.PromptChar = ' ';
    maskedTextBox1.Mask = "009.009.009.900";
    maskedTextBox1.ResetOnSpace = false;
    maskedTextBox1.SkipLiterals = false;
}

In form text box show ( . . . ), exactly what I want. When my input is 123.123.123.123 everything is okay, but when I input 23 .1 .001.200, the return value is 23 .1 .001.200, but I need 23.1.1.200. How can I remove spaces, and return the normal value? Is this possible or not?

For ip check i use ,and this is solution !

try
        {
            IPAddress IP = IPAddress.Parse("127.0.0.000");
            MessageBox.Show(IP.ToString());
        }
        catch (FormatException)
        {
            MessageBox.Show("Wrong ip !");
        }

Solution

  • try with this code

    var input = "009.009.009.900";
    var result = input.Trim();
    

    you can also use these function in the same domain

    String.Trim
    String.TrimEnd
    String.TrimStart
    String.Remove