Getting KeyCode in GotFocus event? - c#

C# WinApps: Is there any way that I can check if something like CTRL-V is pressed but not in the KeyDown,PreviewKeyDown,KeyPress,etc ... events? those are being eaten by some other parts in my App and it is so hard to find them so I thought Ok for this contorl lets check the pressed keys in its GotFocus event! Is it possible?

Not sure what you mean by the events being "eaten". Events can call multiple handlers. So even if the event is already being subscribed to by one handler, you can subscribe to it with another handler and it should work just fine.
Another option would be to subclass the control you are using and use the subclass instead. Then you can override the On{event} methods and do anything you want with those (be sure to call the base method as well to ensure the behavior of the original class is still in place).
HTH

Related

Is there anyway to stop the flow of events in .NET?

that is a question I have been asking myself for a while.
Giving a certain flow of events, can I when handling one of them, stop the next ones to be raised?
For example, when collapsing a node which child was selected in a treeview (winform), the events are raised like that:
BeforeCollapse
BeforeSelect
AfterSelect
AfterCollapse
I could stop them by using a class member, but I was wondering whether there was a built-in function or just another way (a more elegant way) to achieve this, by acting directly on the events queue.
Any idea?
Not easily, no. The order of the events firing is controlled by the TreeView control class, and there is no built-in way to prevent events from firing. But you have a couple of options:
Create your own TreeView class that inherits from the base class,
then add a bool property to prevent the events from processing.
Then you can override BeforeCollapse, etc. to check the bool
before calling base.BeforeCollapse.
Just create a bool flag, and check the flag in each of the events.
No there is no way to do that for that type of event (you are asking for TreeView).
Like for example could be managed KeyEventArgs.Handled via built-in mechanism.
You can use some instance (boolean ?) value to manage the flow,
or you can, unsubscribe from the event that you don't want more recieve, but after subscribe to it again. Sounds rough solution, but sometimes turns out reasonable one.
even if the event are raised nothing will happen if you don't bind an event handler to them. In this case you can just remove the handler using the code below:
object.Event -= new EventHandlerType(your_Method)
Otherwise you should create your own custom control
according to OnBeforeCollapse you get an TreeViewCancelEventArgs which has an Cancel property. Setting this to true should stop the flow, but will also not collapse it.
Same goes for OnBeforeSelect.
The only times you can easily "cancel" an event is if the event handler has the CancelEventHandler delegate type. Even then it doesn't really cancel it as much as set a flag for the remaining events that makes it skip performing all the events subscribed to it.
If you did have a CancelEventHandler type (which these don't) you'd simply set Cancel to true on the event object itself in the handler.
Plenty of other answers give you suggestions for what you should o. I'd just go with your idea: set a 'event cancelled' flag in your control class, and check it. When the last event in the series gets called, reset it.

C# - Changing parameters of an existing event handler

So, here's the deal. What I wanna do must be really simple, but I just can't find a solution that doesn't involve building a few classes and it wouldn't even be worth my trouble.
In a regular TextBox there is the OnKeyUp event. It's handler signature demands the sending object (of course) and a KeyEventArgs object. All I want is replace this KeyEventArgs with a CancelEventArgs object. Or, not even that, let's say that I just wanna add a new CancelEventArgs parameter to the handler. In other words, all I want is that if the wrong key is pressed, the whole event is cancelled.
I see there's a topic with a seemingly similar question, but that won't do.
Any pointers?
Set the SuppressKeyPress property of the KeyEventArgs object of the KeyDown event to true to prevent user input.
No, you can't add an extra parameter to an existing event handler. After all, the TextBox is going to raise that event - how would it know what to do for your extra parameter, or even your different one? It's been told that it will be given a KeyEventHandler to call, so it believes it can call that event handler with the normal arguments.
How would you expect this to work from the point of view of the TextBox?
As SemVanmeenen suggests, you may well find that SuppressKeyPress is what you want, although that's not quite the same as "cancelling" the whole event. It's worth making sure you understand why you can't change event handler parameters though.
If the above doesn't help, you might want to read my article on events and delegates for a better feel for how they work.

C#: Create an event that is fired whenever another event that has listeners is fired, dynamically via reflection maybe?

Here's what I am working with:
Part of my project is a windows form app. I want to basically capture every event that fires and has listeners. So some button's click event, some checkbox's check event, everything on a basic "Control" event list. So that event still fires, but also fires my event.
Is there a generic "some event fired" event under the hood I can tap into, or is there a way using reflection to enumerate through all objects in my form, parse out all the events, parse which have listeners, and then subscribe all of them to a generic event elsewhere in addition to where they are already going?
Anyone know how to do this?
You fundamentally can't do this: an event is a black box with just "subscribe" and "unsubscribe" functionality. So while you can use reflection to find out all the events, you can't reliably detect which have been subscribed to. For field-like events you could fetch the backing field and check whether or not it's null, but even that's not reliable - to avoid null checks, the author may have written something like this:
public event EventHandler SomeEvent = delegate {};
For other events, you'd have to work out what subscribing to the event actually does - for example, it might use EventHandlerList.
Basically, you should rethink your design so you don't need to do this.
Doesn't the fact that a subscribed event got fired indicate it has subscriber(s)? So then all you would need is a list of subscribable events, which you can validate against during an intercepted call.
You can intercept a call using any AOP framework. For instance, by using Unity Interception, you can do something like this:
public IMethodReturn Invoke(IMethodInvocation input,
GetNextHandlerDelegate getNext)
{
// 1. assuming that you are keeping a list of method names
// that are being subscribed to.
// 2. assuming that if the event is fired, then it must have
// been subscribed to...
if (MyReflectedListOfSubscribedEvents.Contains(input.MethodBase.ToString())
{
HandleItSomeHow();
}
// process the call...
return getNext().Invoke(input, getNext);
}

Can we Fire the button CLick event on Windowload In WPf C#

i want to FIre the button Click event When My Window is Loaded..
How Can i Achieve it in Wpf?
Create a single function with the shared behavior in your window, then call that function from both your loaded handler and your click handler.
As per this blog post in WinForms this was really easy by just calling PerformClick(), but in WPF you can do it with Automation, however as a commenter mentioned it's really easy if you have access to the button to just use RaiseEvent.
someButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
But as previously answered, if you only have a single handler that needs to be notified, then simply call that handler method directly.
You could use Automation to do it aswell - I've seen this suggested some places as the more flexible/robust method to use, but it seems a bit heavy weight to me compared to just calling the method you already have directly.
in you page_loaded event handler method, make a call to the click event like this:
_buttonName_click(sender, new RoutedEventArgs())
Trgger this event on Button whic u wanted to click
button.performclick();

Raise button (or any control) click event manually. C#

Can anyone tell me how to raise click event of button control (or for that matter for any event).
Platform: .net 2.0/3.0/3.5
Language: c#
Domain: Windows Application, WinForms, etc.
You can use the Button.PerformClick method.
Maybe the solution is much more simple:
Maybe you don't really want your code "to click the button".
Do you just want to run the code which is behind the button from another place in the form?
If yes, put the code into a separate method (like "DoActionXXX") and call the method from the button and from everywhere else where you need it.
You can also look into Windows Accessibility or some UI automation framework that allows you to programmatically cause UI controls to respond to user gestures. If a control does not offer a way for its events to be programmatically triggered like PerformClick, you can derive from that control and expose a public method like PerformnXXX that when called internally invokes the event handlers subscribed to a particular event.
button click and to new my manually window
ex.. dital.cs open the may window
I dont think sending click events is the best way from a design point of you just make your event handler call another function. That way you always have access to that function.
so ..
void myEventHandler(ObjectwhoSentTheMessage O, Event e)
{
MyOtherFunction();
//consume the event after if you wish etc
}

Categories