C# masked text box

deleted picture deleted · Jul 28, 2012 · Viewed 11.1k times · Source

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 !");
        }

Answer

competent_tech picture competent_tech · Jul 28, 2012

Why not just make life easier on yourself and create 4 separate data entry boxes? I personally always have difficulty with the single textbox approach if I type too quickly or need to back up.

Then you can validate each box for valid data as the user leaves it to ensure that they don't enter an incorrect value.

And if you have access to a numeric editing text box, you could even set min and max values (or you could implement this yourself).