I use this code on Enter keypress to move focus to next like the Tab does in a datagrid.
uiElement.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
At the end of any row while hitting enter moves the focus to the next line but the selection stays where it was. On the other hand using Tab brings the selection with the focus.
Is there a way to move the selection aswell with some adjustments?
Matt Hamilton's answer is great but not doing the selection.
What you could do, instead of trying to programmatically switch focus, is simulating a tab key press each time the enter key is pressed inside the DataGrid. It would then look something like this:
private void DataGrid_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var dataGrid = (DataGrid)sender;
var keyEventArgs = new KeyEventArgs(Keyboard.PrimaryDevice, PresentationSource.FromVisual(dataGrid), 0, Key.Tab)
{
RoutedEvent = Keyboard.KeyDownEvent,
};
dataGrid.RaiseEvent(keyEventArgs);
e.Handled = true;
}
}
Related
Im having troubles with Selecting All content inside of a TextBox.
Ussually by pressing enter I'm jumping from one textbox to another, because there are like 6-7 TextBoxes below each other
in my Grid, and by pressing enter I need to jump from one to another,
private void Grid_PreviewKeyDown_1(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
UIElement element = e.Source as UIElement;
element.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
//TextBox tb = (sender as TextBox);
//if (tb != null)
//{
// tb.SelectAll();
//}
}
}
And while I'm on some of them when I press Enter I'm doing some calculation like this:
private void txt2_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
try
{
CalculateSomethingFromOtherTextBoxes();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
My Question is next: When I jump from each other and when I finish calculation (enter is pressed), the next TextBox I will jump to I would like SELECTALL of TextBox's content when I jumped on it.
In case I want to edit some value or whatever, and it is confusing sometimes content insidee is selected and sometimes it is not.
I tried setting GotFocus event on each of TextBoxes and It looks like this:
private void txt3_GotFocus(object sender, RoutedEventArgs e)
{
txt3.SelectAll();
}
But unfortunately somehow this is sometimes working sometimes it is not, I mean all of content is selected sometimes and sometimes it is not..
Thanks guys
Cheers
Try to handle the GotKeyboardFocus event instead of the GotFocus event. This should work:
private void txt3_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
txt3.SelectAll();
}
There is no property that you can set to select all of the Text in a TextBlock or TextBox. Selecting all text must be accomplished using the TextBoxBase.SelectAll Method. What you could do in a Style is to set an event handler for the GotFocus event, where the handler code would call SelectAll, but your handler would of course need to be in code and not XAML.
One other possibility would be for you to create an Attached Property that would select the text whenever the TextBox gets focus, but again it's not possible to do this in XAML.
I have the typical Web Form with textbox, combobox and a buttonbox (which is the search button).
After push in button I have to fill the data in a table.
All of that controls are inside a FORM.
how can i detect if after push ENTER Key I want to simulate the search button?
Try this:
TextBox tb = new TextBox();
tb.KeyDown += new KeyEventHandler(tb_KeyDown);
private void tb_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter)
{
//enter key is down
}
}
I have a RichTextBox that i am searching text in and I want to be able to control what the enter key does when text is selected. I am able to use this if test below to call the method that I want, but my issue is after the method gets hit when the enter key is pressed it then moves the text to the second line and I want to be able to stop this from happening when the text is highlighted.
I test to check if the text is selected when enter is pressed.
if (IsTextSelected == true)
{
btnSearch_Click(sender, null);
}
You can listen to the PreviewKeyDown event like:
<RichTextBox PreviewKeyDown="RichTextBox_PreviewKeyDown"/>
and in the handler:
private void RichTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
// DO YOUR WORK HERE and then set e.Handled to true on condition if you want to stop going to next line//
e.Handled = true;
}
}
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.
I have a listbox which displays the contents of an array. The array is populated with a list of results when my "go" button is pressed.
The go button is set as the AcceptButton on the form properties so pressing the Enter key anywhere in the focus of the form re-runs the go button process.
Double clicking on a result from the array within the listbox works fine using below:
void ListBox1_DoubleClick(object sender, EventArgs e) {}
I would like to be able to use my arrow keys and enter keys to select and run an event without having to double click on the line within the listbox. (however go button runs each time instead)
Basically open the form, type search string, press enter to run go button, use up and down arrows then press enter on selection to run same event as double click above. Will need to change focus after each bit.
You can handle the KeyDown events for the controls you want to override. For example,
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//execute go button method
GoButtonMethod();
//or if it's an event handler (should be a method)
GoButton_Click(null,null);
}
}
That will perform the search. You can then focus your listbox
myListBox.Focus();
//you might need to select one value to allow arrow keys
myListBox.SelectedIndex = 0;
You can handle the Enter button in the ListBox the same way as the TextBox above and call the DoubleClick event.
This problem is similar to -
Pressing Enter Key will Add the Selected Item From ListBox to RichTextBox
Certain controls do not recognize some keys when they are pressed in Control::KeyDown event. For e.g. list box does not recognize if the key pressed is Enter key.
See the remarks section of the Control::KeyDown event reference.
One way to resolve your problem might be writing a method for the Control::PreviewKeyDown event for your list box control:
private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up && this.listBox1.SelectedIndex - 1 > -1)
{
//listBox1.SelectedIndex--;
}
if (e.KeyCode == Keys.Down && this.listBox1.SelectedIndex + 1 < this.listBox1.Items.Count)
{
//listBox1.SelectedIndex++;
}
if (e.KeyCode == Keys.Enter)
{
//Do your task here :)
}
}
private void listBox1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
e.IsInputKey = true;
break;
}
}