Why isn't e.Handled stopping the beeps? - c#

I've got multiple text and combo boxes in my Visual Studio 2012 project, and I have the keydown set to execute different events on Enter being pressed. And, there's that useless, stupidly annoying beep, every time. I've looked all over, found e.Handled = true and e.SuppressKeyPress = true solutions, and they're doing not a damn thing.
The code from one of my combobox is:
private void cmbNavigate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.SuppressKeyPress = true;
webBrowser1.Navigate(cmbNavigate.Text);
}
}
I've tried with the Suppress and the standard Handled and neither works, it just keeps beeping away mockingly at me.

Try e.Handled = true; in addition to e.SuppressKeyPress = true;

Related

Can't tab through combo and textboxes

I am currently working with an API for a program called Rhinoceros. The program does not allow tabbing through Forms so I am trying to program it in. It works well how I have it, however, when I try to tab from a combobox to a textbox or vice versa the cursor doesn't move. I have tried the select() and focus() function, neither seem to work and I am currently trying SelectNextControl but I cannot seem to get it to work either. If you have any ideas please let me know, anything is helpful.
private void cbNPProjectFolder_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyData == Keys.Tab)
{
txtbxNPProjectNum.SelectNextControl(sender as Control, true, false, true, true);
e.Handled = true;
e.SuppressKeyPress = true;
}
}
I ended up figuring it out, If anyone else has this problem:
I selected all of the containers I wanted to tab through, went to events on the bottom right, in the KeyDown box I named it Generic_KeyDown.
private void Generic_KeyDown(object sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Tab)
{
e.Handled = true;
this.SelectNextControl((Control)sender, true, true, true, true);
e.SuppressKeyPress = true;
}
}

C# maskedTextbox, how to disable whitespaces?

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;
}
}

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 can I send a single Downkey key when my user taps the numpad Enter key?

This is purely for a usability request my clients have asked me.
They work with a datagridview typing in grades for students. So they pretty much go into zombie mode and start tapping in numbers, and they've told me that it would be easier if they could tap the numpad enter key to have the next vertical select focused.
Here's what I've tried to use:
private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SendKeys.Send("{DOWN}");
}
}
Unfortunately, it doesn't work as expected. Sometimes it seems to fire that key many times, causing the focus to scroll 2 or 3 or 4 cells downwards and really shaking up the users focus.
How can I make this work and make my clients happy?
private void datagridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
bindingsource.MoveNext();
}
}
Try this - I have tested it and it works.
private void datagridView12_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
int col = dataGridView12.CurrentCell.ColumnIndex;
int row = dataGridView12.CurrentCell.RowIndex;
if (row != dataGridView12.NewRowIndex)
{
if (col == (dataGridView12.Columns.Count - 1))
{
col = -1;
row++;
}
dataGridView12.CurrentCell = dataGridView12[col, row + 1];
}
e.Handled = true;
base.OnKeyDown(e);
}
}
You could use a boolean that is set to true on keyDown and set to false on keyUp.
Only run your sendKeys.Send when the boolean is true. That should ensure that the event is only fired once for every key press.
That's often how I've seen it done in other languages, I'm not sure if there is a better way to do it in C#.
When you press Enter key in the DataGridView (whether in edit mode or not) it goes one cell down by default (in .NET framework 3.5 and 4.0).
Isn't it the functionality you needed in the first place?
Can you not set the focus on the next textbox when the user presses enter?
You can use the TextBox.Focus() method to move the next textbox.

Detect Key in KeyUp event

