How to disable the space bar in maskedTextBox?

I have a masked text box of an IP address. the mask is set as 099.099.099.099

and it allows user to enter space bar. I' like to know how to disable the space bar key i.e. when a user press space bar then nothing happen, the cursor is still at the same position..

I've tried to use the keyDown and keyPress with the e.handled = true;

but it dose not work.

Can anyone help me

Thank you



Answer this question

How to disable the space bar in maskedTextBox?

  • Joel Parmer

    hi,

    i've tried what you have suggested (also try 000.000.000.000). However, the space bar can still be pressed i.e. the cursor still move to the next position.

    did i miss out anything

    Thankyou


  • DBornack

    Thank you.

    It works!


  • O_er

    The '9's in your mask mean numbers 0-9 or space. For an IP you probably want a mask of "990\.990\.990\.990" Using "\." instead of '.' should avoid having the decimal point being globalized based on the user's local.



  • J. Clay

    Actually, the best approach is to probably wrap the IP common control; which .NET currently seems not to do.

  • reminvestor

    See if this helps

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

        {

        if (e.KeyChar == ' ' ) e.KeyChar = (char)0;

        }

     

    By changing the numerical value of the e.KeyChar to 0, it is seen as null.   Don't use the null word though;


  • waikong

    hi,

    just one more thing to what peter said

    9 mean a digit or space

    0 is just a digit

    or you can use #

    you will find all sympols that you can insert as mask in your maskedtextbox

    http://msdn2.microsoft.com/en-us/library/system.windows.forms.maskedtextbox.mask.aspx

    hope this helps



  • How to disable the space bar in maskedTextBox?