C# masked text box

11,168

Solution 1

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

Solution 2

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

Solution 3

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

maskedTextBox1.Text.Replace(" ", "");

Solution 4

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

9 - Digit or space, optional.

Try to play with mask

Share:
11,168
deleted
Author by

deleted

Updated on June 05, 2022

Comments

  • deleted
    deleted almost 2 years

    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
    deleted almost 12 years
    With this will be more difficult for me to retrieve existing local address
  • deleted
    deleted almost 12 years
    Thank's this works fine, but i find better way,which convert and check for validity ,but anyway thanks for advance!