C# maskedTextbox, how to disable whitespaces? - c#

I cant figure out how to disable whitespaces, I tried multiple things, and yes my mask is 00000000000 but still it allows whitespaces. Anyone know a fix?
Not much code to show, only:
Should only allow numbers to be entered, not whitespaces too :/

Add the KeyDown Event to your textbox and then add the following Code in the created method:
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Space)
{
e.Handled = true;
e.SuppressKeyPress = true;
return;
}
}

Related

Why won't my Enter Key Events in WPF C# do anything?

My enter key events won't do anything...not even show a simple textbox when pressing Enter in a textbox.
I am new to c# and coding in general.
Interestingly, my visual studio won't let some things go through like MessageBox.Show... It makes me do System.Windows.MessageBox.Show. Just in case this is a clue to what the problem may be...
Here is what I have...
private void textBoxPartNumber_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == System.Windows.Forms.Keys.Enter)
{
//textBoxQuantity.Focus();
System.Windows.MessageBox.Show("Testing 123");
System.Windows.Forms.SendKeys.Send("{TAB}");
e.Handled = true;
e.SuppressKeyPress = true;
}
}
TextBox property AcceptsReturn
<TextBox AcceptsReturn="true"/>
Use
if (e.KeyCode == System.Windows.Forms.Keys.Return)
Instead :)
If you're using WPF then it appears your event signature is incorrect. Try something like this:
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
//Do something
}
}
The KeyEventArgs class and Key enum are in the System.Windows.Input namespace in the PresentationCore assembly.

how to disable ctrl+A and ctrl+c in richtextbox in wpf?

my code is :
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Key==Key.LeftCtrl && e.Key==Key.C) || (e.Key==Key.RightCtrl && e.Key==Key.C))
{
MessageBox.Show("Copy not allowed !");
e.Handled = true;
}
}
or , another way , i have tried is :
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.Key==Key.C) && (Keyboard.Modifiers==ModifierKeys.Control))
{
MessageBox.Show("Copy not allowed !");
e.Handled = true;
}
}
But they do not work !
Please do not tell me to set Focusable="False" or IsHitTestVisible="False"
because after that , i can not use scrollbar !
Please help.
thanks.
You can handle the PreviewKeyDown event... you almost had it, you just needed to and (&) the Keyboard.Modifiers because it could contain more than just ModifierKeys.Control:
private void PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.C && (Keyboard.Modifiers & ModifierKeys.Control) ==
ModifierKeys.Control)
{
MessageBox.Show("CTRL + C was pressed");
}
}
I assume your problem is not really how to disable ctrl+A and ctrl+C, just because you don't want the user to do exactly that, but to prevent the user from copying the content of the text box. Problem is, Ctrl+A Ctrl+C is not the only way to copy data. The user could select the text, and right click.
So what you should do then is not to override the keystroke, but the actual command that is being fired. (You may want to read up on how Commands works in WPF.)
To do that, simply add the following method to your class
private void CancelCopyCommand(object sender, DataObjectEventArgs e)
{
MessageBox.Show("Copy not allowed !");
e.CancelCommand();
}
And in the Constructor, register the command as follow:
DataObject.AddCopyingHandler(richTextBox1, CancelCopyCommand);
A richTextbox is for entering Text, ok you can make it readonly, so why do you want to prevent copy?
Try using Images to prevent copy, or disable focusing/selecting. If the user selects Text, destroy the selection.
You have to subscribe for the PreviewKeyDown-Event and in the Handler you have to set e.Handled = true if your Key-Combination was pressed.

Richtextbox deletes selected text

I have the following code:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.N)
{
richTextBox1.Select(1, 3);
}
}
When I press the N key , the selected text is replaced with "n". I read this Selecting text in RichTexbox in C# deletes the text ,but it had no effects.
I am using Windows Forms.
Likely, you will need e.Handled = true; in this to stop the event.
http://msdn.microsoft.com/en-us/library/system.windows.forms.keyeventargs.handled.aspx
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.N)
{
richTextBox1.Select(1, 3);
e.Handled = true;
}
}
Try it yourself:
Open up the editor, type some text, mark some of this text and press N. What happens? The marked text is replaced with n.
The same thing happens in your RichTextBox. Important to understand here is, that with the event you set up, you only add some functionality and leave the default event handling (handled by the OS) intact.
So with your code, on a key press you just do
richTextBox1.Select(1, 3);
which selects some characters and afterwards the default event handling kicks in. Thus there is some marked text which gets replaced with N.
So, you simply have to mark the event as handled by yourself. Not using the Handled-property, but with the SuppressKeyPress-property.
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.N)
{
richTextBox1.Select(1, 3);
e.SuppressKeyPress = true;
}
}
The documentation of Handled clearly states:
If you set Handled to true on a TextBox, that control will
not pass the key press events to the underlying Win32 text
box control, but it will still display the characters that the user typed.
Here is the official documentation of SuppressKeyPress.

