Search code examples
c#validationip-addresskeypressmaskedtextbox

Removing spaces in MaskedTextBox on Leave Event - IP Address Validation


I'm trying to do IPv4 validation in a maskedtextbox. My mask is set to ###.###.###.### and i have my Key Down Event handling the '.' key as going to next octet which works great... However, if an IP address dows not have 3 digits in each octet i get random spaces when I grab the textfield for use.

For example: if I type 72.13.12.1 the output is "72 .13 .12 .1" <- I don't want the spaces.

I've tried doing some validation like removing the spaces once I leave the maskedtextbox, but if I remove the spaces, my mask kicks back in and changes it to "721.321.1 ."

this.maskedTextBoxExternIP.ResetOnSpace = false;
this.maskedTextBoxExternIP.SkipLiterals = false;
this.maskedTextBoxExternIP.PromptChar = ' ';
this.maskedTextBoxExternIP.Mask = "###.###.###.###";
this.maskedTextBoxExternIP.ValidatingType = typeof(System.Net.IPAddress);
this.maskedTextBoxExternIP.KeyDown += new KeyEventHandler(this.maskedTextBoxExternIP_KeyDown);
this.maskedTextBoxExternIP.Enter += new EventHandler(this.maskedTextBoxExternIP_Enter);
this.maskedTextBoxExternIP.Leave += new EventHandler(this.maskedTextBoxExternIP_Leave);

        private void maskedTextBoxExternIP_Leave(object sender, EventArgs e)
        {
            // Resets the cursor when we leave the textbox  
            maskedTextBoxExternIP.SelectionStart = 0;
            // Enable the TabStop property so we can cycle through the form controls again  
            foreach (Control c in this.Controls)
                c.TabStop = true;
            IPAddress ipAddress;
            if (IPAddress.TryParse(maskedTextBoxExternIP.Text, out ipAddress))
            {
                //valid ip
            }
            else
            {
                //is not valid ip
                maskedTextBoxExternIP.Text = maskedTextBoxExternIP.Text.Replace(" ", string.Empty);
            }
        }
        // Handle the Enter event  
        private void maskedTextBoxExternIP_Enter(object sender, EventArgs e)
        {
            // Resets the cursor when we enter the textbox  
            maskedTextBoxExternIP.SelectionStart = 0;
            // Disable the TabStop property to prevent the form and its controls to catch the Tab key  
            foreach (Control c in this.Controls)
                c.TabStop = false;
        }
        // Handle the KeyDown event  
        private void maskedTextBoxExternIP_KeyDown(object sender, KeyEventArgs e)
        {
            // Cycle through the mask fields  
            if (e.KeyCode == Keys.Tab || e.KeyCode == Keys.OemPeriod || e.KeyCode == Keys.Decimal)
            {
                int pos = maskedTextBoxExternIP.SelectionStart;
                int max = (maskedTextBoxExternIP.MaskedTextProvider.Length - maskedTextBoxExternIP.MaskedTextProvider.EditPositionCount);
                int nextField = 0;

                for (int i = 0; i < maskedTextBoxExternIP.MaskedTextProvider.Length; i++)
                {
                    if (!maskedTextBoxExternIP.MaskedTextProvider.IsEditPosition(i) && (pos + max) >= i)
                        nextField = i;
                }
                nextField += 1;

                // We're done, enable the TabStop property again  
                if (pos == nextField)
                    maskedTextBoxExternIP_Leave(this, e);

                maskedTextBoxExternIP.SelectionStart = nextField;
            }
        }

Solution

  • @madreflection I finally got the IPAddressCrontrolLib to work I just used the source files and embedded the library that way. Had to do a little fudging around to clear all the errors that we there. All good now! Just needed a new day to get through that one. Thanks for your help.