I want to cancel the Data gridview cell click event
private void gridViewHistory_CellClick(object sender, DataGridViewCellEventArgs e)
{
}
but this eventargs doesn't have cancel event, How to cancel that event ?
May be this question has a very easy answer but still i am stuck it in, needs help.
Thanks in advance.
You can't.
WinForms events are just multicast delegates then they can't be canceled (in the sense of stop propagation of the event) unless this situation is handled by the object that exposes the event (but I'm now aware of any object that supports this, usually events are notifications).
That said, some events has argument with a Cancel property, it's used by the object to cancel the action that should be performed because of that event. Again if the implementation does not provide that property there is not anything you can do to change this behavior.
In your case you should override the OnCellClick method of DataGridView to handle that in the way you prefer (if you do not call the base class then cell won't get the click event and CellClick event won't be fired).
Not all events can be cancelled. You can choose to not do anything in your event handler but that doesn't mean you can cancel it.
So, unless there is a specific Cancel type method you Cannot cancel the event.
You may want to use CellValidating event:
private void gridViewHistory_CellValidating(object sender,
DataGridViewCellValidatingEventArgs e)
{
if (.....)
{
e.Cancel = true;
}
}
You can try the
return;
statement, but this only works for asp:LinkButton OnClick events that do not then pass over to other post events, such as an UpdateCommand.
Related
Is it possible to know if any of the textbox values have changed in the application.
I have around 30 textboxes and I want to run a part of code only if, any of the textboxes value has changed out of the 30. Is there a way I can know that.
Each text box will raise an event TextChanged when it's contents have changed. However, that requires you to subscribe to each and every event.
The good news is that you can subscribe to the event with the same method multiple times. The handler has a parameter sender which you can use to determine which of your 30 text boxes has actually raised the event.
You can also use the GotFocus and LostFocus events to keep track of actual changes. You would need to store the original value on GotFocus and then compare to the current value on LostFocus. This gets round the problem of two TextChanged events cancelling each other out.
You can assign an event handler to each of the TextBox's TextChanged events. All of them can be assigned to the same event handler in code. Then you'll know when the text changes. You can set a boolean flag field in your class to record that a change occurred.
This is perhaps on the rough and ready side, but I did it this way.
In the constructor, I created
bool bChanged = false;
In the TextChanged event handler of each control (actually same for each), I put
bChanged = true;
When appropriate, I could do some processing, and set bChanged back to false.
You can also just do this:
In your Constructor:
MyTextBox.TextChanged += new TextChangedEventHandler( TextChanged );
And Then this Method:
private void TextChanged(object Sender, TextChangedEventArgs e){
//Do something
}
try this. Add this code to the load/constructor. no need to specify the event in the XAML explicitly
this.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextChanged));
private void TextChanged(object Sender, TextChangedEventArgs e)
{
//ToDO (use sender to identify the actuale text from where it fired }
}
had the issue that my KeyUp and the KeyDown Event both fired twice when i invoked them. Finally found a fix for this:
this.glControl1.KeyUp -= this.glControl1_KeyUp;
this.glControl1.KeyUp += new KeyEventHandler(this.glControl1_KeyUp);
It works, but i really cant understand why. Could anyone explain me please.
Thank you
You probably have some repetitive event which you use as a trigger to hook up your event handler, a classic example is a button click e.g.
public void ButtonClick(object sender, EventArgs e)
{
this.SomeControl.KeyUp += this.SomeHandler;
}
The problem here is everytime the button is clicked the same event handler is assigned to the same event, there is nothing to prevent this from happening as it can be perfectly acceptable under certain circumstances.
To avoid this, you need to unhook the event handler before you reassign it, this is why executing the following line of code
this.SomeControl.KeyUp -= this.SomeHandler;
Before you assign the event handler prevents duplicate calls.
Quick one:
I've created
public event GridViewEditEventHandler invGridEdit {}.
Can I put code in this event to allow my end user to edit the DB entry, or do I need to point this to a new method for editing?
Edit: It's probably obvious that this is the first time I've looked at events. My apologies if this is a stupid question.
Check the documentation.
In general you have to register your own methods on specific events:
When you create a GridViewEditEventHandler delegate, you identify the
method that will handle the event. To associate the event with your
event handler, add an instance of the delegate to the event. The event
handler is called whenever the event occurs, unless you remove the
delegate. For more information about event-handler delegates, see
Events and Delegates.
In your case:
gridview.RowEditing += new GridViewEditEventHandler(myEditHandler);
Registered methods must have the signature your handler expects. In your case it's the delegate
public delegate void GridViewEditEventHandler(
Object sender,
GridViewEditEventArgs e
)
So the method myEditHandler looks like
void myEditHandler(Object sender, GridViewEditEventArgs e)
To access the firing gridview, perform a cast on sender:
GridView gv = (GridView)sender;
Your method is now called, if the event RowEditing is fired. This is the case, when:
The RowEditing event is raised when an Edit button (a button with its
CommandName property set to "Edit") is clicked, but before the
GridView control enters edit mode. This allows you to provide an
event-handling method that performs a custom routine, such as
canceling the edit operation, whenever this event occurs.
Hope it helps =)
Basically this is for any code you want to run to set up the editing on the grid.
From here:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewediteventhandler(v=vs.110).aspx
The RowEditing event is raised when an Edit button (a button with its CommandName property set to "Edit") is clicked, but before the GridView control enters edit mode. This allows you to provide an event-handling method that performs a custom routine, such as canceling the edit operation, whenever this event occurs.
When you create a GridViewEditEventHandler delegate, you identify the method that will handle the event. To associate the event with your event handler, add an instance of the delegate to the event. The event handler is called whenever the event occurs, unless you remove the delegate. For more information about event-handler delegates, see Events and Delegates.
How can I check that whether mouse pointer is pointing a button or some other control?
I want to perform a particular task when mouse hover/move a button.
I know I can set event on individual button. But isn't it possible to check the pointed/hover control is button?
The sender argument in an event method should have the information you need...
private void MyEventHandler(object sender, EventArgs args) {
if(sender is Button) {
//Do some stuff
}
}
I'm not sure if you mean: can I do this without event handlers for MouseHover in individual controls. If so, the answer is no.
But you can attach each contol's MouseHover event to just one event handler that could look like the one in Chris's answer. For convenience you could even do that programmatically by looping through the controls in the form's load event. (assuming this is winforms)
Is there any standard way to route all Key events from the control A to other control B? I wish that the keyboard focus will still be on A however the event handler of A would trigger the all event handlers of B for the key events.
edit: Clarification: calling a specific event handler I wrote for B is not enough. I need to mimic the actual event. So for example I want that if a key is sent to a TextBox, it would be written to the TextBox. The solution given below does not do that (not to mention the fact that if new event handlers are added to B it completely fails).
I'm aware that WPF differentiates between logical focus and keyboard focus, but I need both focuses to remain on control A, but in a certain cases route its incoming event to other controls.
Couldn't you do something like this?
private void button1_Click(object sender, RoutedEventArgs e)
{
// Check if the event needs to be passed to button2's handler
if (conditionIsMet)
{
// Send the event to button2
button2.RaiseEvent(e);
}
else
{
// button1's "Click" code
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
// button2's "Click" code
}
Edit: Modified code to use the RaiseEvent() method to programmatically raise a specific event, rather than just calling the event handler for button2.