Is an easy way to cancel click event when user hit enter on button (instead of mouse click on button?)
i have tried with:
private void button3_Click(object sender, EventArgs e)
{
KeyEventArgs ke = e as KeyEventArgs;
if (ke != null)
{
if (ke.KeyCode == Keys.Enter)
{
return;
}
}
}
But ke is null
public void btnClick(object sender, EventArgs e)
{
bool IsMouse = (e is System.Windows.Forms.MouseEventArgs);
// If not mouse, they hit spacebar or enter
}
Yes it will be null. because EventArgs is not KeyEventArgs
KeyEventArgs will be passed as a parameter to KeyDown or KeyUp events. You're messing up things.
You can do something like this
private bool flag = false;
private void button1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
flag = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (flag)
{
flag = false;
return;
}
//else do original task
}
You can handle KeyPress event for the button and disable or ignore enter key there instead of returning in button click
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((keyData & Keys.KeyCode) == Keys.Enter)
{
SendKeys.Send("{Tab}");
return true;
}
return false;
}
Related
I am trying to disable people from deleting a textbox in a richtextbox. The project is using windows form.
Here is the code I have:
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.KeyPress += new KeyPressEventHandler(richTextBox1_KeyPress);
}
void richTextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)8)
{
e.Handled = true;
MessageBox.Show("Try not to delete... write freely and openly");
//The msgbox shows, but the delete still happens within the form.
}
}
Does not show messagebox and does not stop the delete:
private void Form1_Load(object sender, EventArgs e)
{
richTextBox1.KeyDown += new KeyEventHandler(richTextBox1_KeyDown);
}
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
e.Handled = true;
MessageBox.Show("Delete Pressed");
// Does not show message box...
}
}
Per the MSDN documentation on KeyPressEventArgs.KeyChar, you cannot get or set the DELETE key using that event. You will need to use the KeyEventArgs.KeyCode instead, subscribing to the KeyDown and KeyUp events.
My solution:
void richTextBox1_TextChanged(object sender, EventArgs e) {
richTextBox1.SelectAll();
richTextBox1.SelectionProtected = true;
richTextBox1.Select(richTextBox1.Text.Length, 0);
}
Side note: yes, this will flicker. Proof of concept only. To avoid the flicker, see How to append text to RichTextBox without scrolling and losing selection?
Instead Of KeyPress event use KeyDown In RichText Box.
try this to prevent from deleting text in RichText Box
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 46)
e.Handled = true;
}
If you want to disallow both delete and backspace You Can Change KeyDown Event as follows
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyValue == 8 || e.KeyValue == 46)
e.Handled = true;
}
You must add Back key to prevent delete :
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
{
e.Handled = true;
MessageBox.Show("Delete Pressed");
// Does not show message box...
}
}
Edit:
Non-selectable RichTextBox :
public class ViewOnlyRichTextBox : System.Windows.Forms.RichTextBox {
// constants for the message sending
const int WM_SETFOCUS = 0x0007;
const int WM_KILLFOCUS = 0x0008;
protected override void WndProc(ref Message m) {
if(m.Msg == WM_SETFOCUS) m.Msg = WM_KILLFOCUS;
base.WndProc (ref m);
}
}
My solution is some kind of the combination of SerkanOzvatan's and LarsTech's answers. Here is the code:
private void richTextBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete)
{
e.Handled = true;
MessageBox.Show("Try not to delete... write freely and openly");
// Does not show message box...
}
}
private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
richTextBox1.SelectionProtected = richTextBox1.SelectionLength > 0;
}
It works great :)
And here is another solution of my own which also works great, especially if you want to do with a TextBox (not a RichTextBox), it doesn't have a SelectionProtected, and this is used OK for both TextBox and RichTextBox (just change the class name in the following code accordingly):
public class WritableRichTextBox : RichTextBox
{
protected override bool ProcessKeyMessage(ref Message m)
{
int virtualKey = m.WParam.ToInt32();
if (SelectionLength > 0 || virtualKey == 0x08 || virtualKey == 0x2e)
{
if (virtualKey != 0x25 && virtualKey != 0x26 && virtualKey != 0x27 && virtualKey != 0x28)
return true;
}
return base.ProcessKeyMessage(ref m);
}
}
i already have code when user press key Enter on keyboard, it return tab and "jump" to next field, its working great, its possible make it for 2 or 3 textbox, problem when need make it on multiple textbox like 20 textbox for each form, its just not work.
See code:
// Detect if Enter key is pressed on each text box, mute sound enter "ding" sound and replace Enter for tab (problem that have make it for each textbox)
private void txtAltura_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true; //Silenciar Enter
SendKeys.Send("{TAB}");
}
}
private void txtLargura_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true; //Silenciar Enter
SendKeys.Send("{TAB}");
}
}
private void txtProfundidade_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter)
{
e.Handled = true; //Silenciar Enter
SendKeys.Send("{TAB}");
}
}
//execute keypress command when enter is typed on textbox
private void txtProfundidade_TextChanged(object sender, EventArgs e)
{
if (txtProfundidade.Text != "") { foreach (char c in txtProfundidade.Text.ToCharArray()) txtProfundidade_KeyPress(sender, new KeyPressEventArgs(c)); }
}
private void txtLargura_TextChanged(object sender, EventArgs e)
{
if (txtLargura.Text != "") { foreach (char c in txtLargura.Text.ToCharArray()) txtLargura_KeyPress(sender, new KeyPressEventArgs(c)); }
}
private void txtAltura_TextChanged(object sender, EventArgs e)
{
if (txtAltura.Text != ""){foreach (char c in txtAltura.Text.ToCharArray()) txtAltura_KeyPress(sender, new KeyPressEventArgs(c));}
}
Hope make it better.
Thanks in advance..
if its windows form app can use this, this will replace tab key press with Enter key
protected override bool ProcessKeyPreview(ref Message m)
{
if (m.Msg == 0x0100 && (int)m.WParam == 13)
{
this.ProcessTabKey(true);
}
return base.ProcessKeyPreview(ref m);
}
From what I understand, you're trying to figure out a way to assign event handlers to multiple textbox controls and don't want to write a handler for each one. If that's the case, try this:
private void Form1_Load(object sender, EventArgs e)
{
foreach (TextBox textBox in this.Controls.OfType<TextBox>())
{
textBox.KeyDown += new KeyEventHandler(textBox_KeyDown);
}
}
void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
SendKeys.Send("{TAB}");
}
}
This will assign a handler to each textbox control on the form.
That worked perfectly well.
I also have to repeat this on each textbox to check if all codes was filled and update statusbar:
private void txtAltura_TextChanged(object sender, EventArgs e)
{
testePreenchidoecalculo();
}
private void txtLargura_TextChanged(object sender, EventArgs e)
{
testePreenchidoecalculo();
}
private void txtProfundidade_TextChanged(object sender, EventArgs e)
{
testePreenchidoecalculo();
}
Its possible to make it better ?
I have no idea how do this.
I know only how do detect one key:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.C)
{
MessageBox.Show("C key");
}
}
You have to keep track of keydown/keyup events, and keep a list of all the keys that are currently "down". The keyboard handler can only trigger on individual keys, and it's up to your code to detect/keep track of which ones are down, and if those individual keydown events are close enough to each other to be counted as "together".
put a break point in your key down event and press your two keys together. examine the KeyData of the KeyEventArgs. it will show you what you have to use to detect two keys pressed together. Use some dummy code like this:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("KeyData is: " + e.KeyData.Tostring());
}
like I have done for shift and r pressed together
As you can see, you can use a timer event with booleans to detect if two keys are pressed:
bool keyup = false;
bool keyleft = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = true;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
keyup = false;
}
else if (e.KeyCode == Keys.Left)
{
keyleft = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (keyleft && keyup)
{
Console.Beep(234, 589);
}
}
Use this:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
string keysPressed = keyData.ToString();
// your code goes here
}
This is what I get for Up + Shift: "Up, Shift"
I have a standard WinForms-application. I want to implement such functionality:
user can press and hold only one keyboard button at a time. If he tried to press a button, while another button pressed, then it gets no result.
PS: this behavior spreads only to a form that I want, not to all forms of my application.
C#, 2.0 - 3.5, VS 2008
I got something similar than Khadaji
private Keys CurrentKey = Keys.None;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (CurrentKey == Keys.None)
{
CurrentKey = e.KeyData;
// TODO: put your key trigger here
}
else
{
e.SuppressKeyPress = true;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == CurrentKey)
{
// TODO: put you key end trigger here
CurrentKey = Keys.None;
}
else
{
e.SuppressKeyPress = true;
}
}
I banged this out pretty quickly, so you might have to tinker with it to make it work, but it should get you started.
Set your form's KeyPreview to true. Put the in a KeyDown event and KeyUp Event.
Keys MyKey;
bool KeyIsDown = false;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (KeyIsDown)
{
e.Handled = true;
}
else
{
MyKey = e.KeyData;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (KeyIsDown)
{
if (e.KeyData == MyKey)
{
KeyIsDown = false;
}
}
}
How do I capture the enter key in a windows forms combo box when the combobox is active?
I've tried to listen to KeyDown and KeyPress and I've created a subclass and overridden ProcessDialogKey, but nothing seems to work.
Any ideas?
/P
Hook up the KeyPress event to a method like this:
protected void myCombo_OnKeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
MessageBox.Show("Enter pressed", "Attention");
}
}
I've tested this in a WinForms application with VS2008 and it works.
If it isn't working for you, please post your code.
In case you define AcceptButton on your form, you cannot listen to Enter key in KeyDown/KeyUp/KeyPress.
In order to check for that, you need to override ProcessCmdKey on FORM:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if ((this.ActiveControl == myComboBox) && (keyData == Keys.Return)) {
MessageBox.Show("Combo Enter");
return true;
} else {
return base.ProcessCmdKey(ref msg, keyData);
}
}
In this example that would give you message box if you are on combo box and it works as before for all other controls.
or altertatively you can hook up the KeyDown event:
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show("Enter pressed.");
}
}
private void comboBox1_KeyDown( object sender, EventArgs e )
{
if( e.KeyCode == Keys.Enter )
{
// Do something here...
} else Application.DoEvents();
}
Try this:
protected override bool ProcessCmdKey(ref Message msg, Keys k)
{
if (k == Keys.Enter || k == Keys.Return)
{
this.Text = null;
return true;
}
return base.ProcessCmdKey(ref msg, k);
}
It could be that your dialog has a button that's eating the enter key because it's set to be the AcceptButton in the form property.
If that's the case then you solve this like this by unsetting the AcceptButton property when the control gets focus then resetting it back once the control loses focus ( in my code, button1 is the accept button )
private void comboBox1_Enter(object sender, EventArgs e)
{
this.AcceptButton = null;
}
private void comboBox1_Leave(object sender, EventArgs e)
{
this.AcceptButton = button1;
}
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
MessageBox.Show("Hello");
}
}
I have to admit not liking my own solution as it seems a little hacky to unset/set the AcceptButton property so if anyone has a better solution then I'd be interested
protected void Form_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13) // or Key.Enter or Key.Return
{
MessageBox.Show("Enter pressed", "KeyPress Event");
}
}
Don't forget to set KeyPreview to true on the form.