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

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.

Related

Focus selected controls by pressing Enter Key

In C# windows application to navigate all control of a Form (using Enter Key) I am using the below code:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == System.Windows.Forms.Keys.Enter)
{
SendKeys.Send("{TAB}");
}
}
N.B.: Form Property KeyPreview = True;
The above code works fine but when I am going to navigate between two dateTimePicker (dateTimePicker1, dateTimePicker2) pressing Enter Key.
When Form open Focus on dateTimePicker1 and press Enter Key then Focus dateTimePicker2 and press Enter Key Focus dateTimePicker1.
The below code works fine without the above code. What is the best way to navigate the two dateTimePicker using the above code or any other way?
private void dateTimePicker1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
dateTimePicker2.Focus();
}
}
private void dateTimePicker2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
dateTimePicker1.Focus();
}
}
Anybody please help me.
You can subscribe your two DateTimePickers to the same event handler instead of using two events, and use the sender object:
private void dateTimePicker_KeyDown(object sender, KeyEventArgs e)
{
var dtp = sender as DateTimePicker;
if (e.KeyCode == Keys.Enter)
{
if (dtp?.Name[dtp.Name.Length - 1] == '1')
dateTimePicker2.Focus();
else dateTimePicker1.Focus();
}
}
Just don't forget to change the value of the KeyDown event in the properties window of the both DateTimePickrs to point to this event.

Restrict keypress in wpf textbox

I have TextBox in WPF where i need to fill the box only by pasting (ctrl +v) not by typing. So i need to restrict entire key press except ctrl+v. Since WPF is not having keypress event i am facing the problem to restrict the keypress
Do it WPF style and use ApplicationCommands.Paste and make the textbox readonly.
you can add this Key_Down handler to the textBox:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Modifiers == Keys.Control && e.Key==Key.V)
{
//Logic here
}
else
e.handled=true;
}
Provided you don't allow Right Click + Paste, but only Ctrl + V, I would simply check for the Ctrl key modifier being pressed and prevent everything else.
So try this:
myTextBox.KeyDown += new KeyEventHandler(myTextBox_KeyDown);
private void myTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
{
input = myTextBox.Text;
}
else
{
input = "";
}
}
<TextBox IsReadOnly="True" Name="Policy_text">
<TextBox.CommandBindings>
<CommandBinding Command="ApplicationCommands.Paste" CanExecute="PasteCommand_CanExecute" Executed="PasteCommand_Executed" />
</TextBox.CommandBindings>
</Textbox>
and in code behind
private void PasteCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = Clipboard.ContainsText();
}
private void PasteCommand_Executed(object sender, ExecutedRoutedEventArgs e)
{
Policy_text.Paste();
}

Disable MaskedTextBox sound

I use a MaskedTextBox control to facilitate the entry of dates in my project. I have mtb.BeepOnError set to false. However, it makes a generic beep whenever the 'Enter' key or 'Esc' key is pressed, and this is undesirable for my application.
This seems to be the default behaviour for the MTB, so is there any way to change that?
You can try something like this:
void mtb_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter | e.KeyCode == Keys.Escape) {
e.SuppressKeyPress = true;
}
}

Detect Shift+Tab Function Into Backspace

Software Utilize : C#, VS-2005
Is This Possible to override Shift+Tab Function/Method or detect Shift+Tab Function and Utilize it with Backspace.?
In Shot replace Shift+Tab Function with Backspace. And Then Backspace will Behave like Shift+Tab:
Is this possible in C#?
I suppose you are working on a win-form. Register a key down event:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab && e.Shift)
{
// act like a backspace is pressed
}
else if (e.KeyCode == Keys.Back)
{
SendKeys.Send("+{TAB}"); // simualte a shift-tab press
}
}
To resolve the issue #liggett78 mentioned in the comment, you can set
form.KeyPreview = true;
to handle all the key events of child controls in the KeyDown event of the from.
EDIT: To prevent deleting a character in textbox when pressing BACKSPACE, you can:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back)
{
e.SuppressKeyPress = true;
SendKeys.Send("+{TAB}");
}
}
Override ProcessDialogKey or ProcessTabKey on your form.

How to capture delete key press in C#?

I want to capture delete key presses and do nothing when the key is pressed. How can I do that in WPF and Windows Forms?
When using MVVM with WPF you can capture keypressed in XAML using Input Bindings.
<ListView.InputBindings>
<KeyBinding Command="{Binding COMMANDTORUN}"
Key="KEYHERE" />
</ListView.InputBindings>
For WPF add a KeyDown handler:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
MessageBox.Show("delete pressed");
e.Handled = true;
}
}
Which is almost the same as for WinForms:
private void Window_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
MessageBox.Show("delete pressed");
e.Handled = true;
}
}
And don't forget to turn KeyPreview on.
If you want to prevent the keys default action being performed set e.Handled = true as shown above. It's the same in WinForms and WPF
I don't know about WPF, but try the KeyDown event instead of the KeyPress event for Winforms.
See the MSDN article on Control.KeyPress, specifically the phrase "The KeyPress event is not raised by noncharacter keys; however, the noncharacter keys do raise the KeyDown and KeyUp events."
Simply check the key_press or Key_Down event handler on the specific control and check like for WPF:
if (e.Key == Key.Delete)
{
e.Handle = false;
}
For Windows Forms:
if (e.KeyCode == Keys.Delete)
{
e.Handled = false;
}
I tried all the stuff mentioned above but nothing worked for me, so im posting what i actually did and worked, in the hopes of helping others with the same problem as me:
In the code-behind of the xaml file, add an event handler in the constructor:
using System;
using System.Windows;
using System.Windows.Input;
public partial class NewView : UserControl
{
public NewView()
{
this.RemoveHandler(KeyDownEvent, new KeyEventHandler(NewView_KeyDown));
// im not sure if the above line is needed (or if the GC takes care of it
// anyway) , im adding it just to be safe
this.AddHandler(KeyDownEvent, new KeyEventHandler(NewView_KeyDown), true);
InitializeComponent();
}
//....
private void NewView_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
//your logic
}
}
}

Categories

Resources