Do logic on keypress down of textbox - c#

I need to do some logic but only from a specific textbox. Trying to do some search it appears that there is no event for KeyDown or PreviewKeyUp for a textbox but for the entire window. So in XAML I have this
PreviewKeyUp="keyPressLogic"
Then have a method that looks like this;
private void keyPressLogic(object sender, KeyEventArgs e)
{
if ((e.Key == Key.Down) && (check focus command ) )
{
//My logic
return;
}
}
As you can see I cannot figure out the check focus command. So either I am missing the key check on the textbox or got to find the focus command
thanks

To get the textbox you pressed you should:
TextBox textbox = (TextBox)sender;
and then you can:
private void keyPressLogic(object sender, KeyEventArgs e)
{
if ((e.Key == Key.Down) && (textbox.IsFocused))
{
//My logic
return;
}
}

System.Windows.Controls.TextBox has an event caleed. Here you can find it.
It will trigger the KeyDown event only for the textbox you add it on.
Here is how you add it into your XAML
<TextBox x:Name="MyTextbox" KeyDown="MyTextbox_KeyDown" />
And here is how your event handler should look like
private void MyTextbox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if(e.Key == Key.Down)
{
// Add your logic here
}
}

Related

How to handle Ctrl + Enter with TextBox when AcceptsReturn is true?

In my UWP application, I want my TextBox to be able to go to new line by pressing down the Enter key but I also need to trigger an action when Ctrl+Enter are pressed.
The issue is, I can't seem to find a way to prevent the text to go to the next line when I press down Ctrl+Enter. Here is the code I have tried.
XAML
<TextBox x:Name="TextBox1" AcceptsReturn="True" />
In the constructor
TextBox1.AddHandler(KeyDownEvent, new KeyEventHandler(TextBox1_KeyDown), true);
Handler
private void TextBox1_KeyDown(object sender, KeyRoutedEventArgs e)
{
var ctrl = Window.Current.CoreWindow.GetKeyState(VirtualKey.Control);
if (ctrl.HasFlag(CoreVirtualKeyStates.Down) && e.Key == VirtualKey.Enter)
{
e.Handled = true;
}
}
You could create a custom class that inherits from TextBox and override its OnKeyDown method where you have full control of firing the base.OnKeyDown method to prevent from adding a new line.
class CTRLEnterTextBox : TextBox
{
protected override void OnKeyDown(KeyRoutedEventArgs e)
{
if (Window.Current.CoreWindow.GetKeyState(VirtualKey.Control).HasFlag(CoreVirtualKeyStates.Down) && e.Key == VirtualKey.Enter)
{
e.Handled = true;
}
else
{
base.OnKeyDown(e);
}
}
}

How to call event key_down only in specific situations in c#?

I have 2 combo boxes and one text box(combo1, combo2, textBox). Here is the code for event key_down:
private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
buttonSearch_Click(sender, e);
}
When I click button ENTER on keyboard I want that program call search button on form. The problem is when I select some item from combo box and click on ENTER to give me that item, he also call searh button, ofcource, but I dont want to call search until I fill both combo boxes and text box. So, I want to call search button ONLY when my focus is on text box. Any idea how to do that?
as said, you could put the event on the textbox. Also, going with your original problem, you could check if the textbox has focus:
private void MyForm_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (textBox1.Focused) // or whatever your textbox is called
{
buttonSearch_Click(sender, e);
}
}
}
You have specific events for each control. You are using the Form events but if you only want to have the keydown when the Textbox is focussed I suggest the following:
private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
buttonSearch_Click(sender,e);
}

TextBox KeyPress event doesn't fire

I have a Form with nine TextBox controls. Each one has a KeyPress event handler that fires on Enter/Return and more.
The fifth TextBox(Kategorie) and sixth (Ort) don't fire. The others do. The code is:
private void tb_Kategorie_KeyPress(object sender, KeyPressEventArgs e)
{
MessageBox.Show("works");
if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Return)
{
tb_Ort.Focus();
}
else if (e.KeyChar == (char)Keys.Escape)
{
tb_Kategorie.Text = escSpeicher;
tb_Kategorie.SelectAll();
}
}
The event handler is set in the Designer and in designer.cs. The button and the code are not copy/pasted. Can someone tell me where the problem is?
// You Can Use Key Down method here
private void tb_Kategorie_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show("works");
if (e.KeyCode== Keys.Enter || e.KeyCode== Keys.Return)
{
tb_Ort.Focus();
}
else if (e.KeyCode== Keys.Escape)
{
tb_Kategorie.Text = escSpeicher;
tb_Kategorie.SelectAll();
}
}
// try this
Try to set the Form.KeyPreview property to True
It is something with the Autocomplete Source.
This is the same problem like in
Autocomplete on Combobox onkeypress event eats up the Enter key
It is not really solved, but a nice workaround

C# - How to override actions for "Up arrow" and "Down arrow" for a textbox?

I have a textbox and below it i have a listbox.
While the user is typing in the textbox if he presses the up or down arrow he should make a selection in the listbox. The textbox detects all the characters (except space) but it seems that it can't detect the arrow presses.
Any solution for this? This is a WPF project btw.
EDIT, Here's the working code thanks to T.Kiley:
private void searchBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.IsDown && e.Key == Key.Down)
{
e.Handled = true;
//do your action here
}
if (e.IsDown && e.Key == Key.Up)
{
e.Handled = true;
//do another action here
}
}
I just tried this and it works. Add a preview key down event to the textbox
private void TextBox_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.IsDown && e.Key == Key.Down)
MessageBox.Show("It works");
}
You can listen to they KeyDown event of the TextBox. In the handler, check whether the arrow key was pressed (you might need to listen to key up to avoid triggering your code multiple times if the user holds down the button for too long).
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
// Do some code...
}
}

Fire button event manually

I have a search text box on my WPF Windows. Whenever, the user presses enter key after writing some query in the textBox, the process should start.
Now, I also have this Search button, in the event of which I perform all this process.
So, for a texBox:
<TextBox x:Name="textBox1" Text="Query here" FontSize="20" Padding="5" Width="580" KeyDown="textBox1_KeyDown"></TextBox>
private void queryText_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
//how do I fire the button event from here?
}
}
It is possible but rather move your search logic into a method such as DoSearch and call it from both locations (text box and the search button).
Are you talking about manually invoking buttonSearch_onClick(this, null);?
You can Create a Common Method to do search for ex
public void MySearch()
{
//Logic
}
Then Call it for diffetnt places you want like...
private void queryText_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
MySearch();
}
}
private void buttonSearch_onClick(object sender, EventArgs e)
{
MySearch();
}
A event can not be fired/raised from outside the class in which it is defined.(Using reflection you can definitely do that, but not a good practice.)
Having said that, you can always call the button click event handler from the code as simple method call like below
private void queryText_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
OnSearchButtonClick(SearchButton, null);
}
}

Categories