In EF4, I want to show a confirmation message when user changes a particular column, saying something like "Are you sure you want to change it?", Yes/No. I want to accept this change only if user presses Yes and revert it back to original value if he presses No. I'm trying to do this in the partial method On*Column*Changing (such as OnStatusChanging) generated by EF. The method provides me the new incoming value, but I don't know how to "cancel" it, something equivalent to e.Handled = true, or e.Cancel = true.
BTW, I'm following this approach for validations/confirmations.
Related
I want to make a basic game using Windows Forms.
I want to end the turn of a player when I press Space on the keyboard. I've tried using getch(), but it isn't a good solution because the character has to be entered in the debug window, as it isn't constantly polling for a key press.
A basic pseudocode example of what I need is something like this:
if(PlayerPressesSpace)
{
//end turn of player (I've got this bit figured out)
}
In a Windows Forms GUI app, each Form, and every child Control inside of it, has its own KeyDown, KeyUp, and KeyPress events that you can assign individual handlers to as needed.
Or, you can handle all key events within an entire Form instead of on a per-Control basis, by setting the Form's KeyPreview property to true and then using the Form's KeyDown event. See How to handle keyboard input messages in the form (Windows Forms .NET) for more details.
In a WinForms Form, you can override ProcessCmdKey of the Form container, to pre-process Key presses before they're dispatched to the destination. See also the Docs about this.
This allows to intercept keys that are used to activate default action, as the TAB Key, used to move the Focus to the next Control in the TabIndex hierarchy.
You can then perform any action required before the default action is performed, or suppress the Key, returning true when the conditions you have set are met.
It can also be used to generate special actions when combinations of Keys are pressed.
protected:
virtual bool ProcessCmdKey(Message %msg, Keys keyData) override {
if (keyData == Keys::Space) {
/* do something */
// Suppress the Key press, the message is not sent to the destination recipient
return true;
}
// Otherwise, let the message pass through
return Form::ProcessCmdKey(msg, keyData);
}
If you just need to intercept Keys that don't have special uses, as the aforementioned TAB Key, or some other Key that is processed by specific Controls for their internal use - so the Parent may or may not be notified - you can set the KeyPreview property of your Form container to true and handle the KeyDown event, as mentioned in Remy Lebeau answer.
When KeyPreview is set to true, the Form class sets a state, formState[FormStateKeyPreview]; its ProcessKeyPreview override checks this state and behaves as the ProcessCmdKey override in most cases.
I can only think of
if(Console::ReadKey(true).KeyChar == ' ') {
...
}
Console.ReadKey Method Documentation
I need to allow or stop an event from happening, based on a given condition such as a pop up box with options YES, NO and CANCEL.
I need to notify the user saying that there are unsaved data and if the user wishes to SAVE it, IGNORE it or CANCEL his current action(event such as Selection-change or Click).
I tried to use createInterceptor() function. But could not achieve the functionality.
Can anybody give me some suggestions with example? Basically I want to know how to stop an event.
Thanks..
Edited
I like the idea of using beforeXXX events. But I am still facing problems. As I mentioned, I need to ask the user if he wishes to save the unsaved data, which is a popup message box (With options YES, NO and CANCEL) that runs asynchronously. So by the time I get a reply, the event will have happened.
For ex. lets imagine a situation where, there is a page that displays a list of records in a grid with a pagination toolbar attached to it on the bottom(with a page size of 10 and total number pages is 10. So totally 100 records) on the left hand side. If you select a record in the list, the details are shown in a detail view on the right hand side.
Now,
I select third record in the list and make some changes to it in the detail view(form).
Without saving the record, I click on next page button on the Pagination toolbar.
It will show a confirm box from the beforeXXX event of Pagination toolbar, but the event will have happened anyway.
Here if the user clicks on CANCEL, I will have to restore the previous state which is already gone. Somehow I will have to go back and select the third record in the list of previous page.
So in order to resolve this problem, if I return false from my beforeXXX event, the next XXX event will be not be triggered.
But if the user clicks on YES or NO options I will have to trigger the event XXX manually which I am unable to do it for a selection-change event as of now.
Like this there can be many operations like list-filtering, searching, Ordering(A-Z/ Z-A), logout etc. For each of this operation I will have to write customised code which totally spoils the readability of the code.
So I was thinking if there is way to, somehow manually trigger the event XXX by holding the event object in beforeXXX...or is there any other way to restore the previous state.
Please give me suggestions....Thanks...
Many events have their "before-" counterpart, for example "beforeactivate". If you return false from this kind of the processing stops.
If not, or if your event does not have it's before- part, then you can use event object passed to all event handlers and call:
ev.stopEvent();
ev.stopPropagation();
return false;
at the end of your handler.
If users are going to be typing dates as well as choosing from the dropdown calendar widget, where is the best event to trap the entered value, whether it was typed or picked, then warn users if the date fails some validation, and finally rollback the edit value to where it was if the user decides not to override the warning?
We need to allow dates in the past, but want to prevent accidental dates in the past, which typically occurs in the first few months of the new year after users have been accustomed to typing, say, 2011 for the entire year and then when the year changes to 2012, they type 2011 out of habit. So this validation would only be in effect for the first few months of the year, not year-round.
I don't see how to rollback the value in EditValueChanged. The args don't have a cancel option there. Is there another better event to do this that works with typed values and picked values?
Use the EditValueChanging event. It does have a Cancel event, along with NewValue and OldValue.
I prefer to use Validating event of Control, where i get CancelEventArgs with which you can set the value as well as the focus on the control. e.Cancel will set the focus on control.
eg:
if (txtName.Text == "")
{
txtName.ErrorText = "Client Name should not be blank.";
e.Cancel = true;
}
I am having some trouble with my comboBox logic in my viewModel. The viewModel populates the comboBox and the user can select items.
When an item is selected, some editable info appears in the view and they can make changes. Now, I want to prompt the user to save if changes were made and not saved and they are trying to select another item in the drop down (a "Save?" yes/no/cancel messageBox).
Right now I need to remember the previously selected item and handle this myself by resetting the selected item if the user selects cancel. Since the comboBox does not have a PreviewSelectionChanged event, this is the only way I can think of of handling this scenario. It works but it gets a little messy hwen there are multiple comboBoxes, etc.
Am I missing anything or is this just the way it needs to be done?
You essentially have to make a flag in your view model called isDirty or something along those lines.
public class EditorViewModel
{
private bool _isDirty = false;
private long _editableProperty;
public long EditableProperty
{
get { return _editableProperty; }
set
{
_editableProperty = value;
// We've detected a change so mark this view model as dirty.
_isDirty = true;
}
}
}
Note that you will have to have to jump through a few more hoops if you want ensure that the data is in fact different from your original. So say someone accidently adds a space in EditableProperty and removes it your view model will think it's dirty and prompt the user.
The Windows Forms ComboBox provided a SelectionChangeCommitted event, but for the WPF ComboBox control you correct in that there is no event that will notify before the selection change occurs that will provide you with a means of cancelling the event.
If you are going to take a change tracking/editable approach, I would recommend considering implementing IChangeTracking and IEditableObject on the items in your combobox items source.
You will probably have to handle the SelectionChanged event, inspect the removed items to determine if the item that was previously selected was modified and then display a dialog requesting confirmation. If no/cancel was indicated, you can then set the selected index back to that of the previously selected item.
What about making the Editable item a copy of an item instead of the actual item?
So your ViewModel would contain
ObservableCollection<MyModel> ComboBoxItems;
int SelectedComboBoxIndex;
MyModel EditingItem;
Whenever the PropertyChange event occurs on SelectedComboBoxIndex, you check and see if EditingItem is null or not. If it is null, it means you're safe to switch and you set
EditingItem = ComboBoxItem[SelectedComboBoxIndex]).Copy();
If the EditingItem is not null, then you throw up a prompt asking if the user wants to save changes or cancel.
When the user hits Save, it takes the EditingItem and applies the changes to the data store and updates the item in the ComboBoxItems list.
If they hit Cancel, the EditingItem is simply discarded.
For a certain inputform, I'd like to make it possible to do input with the keyboard. I know how to read the keys through KeyPressed and KeyUp, but the problem is that when a control has got the focus and the user presses the Enter key, that control receives the a Click event. Is it possible to prevent that behaviour ? Or is it possible to know if a Click Event was fired by the mouse or by the keyboard ?
Does this help? From Microsoft Knowledge Base
Move the Button's code from the button.Click() to a button.MouseClick()
This would be easier if you could describe the situation and exact behaviour you want... :)
You can set:
Form.KeyPreview = True
This sends Key Events to the Form first, and then to the Control. This gives you the opportunity to catch Key Events on the form and 'cancel' them:
e.Handled = True
More info
Also make sure you haven't set the AcceptButton for the Form!
You can also listen for keyboard events and filter out keys.