I have a textbox on a form where I'm trying to detect the keys the user types in. The TextBox is multilined with wordwrap on. I don't want the user the press the enter key (as I want all text entered on ONE line, wrapped) so I used the following code:
private void txtPlain_KeyPress(object sender, KeyPressEventArgs e) {
if (e.KeyChar == (char)13) {
MessageBox.Show("Enter keys are not allowed");
e.KeyChar = (char)0;
}
}
This worked fine in my tests, but when I tested for CTRL+ENTER it didn't work as I'm not sure how to detect for the control key. From my googling I found that I need to use the KeyUp/Down events so I now have the following Code:
private void txtPlain_KeyUp(object sender, KeyEventArgs e) {
//if (e.KeyData == (Keys.Control | Keys.Enter)) {
if (e.KeyCode == Keys.Enter || (e.KeyCode == Keys.Enter && e.Control)) {
MessageBox.Show("Enter keys are not allowed:");
//e.KeyValue = Keys.None;
}
}
The first commented out line didn't work for some reason so if anyone could explain why this would be useful.
The problem with the KeyUp/Down event is that I don't know how to REMOVE the enter key from the text - unlike the KeyPress event when I can set the KeyChar to zero. The event captures both the Enter and Ctrl+Enter keys, but the cursor still goes to the next line in the TextBox.
Thanks for any help on this.
Hmm, there's no reason to disallow the Enter key by handling the KeyDown or KeyUp events. You can simply set the AcceptsReturn property of the textbox control to False. This will prevent a multiline textbox from responding to a press of the Enter key.
Of course, this doesn't solve the problem of Ctrl+Enter. In fact, that's the expected way to create a new line when the AcceptsReturn property is set to False. To solve that, you will need to handle one of the keyboard events and prevent the control from receiving this input.
KeyDown is a good place to start. What you want to do is filter out any keyboard events that include the Keys.Enter flag. That will catch them no matter which other modifier key they might be combined with. Then, once you've found an Enter keypress, you want to set the e.Handled property to True in order to prevent it from being passed on to the control.
But unfortunately, we're not quite done yet. The textbox control tries to handle certain keys internally, and you're not going to be able to override that in a key event handler method. You also need to tell the control not to interpret that particular key as an input key. There are two primary ways of doing this. The first (and recommended way) is to inherit from the base TextBox class to create your own custom control, and then override the protected IsInputKey method. The second (somewhat simpler) way is just to handle the PreviewKeyDown event, and set the IsInputKey property to False.
Sample code:
private void txtPlain_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
// Check if the KeyCode value has the Keys.Enter flag set
if ((e.KeyCode & Keys.Enter) == Keys.Enter)
{
// Set the IsInputKey property to False
e.IsInputKey = false;
}
}
private void txtPlain_KeyDown(object sender, KeyEventArgs e)
{
// Check if the KeyCode value has the Keys.Enter flag set
if ((e.KeyCode & Keys.Enter) == Keys.Enter)
{
// Show the user a message
MessageBox.Show("Enter keys are not allowed in this textbox.");
// Prevent the key event from being passed on to the control
e.Handled = true;
}
}
And, though I assume this is for testing purposes only, you definitely want to take that MessageBox call out of there for production code. Find another way to alert the user that their input was not allowed, such as a short beep sound and an ErrorProvider component placed next to the textbox. Showing a message box is very jarring, and not very user-friendly. See my answer here for other hints and tips.
private void txtPlain_KeyUp(object sender, KeyEventArgs e) {
//if (e.KeyData == (Keys.Control | Keys.Enter)) {
if (e.KeyCode == Keys.Enter || (e.KeyCode == Keys.Enter && e.Control)) {
MessageBox.Show("Enter keys are not allowed:");
//e.KeyValue = Keys.None;
// mark event as handled
e.Handled = true;
}
}
from msdnlink
edit:
I think that you need the key down event not the key up
EDIT2
here is some tested code and it works as you wanted:
bool invalid=false;
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if ((e.KeyCode & Keys.Enter) == Keys.Enter)
{
invalid = true;
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (invalid)
{
e.Handled = true;
}
invalid = false;
}
The first commented out line didn't work for some reason so if anyone could explain why this would be useful.
You wanted to detect Ctrl + Enter.
if (e.KeyData == (Keys.Control | Keys.Enter)) {..
Keys.Control and Key.Enter are nothing but are some values please refer . Now doing logical or will not necessarily result to key which has been pressed. Totally illogical clause.
Ok now come to your actual problem you want to detect Enter stroke and Ctrl + Enter stroke to be treated as same.
Besides you want to undo the newline character thats been introduced. Try
PreviewKeyDown or Preview key up eventhandler with the following condition
if(e.KeyCode==Keys.Enter)
Let me know if this works

Categories

Resources