Prevent DataGrid from handling Enter-key - c#

I have a readonly DataGrid showing items inside a window that is shown as dialog. This dialog also has two buttons, one to confirm and one to cancel the dialog. The confirm button's IsDefault property is set to true, the cancel button's IsCancel property is set to true. I want to keep up the default dialog button behaviour, i.e. confirming and closing the dialog when pressing the confirm button or ENTER or cancelling and closing it by pressing the cancel button or ESCAPE.
Unfortunately the DataGrid consumes the KeyDown event of the ENTER-key and selects the next row inside the grid. Because the KeyDown event is marked as handled by the DataGrid the dialog will not be confirmed and stays open. My desired behaviour is that the selected row is not changed and the dialog will be confirmed and closed.
I could create my own DataGrid-derived type an overwrite OnKeyDown as follows:
public class MyDataGrid : DataGrid
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key != Key.Enter)
{
base.OnKeyDown(e);
}
}
}
But I was wondering if there is another way without having to create a derived type to achieve my goal.

You can use the curernt DataGrid.
<DataGrid PreviewKeyDown="DataGrid_PreviewKeyDown"/>
private void DataGrid_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// This makes this key pressed event end here.
e.Handled = true;
// Do what you need to do here...
}
}

Related

How to make Enter Key as Default key for Tab Changing for all the textbox in a Window Form C#?

I am using c# window form application. I have already tried many example on the internet for this query but nothing works for me. I just want to replace the action of Enter key with Tab key on a Window Forms. I don't want to apply keydown or up event on one by one on my textbox. I just want a single event which I can apply on my window form.
Thanks.
It works fine now I used these code of lines on form keyup event, and set the KeyPreview property True.
if (e.KeyData == System.Windows.Forms.Keys.Enter)
{
SendKeys.Send("{TAB}");
}
Create your own custom TextBox:
public class MyTextBox : TextBox
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Parent.SelectNextControl(this, true, true, true, true);
e.Handled = true;
e.SuppressKeyPress = true; // suppress Ding sound
}
base.OnKeyDown(e);
}
}
Compile the code. This component will appear in the toolbar. Use it instead of the regular textboxes.

bindingNavigator _Validating event not returning control to the form

I am trying to follow the MSDN example code for the Control.Validating Event and to apply it to a bindingNavigator item instead of the textbox as in the example.
The code provided for the textbox validation event is shown below.
private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e)
{
string errorMsg;
if(!ValidEmailAddress(textBox1.Text, out errorMsg))
{
// Cancel the event and select the text to be corrected by the user.
e.Cancel = true;
textBox1.Select(0, textBox1.Text.Length);
// Set the ErrorProvider error with the text to display.
this.errorProvider1.SetError(textBox1, errorMsg);
}
}
I too intend on validating texboxes however I have several and have already written the validation methods for them which handle the error messages. I would like these methods to be tested when the user selects an item (Arrow buttons / Save button / Add button) on the bindingNavidator.
I therefore have the code below to attempt this,
private void myBindingNavigator_Validating(object sender, CancelEventArgs e)
{
if (!IsPostCodeValid())
{
PostCodeTextBox.Focus();
e.Cancel = true;
}
}
I have the ItemClicked event for the bindingNavigator set to bindingNavigator.Focus() with the hope of initiating the Validating event. (myBindingNavigator.CausesValidation = true; has been declared on formLoad). However I feel this is where an infinite loop of focusing the Navigation bar follows? I have stepped through and no code is executed once it has locked on to the Navigation bar it simply won't let the user interact with the rest of the form to change and correct the error after the navigation bar has been locked onto.
I can provide any extra information required and test any suggestions to discover what is going on.
Thanks
When you work with BindingNavigator and you have put your controls in details mode on the form, to ensure save only valid data, you should write validation rules for your controls and also you should handle your binding navigator items yourself.
This way you even don't need to set AutoValidate Property of your form to annoying EnablePreventFocusChange and you can set it to be the friendly EnableAllowFocusChange and because you can't navigate or save anything when there is validation errors, then you can be sure only valid data will save in database.
To do so, follow these steps:
Step 1
Handle Validating event of child controls and set e.cancel = true when the value is not valid.
private void nameTextBox_Validating(object sender, CancelEventArgs e)
{
//Put validation logic here
if (this.nameTextBox.Text.Length < 10)
{
e.Cancel = true;
this.errorProvider1.SetError(this.nameTextBox, "Some Error");
}
else
{
this.errorProvider1.SetError(this.nameTextBox, "");
}
}
Step 2
Go to BindingNavigator properties, and set MoveFirstItem, MovePreviousItem, MoveNextItem, MoveLastItem, AddNewItem, DeleteItem properties To (none). Also from designer click on the text box that shows record number, it is bindingNavigatorPositionItem, and then set its ReadOnly property to true.
Step 3
For all buttons, including navigation buttons, add button, delete button, save button and other custom buttons, handle Click event and call ValidateChildren method of the container of your text boxes and check if ValidateChildren() doesn't return true, exit the method, else do the task that the method should do, for example:
private void toolStripButton1_Click(object sender, EventArgs e)
{
if (!this.ValidateChildren())
return;
//Put the logic for this button here
//MoveFirstItem: `this.myBindingSource.MoveFirst();`
//MovePreviousItem: `this.myBindingSource.MovePrevious();`
//MoveNextItem: `this.myBindingSource.MoveNext();`
//MoveLastItem: `this.myBindingSource.MoveLast();`
//AddNewItem: `this.myBindingSource.AddNew();`
//DeleteItem: `this.myBindingSource.RemoveCurrent();`
}

