3

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 !");
        }
deleted
  • 363
  • 4
  • 6
  • 12
  • anybody ? Please... ! sugest please other ways to input IP and check it ! – deleted Jul 28 '12 at 19:07
  • I'm curious... Does this validate the inserted ip-addresses? Or can you enter values like for instance 999.999.999.999? (Invalid values for an IP address). I would recommend going with the idea by @competent_tech, and split this into 4 separate fields, then validate each of them to a range between 0 and 255. Otherwise you might have to implement your own custom data type for validation. See the "Remarks" section here: http://msdn.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.validatingtype.aspx – Kjartan Jul 28 '12 at 20:13
  • @Kjartan I post your question, answer ! – deleted Jul 28 '12 at 22:29

4 Answers4

3

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

competent_tech
  • 44,465
  • 11
  • 90
  • 113
2

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
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
2

You can just strip out the spaces when you access the Text property, e.g.

maskedTextBox1.Text.Replace(" ", "");
Matt Varblow
  • 7,651
  • 3
  • 34
  • 44
  • Thank's this works fine, but i find better way,which convert and check for validity ,but anyway thanks for advance! – deleted Jul 28 '12 at 22:35
1

Yes, it's possible, take a look on MaskedTextBox.Mask Property, as per msdn

9 - Digit or space, optional.

Try to play with mask

GSerjo
  • 4,725
  • 1
  • 36
  • 55