I have a DataGridView that the user can search text in.
When the search text box has focus, I'm setting the Search button as the default button, so the user just has to hit the enter key to search. Here's the code
private void txtSearchCatsGrid_KeyUp(Object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
SearchCatsDgV();
}
}
I don't have a default button set for the form. There really isn't a button on the form that I want as the default.
When a search is performed and the result is no records found, I'm opening a message box saying that. The box has just an "OK" button.
The problem is, if the message box opens after a search with no results, if you hit enter, it appears that the search button retains focus and the search re-runs and the message box opens/stays open.
So I guess my question is how do I do the "opposite" of the above code. When the message box is open, obviously I want the Okay button to be the default when it is open and the Enter key is pressed. How do I do that?
Related
I have a form with 2 textboxes. The first one has a customsource for its autocompletion set like this :
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = GetUserNames();
The GetUserNames() method retuns an AutoCompleteCustomSource and all this works very well.
When this form opens the focus in on the first textbox, the user can type in or choose from the autocomplete and that works well.
Both textboxes have an onKeyDown event and in that event they should do some validations using the values of both textboxes.
I want to keydown to only do its validations when ENTER is pressed when the autocomplete listbox is closed.
Look at it this way:
the user starts typing, a list appears, the user chooses an item from the list and presses enter to confirm his choice, and then he wants to press TAB to go to the next textbox.
But when he presses ENTER after choosing an item in the autocomplete list the keyDown event already fires. At this stage the keydown event should not fire, the ENTER should only confirm the choice from the autocomplete list.
Is there a way to detect in the keydown that ENTER was pressed while the autocomplete list was still open ?
Or is there a way to disable the keydown event while the autocomplete list is open ?
EDIT:
from the comments I tried the answer in this link https://stackoverflow.com/a/40915048/3110834
Unfortunate it does not works in this case but it has teached me that pressing enter on the autocomplete suggestion does 2 things:
close the autocomplete window
fire the keydown event of the textbox
So I need to find a way to stop the keydown event for the textbox to fire when pressing enter on the autocomplete window.
Edit:
Things are far worse than I thought.
When you open the autosuggest box and then click on a suggested item to select it, the keydown event also fires and it has Keys.Enter in its KeyCode ! Since when is a click equal to a keystroke ?
How do I stop this ? Is this a bug ?
I ran into this same problem, I followed the approach in this link https://stackoverflow.com/a/40915048/3110834
if you try to evaluate if there is a list open during the Keydown event, it will always return a false, since the event already closed the list.
instead I observe the PreviewKeyDown event to evaluate if the AutoComplete list is open or not, if it is Open, i unsubscribe from the KeyDown event (which only uses the Enter key on my case) and if the list is close, i re-subscribe to it back again
private void tbxAND_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (IsAutoCompleteOpen())
{
tbxAND.KeyDown -= tbxAND_KeyDown;
}
else
{
tbxAND.KeyDown += tbxAND_KeyDown;
}
}
}
notice that IsAutoCompleteOpen() starts the code to enumerate the openlist.
this may not be the full solution for this particular case if you still need to catch other keys, but i wanted to leave the hint in case someone else run into this problem.
I want to set focus on a button while caret is present in textbox and user can still enter data to textbox. But when user presses enter key, button press will be simulated.
I am currently using a work around to solve this problem by handling onKeyDown event and checking for enter key. But problem is there is no clue for user to understand this as there is not blue border around the button that indicates focus on button.
Here is a example of what I want to implement (user can enter text in textbox while focus is on :
I have tried to search on google and StackOverflow but could not find any relevant result.
This is a fundamental Windows principle. It's not possible to have 2 controls (windows) focused at the same time.
So the focus should be inside the text box. But you can get the visual indication needed by setting the ok button as AcceptButton of the form (you might also want to set cancel button as CancelButton).
In the form constructor, load event or using designer:
this.AcceptButton = okButton;
There is no need to handle KeyDown event - as soon as the text box is not multiline, pressing Enter while the focus is inside it will generate ok button click. The same applies for the button set as CancelButton when you press ESC.
I am implementing a WinForms form in a C# project.
My form is a child of a MDI form.
My form contains a user control.
My user control contains some buttons including a validation button and a cancel one.
I want to implement the following logic :
When my form is active and the user presses the enter key then I want the validation button clicked event to be fired automatically.
When my form is active and the user presses the escape key then I want the cancel button clicked event to be fired automatically.
If my validation and my cancel buttons were not included in a user control then I would probably set the AcceptButton and CancelButton properties of my form.
Here is the code I have written in the Load event handler of my user control according to a tip given by Arthur in a comment to my first post :
// Get the container form.
form = this.FindForm();
// Simulate a click on the validation button
// when the ENTER key is pressed from the container form.
form.AcceptButton = this.cmdValider;
// Simulate a click on the cancel button
// when the ESC key is pressed from the container form.
form.CancelButton = this.cmdAnnulerEffacer;
Set the KeyPreview Property of your from true from properties;
Add keyDownEvent to your Form
In keyDownEvent of your Form, include following lines of code
The code
if(e.KeyValue==13)// When Enter Key is Pressed
{
// Last line is performing click. Other lines are making sure
// that user is not writing in a Text box
Control ct = userControl1 as Control;
ContainerControl cc = ct as ContainerControl;
if (!(cc.ActiveControl is TextBox))
validationButton.PerformClick(); // Code line to performClick
}
if(e.KeyValue==27) // When Escape Key is Pressed
{
// Last line is performing click. Other lines are making sure
// that user is not writing in a Text box
Control ct = userControl1 as Control;
ContainerControl cc = ct as ContainerControl;
if (!(cc.ActiveControl is TextBox))
cancelButton.PerformClick(); // Code line to performClick
}
validationButton or cancelButton are the names of your buttons which I am just supposing. You may have different ones. Use Your names instead of these two if you have different.
I am using EditorGridPanel with cellEditor which acts nearly like Excel. We implemented Arrow Keys to move among rows and columns.While validating a row, if it does not match the validation rule we show a MessageBox and hope that focus does not move if it is not valid. But, after the MessageBox shows - focus moves to the next row/column. Another problem is, user have to click in the OK button of the MessageBox to remove that from screen. Can we have the focus on the invalid column editor and also focus on the MessageBox OK button - so that user can press Enter to hide the message and continue entry?
Please check our link. http://www.softworksbd.com/swazilandlmis/yyyy_stockdata.php
Only one element can be focused, so focus should go to the message box. AFAIK enter/escape keys works out of box with message box.
You can pass callback to the messagebox. In this callback you can focus desired cell in the grid. Like this:
Ext.Msg.alert('Title', 'Message.', function(){
this.startEditing(rowIndex, colIndex);
}.createDelegate(grid));
So after message box is closed, the focus will go back to the editor.
I Have a WPF window that have a RadGridView and a Button,
My requirement is, when you click on the button and
select a row in the grid, it should work similar to
pressing on the shift key and clicking on a gridview row.
(To select multiple rows).
So, i need to press the shift key programatically in button
click event.
Can this be done?
Appreciate if anyone can provide a solution
Do not fire a KeyPress event manually. Rather, on this page, Telerik indicates that to allow multiple selection, you must set the SelectionMode to Extended:
this.radGridView.SelectionMode = System.Windows.Controls.SelectionMode.Extended;
So, put this in your button click (I guess you'll want it to toggle between Extended and Single. Multiple's behaviour is awkward, but that's just me) and you're good to go.
Edit:
The toggle button will have something like this in its click event handler:
private void toggleButton1_Click(object sender, RoutedEventArgs e)
{
this.radGridView.SelectionMode = toggleButton1.IsChecked ? SelectionMode.Extended : SelectionMode.Single;
}