Combobox SelectionChangeCommitted event doesn't work with arrow keys

I have a combobox with the the property DropDownList that runs some code on SelectionChangeCommitted event. It works fine with mouse-click but if I try to select using arrow keys it fires after the first key press. How do I get this to work as a standard drop-down that I can navigate with mouse and keys?
private void dd_jobs_SelectionChangeCommitted(object sender, EventArgs e)
{
Pk_Error p = new Pk_Error(ref_num, j[dd_jobs.SelectedIndex]);
p.Show(); p.BringToFront();
this.Close();
}
I had the same issue to adhere to 508 compliance. I had to implement a combobox that inherits from the windows one and override the functionality.
For you capture the selected text in the OnEnter call and use that during the OnLeave to make the change. Override the OnSelectionChangeCommitted to check if the list is dropped and not make a selection.
if(this.DroppedDown == true)
{
base.OnSelectionChangeCommitted (e);
sCurrentItem = this.Text;
}

Set focus to element

There are a button and a textbox. I added a "KeyDown" event to textbox so that when "enter" is pressed button gets clicked. Good, then I tried to give focus to textbox again but failed. In the code below I tried three ways but neither is working.
private void txt_addRemove_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
btn_BC_add.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
// 1.way
// IInputElement focusedElement = FocusManager.GetFocusedElement(txt_addRemove);
// 2.way
// Keyboard.Focus(txt_addRemove);
// 3.way
// txt_addRemove.Focus();
}
}
Add this
if (!textBox1.Focus())
{
textBox1.Focus();
}
What this does:
1. We check if the textbox is NOT focused.
2. If it is not focused, focus the control.
EDIT: How about this:
btn_BC_add.PerformClick()
Try focusing the textbox after the event handler is finished using:
Dispatcher.Invoke(() => { txt_addRemove.Focus(); })
couldnt you also try changing the focus from the button event handler? this might not be desirable if you dont want the focus to be on your textbox after a normal click of the button, but it should work.
Im guessing that your button click is generating a post back before the focus can be changed

How to skip Validating after clicking on a Form's Cancel button

