is there a way to disable the validation of errorprovider elegantly when click cancel button to dismiss a winform?
The validation always happens when the textbox lose focus, and i don't wanna it to validate when the user click cancel button, it is just a little bit silly to validate when the user clicking cancel.
after googling, found the answer, just set CauseValidation property of the cancel button to false. that's it.
I just ran into this myself and setting CauseValidation = false is only a partial solution.
When you set the Form.CancelButton to the cancel button, the Escape key is supposed to invoke that button. It does, however, validation still runs in response to the Escape key, even though we set CauseValidation = false.
To fix it, add the following hack:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// Although we set CausesValidation = false for the Cancel button,
// the Escape key fails to cancel due to validation failure. The
// Form.CancelButton property should invoke the validation-free
// cancel button, but doesn't. Force the issue here.
if (keyData == Keys.Escape)
{
DialogResult = DialogResult.Cancel;
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Related
This is the mock-up of a child window that I have in a Windows Forms application.
This will be shown when a button is clicked on the parent window.
When the child form opens up, the focus is in Text box1.
The Leave event on the Text box1 fires when the user tabs out of it or clicks on any button.
This triggers a validation:
if(String.IsNullOrEmpty(textBox1.Text)
{
MessageBox.Show("Please enter a value of Id.");
}
else {
... other logic omitted for brevity
}
Now the user can also click on the Cancel button to exit the form without doing any action.
Here, on the first try, the Leave event fires because the focus is in Text box1 when the Form is loaded so when the user clicks on Cancel for the first time the form does not close, instead the message box with validation message is shown.
On the second click, since the focus is no longer on Text box1, the Leave event does not fire and the form closes.
I can put the focus on any other control to handle this but for the sake of user experience I had to put the focus on Textbox1 since it's value decides the further logic of what is shown in Textbox2(Textbox1 can be left empty in which case nothing is shown in Textbox2 but that is not related to this question).
Tried this to see if I can get the Cancel button inside the Leave event of Textbox1:
Button b = sender as Button
b is NULL when Cancel is clicked.
What can I do to make the Cancel click work, without triggering the Leave event initially?
Thanks in advance,
Regards.
If you only want to supress the error message when the cancel button recieves the focus, simply check for the cancelButton.Focused flag in the leave event, then block the message if the cancel button got the focus.
Nevertheless, it is usually considered good practice to avoid modal dialog boxes if they are not required. So depending on your particular situation, you may decide to replace the MessageBox with a ToolTip instead, which will not entirely block the UI thread.
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.
Is there a way I can cancel editing on my form without adding a cancel button?
I am looking for a way to call CancelButton.PerformClick() when I don't have a cancel button.
In the form code I have added the following - but need to know how to cancel the editing.
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if(keyData == Keys.Escape) {
// What goes here?
}
return base.ProcessCmdKey(ref msg, keyData);
}
The form contains text controls , and a BindingNavigator
It turns out that I can accomplish what I need using
navigator.BindingSource.CancelEdit();
however I wondered it there way of calling something like
form.CancelButton.PerformClick() if the form.CancelButton is not set.
Is there a way I can cancel editing on my form without adding a cancel button?
I am looking for a way to call CancelButton.PerformClick() when I don't have a cancel button.
In a word, no. There is no cancel button unless you've created one. The Form.CancelButton property will not specify any object unless you've assigned an object to it.
However, there may be a way to achieve what you seek to accomplish. Think about this: what would happen if you did have a cancel button? What would that cancel button do?
It would close the form, discarding all of the user's changes, right? So why not just close the form? Something like:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if(keyData == Keys.Escape) {
this.Close();
}
return base.ProcessCmdKey(ref msg, keyData);
}
If you need to do more clean-up on cancellation (e.g., whatever you would normally put in the cancel button's Click event handler method), you could just define a form class-level method called something like CancelForm, and then call that. For example:
private void CancelForm() {
// do cleanup
this.Close();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if(keyData == Keys.Escape) {
this.CancelForm();
}
return base.ProcessCmdKey(ref msg, keyData);
}
Then again, I question your motivation for wanting to do this. If you want to support cancellation, why not just add a cancel button? Why does the user need to memorize keyboard commands? Keyboard commands are great for users who want to use them, but there's a reason that GUIs are so popular. If the user should be able to cancel the operation, give 'em a big ol' cancel button for them to click. Be sure to disable it when cancelling is not available, giving instant visual feedback.
As a bonus, when you set the Form.CancelButton property to a cancel button on your form, the Esc key automatically invokes the cancel button, freeing you from overriding ProcessCmdKey. You seem to be trying to solve the problem backwards. WinForms takes care of all this for you automagically when you follow the standard idioms. And there are some really good reasons for following the standard idioms.
I am new to C#, I am creating a application in which there is a need of using two forms
one is Mainform and other is DialogForm.
The DialogForm has two buttons btnYes and btnNo.
whenever the user clicks the close button, FormClosing Event invokes in which I am calling the DialogForm as shown below:
DialogForm ex = new DialogForm();
ex.ShowDialog(this);
Now I want to give e.cancel=false for btnYes and e.cancel=true for btnNo. (this explained by my sir, only basics)
I know how to give functions to a Button which is in same Form but I dont know how to if the Form is different.
I have gone through some links but as I am new to c#, I can't understand it.
If you atleast provide me some links that would be appreciable.
Thanks in advance.
Forms have a property DialogResult. You can set it in the button event handlers.
DialogResult = DialogResult.Yes;
// or
DialogResult = DialogResult.No;
Then you can call the form like this
if (ex.ShowDialog(this) == DialogResult.Yes) {
// Do something
} else {
// Do something else
}
You can also set the CancelButton property of the form in the properties window. Microsoft says:
Gets or sets the button control that is clicked when the user presses the ESC key.
The form has also an AcceptButton property. Microsoft says:
Gets or sets the button on the form that is clicked when the user presses the ENTER key.
I'm trying to get a function to execute when a checkbox is checked or unchecked to verify all checkboxes are unchecked but it never gets executed. I assume I'm not doing it correctly.
#Html.CheckBox("Subscription", new{ data_bind="disable: Disabled, checked: Checked, click: $parent.allSubscriptionsUnchecked"} )
You can add both a checked and click binding to an input. However, you would want to return true; from the click handler. This will allow the default action to proceed (the checkbox will be checked/unchecked).
Here is a sample: http://jsfiddle.net/rniemeyer/cnkVA/
An alternative technique is to push this logic into your view model and subscribe to changes to a boolean observable and execute your action like: http://jsfiddle.net/rniemeyer/cnkVA/2/