how to make a multiline textbox not accept Enter and Backspace

I have a multiline textbox which shouldn't accept alphabets, numbers, newline(enter key) and backspace. In short, the textbox.Text shouldn't be editable. But I do want the textbox to accept two shortcut keys - control and control+R. One way I can make the textbox un-editable is by making it read-only. But then the textbox wont accept any keystroke at all. In short, my shortcuts ( control and control+R) wont work( Control + R) with read-only method.
Can anyone help in this regard. That's all I have to do.
One thing I could do here is not to make the textbox read-only and then restrict the characters(alphabets and digits) that could be inputted in the textbox. With this code:
private void txtResult_KeyPress(object sender, KeyPressEventArgs e)
{
// only modifier keys, escape, enter, backspace etc is accepted now
e.Handled = !char.IsControl(e.KeyChar);
}
private void txtResult_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control == true)
{
if (e.KeyCode == Keys.R)
{
// do something
}
else
{
//do something
}
}
if (e.KeyCode == Keys.Escape)
{
//do something
}
}
With this technique I can get the shortcuts(control and control+R) working. But the trouble with this method is that Enter and Backspace keys work as well making it possible to edit the text of textbox. How can I specifically restrict Enter and Backspace key being registered from the textbox, but let Control and Escape??
did you try SuppressKeyPress = true ?
private void textBox2_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control == true)
{
if (e.KeyCode == Keys.R)
{
// do something
}
else
{
//do something
}
}
else
e.SuppressKeyPress = true;
}
Since you are handling the keys in the KeyDown event handler, why not have your KeyPress handler return that all keystrokes are handled?
So just set e.Handled = true no matter what. I believe the backspace and enter would be interpreted as control characters, also.
The Enter and Backspace keys won't work if the textbox is set to ReadOnly, as you suggested early on in the question that you had done. Make sure the property is still set to true. You can either set it in the Properties window, or through code like so:
myTextBox.ReadOnly = true;
Then, you need to handle the KeyDown event for the textbox control, and watch for the specific keys that you're interested in. Something like this:
private void myTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control)
{
if (e.KeyCode == Keys.R)
{
MessageBox.Show("Pressed Ctrl+R");
}
else
{
MessageBox.Show("Pressed Ctrl");
}
}
else if (e.KeyCode == Keys.Escape)
{
MessageBox.Show("Pressed Esc");
}
}
This works exactly as expected, as long as the textbox is set to read-only. No other keys are recognized, and the user cannot change or modify any of the text in the textbox. You don't need to suppress the keypresses, as the control is already doing that when you set it to read-only. You also don't need to handle both the KeyDown and KeyPress events. KeyPress won't work for you anyway, as it doesn't let you handle control characters.

Having a MaskedTextBox only accept letters

Here's my code:
private void Form1_Load(object sender, EventArgs e)
{
maskedTextBox1.Mask = "*[L]";
maskedTextBox1.MaskInputRejected += new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected);
}
How can I set it to accept only letters, but however many the user wants? Thanks!
This would be easy if masked text boxes accepted regular expression, but unfortunately they don't.
One (albeit not very pretty) way you could do it is to use the optional letter ? mask and put in the same amount as the maximum length you'll allow in the text box, i.e
maskedTextBox1.Mask = "????????????????????????????????.......";
Alternatively you could use your own validation instead of a mask and use a regular expression like so
void textbox1_Validating(object sender, CancelEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(textbox1.Text, #"^[a-zA-Z]+$"))
{
MessageBox.Show("Please enter letters only");
}
}
Or yet another way would be to ignore any key presses other than those from letters by handling the KeyPress event, which in my opinion would be the best way to go.
private void textbox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), #"^[a-zA-Z]+$"))
e.Handled = true;
}
If you want only letters to be entered you can use this in keyPress event
if (!char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar)) //The latter is for enabling control keys like CTRL and backspace
{
e.Handled = true;
}

Categories

Resources