I use C#.
I have a Windows Form with an edit box and a Cancel button. The edit box has code in validating event. The code is executed every time the edit box loses focus. When I click on the Cancel button I just want to close the form. I don't want any validation for the edit box to be executed. How can this be accomplished?
Here is an important detail: if the validation fails, then
e.Cancel = true;
prevents from leaving the control.
But when a user clicks Cancel button, then the form should be closed no matter what. how can this be implemented?
If the validation occurs when the edit box loses focus, nothing about the the cancel button is going to stop that from happening.
However, if the failing validation is preventing the cancel button from doing its thing, set the CausesValidation property of the button to false.
Reference: Button.CausesValidation property
Obviously CausesValidation property of the button has to be set to false and then the validating event will never happen on its click. But this can fail if the parent control of the button has its CausesValidation Property set to true. Most of the time developers misses/forgets to change the CausesValidation property of the container control (like the panel control). Set that also to False. And that should do the trick.
I was having problems getting my form to close, since the validation of certain controls was stopping it. I had set the control.CausesValidation = false for the cancel button and all the parents of the cancel button. But still was having problems.
It seemed that if the user was in the middle of editing a field that was using validation and just decided to give up (leaving the field with an invalid input), the cancel button event was being fired but the window would not close down.
This was fixed by the following in the cancel button click event:
private void btnCancel_Click(object sender, EventArgs e)
{
// Stop the validation of any controls so the form can close.
AutoValidate = AutoValidate.Disable;
Close();
}
Set the CausesValidation property of the Cancel button to false.
Set the CausesValidation property to false.
None of these answers quite did the job, but the last answer from this thread does. Basically, you need to:
Insure that the Cancel button (if any) has .CausesValidation set to false
Override this virtual method.
protected override bool ProcessDialogKey(Keys keyData) {
if (keyData == Keys.Escape) {
this.AutoValidate = AutoValidate.Disable;
CancelButton.PerformClick();
this.AutoValidate = AutoValidate.Inherit;
return true;
}
return base.ProcessDialogKey(keyData);
}
I didn't really answer this, just pointing to the two guys who actually did.
Setting CausesValidation to false is the key, however this alone is not enough. If the buttons parent has CausesValidation set to true, the validating event will still get called. In one of my cases I had a cancel button on a panel on a form, so I had to set CausesValidation = false on the panel as well as the form. In the end I did this programatically as it was simpler than going through all the forms...
Control control = cancelButton;
while(control != null)
{
control.CausesValidation = false;
control = control.Parent;
}
In my case, in the form I set the property AutoValidate to EnableAllowFocusChange
By using Visual Studio wizard you can do it like that:
Judicious use of the Control.CausesValidation property will help you achieve what you want.
Just above the validation code on the edit box add:
if (btnCancel.focused)
{
return;
}
That should do it.
In complement of the answer of Daniel Schaffer: if the validation occurs when the edit box loses focus, you can forbid the button to activate to bypass local validation and exit anyway.
public class UnselectableButton : Button
{
public UnselectableButton()
{
this.SetStyle(ControlStyles.Selectable, false);
}
}
or if you use DevExpress:
this.simpleButtonCancel.AllowFocus = false;
Note that doing so will change the keyboard experience: the tab will focus anymore on the cancel button.
Maybe you want to use BackgroundWorker to give little bit delay, so you can decide whether validation should run or not. Here's the example of avoiding validation on form closing.
// The flag
private bool _isClosing = false;
// Action that avoids validation
protected override void OnClosing(CancelEventArgs e) {
_isClosing = true;
base.OnClosing(e);
}
// Validated event handler
private void txtControlToValidate_Validated(object sender, EventArgs e) {
_isClosing = false;
var worker = new BackgroundWorker();
worker.DoWork += worker_DoWork;
worker.RunWorkerAsync();
worker.RunWorkerCompleted += worker_RunWorkerCompleted;
}
// Do validation on complete so you'll remain on same thread
void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) {
if (!_isClosing)
DoValidationHere();
}
// Give a delay, I'm not sure this is necessary cause I tried to remove the Thread.Sleep and it was still working fine.
void worker_DoWork(object sender, DoWorkEventArgs e) {
Thread.Sleep(100);
}
This is an old question however I recently ran into this issue and solved it this way:
1st, we are loading a UserControl into a 'shell' Form that has a save and cancel button. The UserControl inherit an interface (like IEditView) that has functions for Save, Cancel, Validate and ToggleValidate.
In the shell form we used the mouse enter and mouse leave like so:
private void utbCancel_MouseEnter(object sender, EventArgs e)
{
((Interface.IEdit)tlpMain.Controls[1]).ToggleValidate();
}
private void utbCancel_MouseLeave(object sender, EventArgs e)
{
((Interface.IEdit)tlpMain.Controls[1]).ToggleValidate();
}
Then in ToggleValidate (Say a simple form with two controls...you can always just loop through a list if you want) we set the CausesValidation
public bool ToggleValidate()
{
uneCalcValue.CausesValidation = !uneCalcValue.CausesValidation;
txtDescription.CausesValidation = !txtDescription.CausesValidation;
return txtDescription.CausesValidation;
}
Hope this helps.
I found this thread today while investigating why my form would not close when a validation error occurred.
I tried the CausesValidation = false on the close button and on the form itself (X to close).
Nothing was working with this complex form.
While reading through the comments I spotted one that appears to work perfectly
on the form close event , not the close button (so it will fire when X is clicked also)
This did the trick.
AutoValidate = AutoValidate.Disable;
Create a bool:
bool doOnce;
Set it to false in your function and then:
if (doOnce == false)
{
e.cancel = true;
doOnce = true;
}
This means it will only run once and you should be able to cancel it. This worked for me anyways.
This work for me.
private void btnCancelar_MouseMove(object sender, MouseEventArgs e)
{
foreach (Control item in Form.ActiveForm.Controls)
{
item.CausesValidation = false;
}
}

Categories

Resources