Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How disallow space, blank, ("_") charachter from text box input by keyboard

Tags:

c#

wpf

keypress

How disallow space, blank, ("_") character from text box input by keyboard (WPF, C#)?

I try by code:

Regex regex = new Regex(@"^[A-Za-z0-9\[\]/!$%^&*()\-_+{};:'£@#.?]*$"); 

but this part of code disallow all character, but not space.

like image 962
WPFwtf Avatar asked Oct 26 '25 17:10

WPFwtf


1 Answers

You can add PreviewKeyDown handler:

private void textBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Space)
    {
        e.Handled = true;
    }
}

Now your textbox will ignore spaces.

like image 99
Roman Doskoch Avatar answered Oct 28 '25 07:10

Roman Doskoch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!