0

I need a Maskedtextbox/Textbox which one does accept versions just like : Exaple: X.X.X.X or X.X.X.XX or X.XX.X.X and so on (X = a digit)

Is there a possibility that i can say no alphabetical/symbol-inputs, it must have 4 points, and it cant be null. (probably i need a Textbox insteat of a Manskedtextbox)

I set the mask of my Maskedtextbox under Properties --> Mask

Current Mask = 9.9.9.9 Currently my Maskedtextbox does accept only X.X.X.X

Please correct me :) I'm learning so every constructive criticism is welcome :)

Gentle
  • 87
  • 10
  • You can use regular expression in the backend to check the input after the text has been submitted. Or you can just create 4 textbox which only accept 0-9 numbers. – Tianyun Ling Aug 01 '14 at 13:55
  • 1
    Seems to be you are after the same as http://stackoverflow.com/questions/7924000/ip-address-in-a-maskedtextbox – Mike Guthrie Aug 01 '14 at 14:04

1 Answers1

1

You could do something like this or use RegEx I'm sure

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    bool handled = false;
    //test if digit
    if (char.IsDigit(e.KeyChar))
    {
        //only allow 2 digits
        //test if length is long enough to contain a prior char
        if (textBox1.Text.Length - 1 >= 0)
        {
            //test if prior char is digit
            if (char.IsDigit(textBox1.Text[textBox1.Text.Length -1]))
            {
                //test if length is long enough to have 2nd prior char
                if (textBox1.Text.Length -2 >= 0)
                {
                    //test 2 prior char if it's a digit because you
                    //only want to allow 2 not 3
                    if (char.IsDigit(textBox1.Text[textBox1.Text.Length - 2]))
                    {
                        handled = true;
                    }
                }
            }
        }
    }
    //test if decimal
    else if (e.KeyChar.Equals('.'))
    {
        //only allow 4 decimals
        if (textBox1.Text.Count(t => t.Equals('.')).Equals(3))
        {
            handled = true;
        }
        else
        {
            //don't allow multiple decimals next to each other
            //test if we have prior char
            if (textBox1.Text.Length - 1 >= 0)
            {
                //test if prior char is decimal
                if (textBox1.Text[textBox1.Text.Length - 1].Equals('.'))
                {
                    handled = true;
                }
            }
        }
    }
    else
    {
        handled = true;
    }
    e.Handled = handled;
}

Hooking up events from the designer ...

enter image description here

Tsukasa
  • 6,342
  • 16
  • 64
  • 96
  • im asking this now because i'm a newby in programming so do i need a spezial "using System.???" for this :s (i know i should not ask questions in the comments :/) – Gentle Aug 01 '14 at 16:10
  • but i get a error at "if (char.IsDigit(e.KeyChar))", at "e.KeyChar" and at e.Handled = handled; "at e.Handled" its gonna be a newby mistake im sorry 4 that – Gentle Aug 01 '14 at 16:39
  • 'System.EventArgs' does not contain a definition for 'KeyChar' and no extension method 'KeyChar' accepting a first argument of type 'System.EventArgs' could be found (Are you missing a using directive or an assembly reference?) 'System.EventArgs' does not contain a definition for 'Handled' and no extension method 'Handled' accepting a first argument of type 'System.EventArgs' could be found (Are you missing a using directive or an assembly reference?) I hope i wrote it right – Gentle Aug 01 '14 at 16:49
  • You aren't hooking the right TextBox event. Use KeyPress – Tsukasa Aug 01 '14 at 16:51
  • i did that but its there. and than i pressed rightclick and renamed it: private void textBox1_KeyPress(object sender, EventArgs e) – Gentle Aug 01 '14 at 16:54
  • textBox1_KeyPress(object sender, KeyPressEventArgs e) is not textBox1_KeyPress(object sender, EventArgs e). Hook up the KeyPress event in the designer and it will create the method for you then just add the code inside – Tsukasa Aug 01 '14 at 16:57
  • ohh im sorry for wasting ure time :/ but thanks.... but now i get : no overload for textbox1_Keypress matches delegate 'System.EventHandler' At : " this.textBox1.TextChanged += new System.EventHandler(this.textBox1_KeyPress);" – Gentle Aug 01 '14 at 17:01
  • this.textBox1.TextChanged, sounds like you removed the method hooked up for this event. – Tsukasa Aug 01 '14 at 17:02
  • ... and that means?:/ i think im too bad to understand what you mean so if u dont want to help me anymore its ok... – Gentle Aug 01 '14 at 17:05
  • When you hooked up the TextChanged event on the TextBox it created a method in your code behind that should have looked like private void textBox1_TextChanged(object sender, EventArgs e), you likely changed it or deleted it. – Tsukasa Aug 01 '14 at 17:10
  • do you mean that : private void textBox1_KeyPress(object sender, System.EventArgs e) { throw new System.NotImplementedException(); } – Gentle Aug 01 '14 at 17:12
  • No, private void textBox1_TextChanged(object sender, EventArgs e) is missing. You had this event hooked up. You either renamed this or deleted it without removing the hookup in the designer. – Tsukasa Aug 01 '14 at 17:13
  • sounds like you didn't hook the event up. – Tsukasa Aug 01 '14 at 17:21
  • Wow Thank you for your patience :) have a nice Day :) it does work :3 – Gentle Aug 01 '14 at 17:32