Reacting to events triggered by API in C# - c#

I am using an API that has the following EventHandler:
static EventHandler<GPRSArgs> EventStateChanged
How can I receive these events in my client application so that I can react to them?

subscribe to the event.
lambda:
EventStateChanged += (sender, gprsArgs) => { do your thing }
delegate:
void Listener(object sender, GPRSArgs args)
{
do your thing
}
EventStateChanged += Listener;

just hook into this event. You gave few code but assuming your class name is MyClass you can do
MyClass.EventStateChanged += MyEventHandler
with
void MyEventHandler(object sender, GRPSArgs e)
{
// ...
}
or hooking up with a lambda:
MyClass.EventStateChanged += (sender, e) => { /* whatever you want */ }

Subscribe to it, as you would any other event.
I assume APIClass is the class the API has with the EventStateChanged event.
Somewhere in your code you subscribe to the event as below:
APIClass.EventStateChanged += EventStateChangedHandler;
This assumes you have a method as follows:
private void EventStateChangedHandler(object sender, GPRSArgs e)
{
// Code to react to the event.
}
You could do this in one line, with a lambda:
APIClass.EventStateChanged += (sender, e) => { /* Code to react to the event */ }

I assume you left out a public and an event in the declaration:
public class GPRSThingy
{
public static event EventHandler< GPRSArgs > EventStateChanged;
...
}
Then use it
void DoSomething(GPRSArgs e)
{
// whatever
}
var gprstThingy = new GPRSThingy();
gprstThingy.EventStateChanged += (sender, e) => { DoSomething(e); };
Sort of.

You will need to subscribe to the event:
theClassWithEvent.EventStateChanged +=
delegate (object sender, GRPSArgs e)
{
//process the event
};
The event will also need to be called to alert all subscribers, from within theClassWithEvent:
if (EventStateChanged != null)
{
EventStateChanged(someObject, new GRPSArgs());
}
Note the use of someObject above, because the exposed event is static you can't pass this and the sender, therefore will need to send something apt. Though, since you say you're using this from an API, I think we can assume that you don't need to make this call and should only subscribe to the event.

Related

How can I remove a Loaded event handler in DataGrid?

I'm trying to dynamically load columns when DataGrid is loaded, and add the event handler with some parameters in initialization.
dataGrid.Loaded += (sender, args) => AddColumns(dataGrid, GetAttachedColumns(dataGrid));
But have no idea to how to remove this handler after DataGrid is loaded. The following code doesn't work.
dataGrid.Loaded -= (sender, args) => AddColumns(dataGrid, GetAttachedColumns(dataGrid));
Please help out. Thanks.
In cases when you need to explicitly remove the event listener, you can't really use an anonymous handler. Try a plain old method:
private void DataGridLoaded(object sender, RoutedEventArgs args)
{
AddColumns(dataGrid, GetAttachedColumns(dataGrid));
}
Which you can then simply add/remove:
dataGrid.Loaded += DataGridLoaded;
dataGrid.Loaded -= DataGridLoaded;
Alternatively, if you really wanted to use the lambda form, you could hold onto a reference in a member variable. Eg:
public class MyControl
{
private RoutedEventHandler _handler;
public void Subscribe()
{
_handler = (sender, args) => AddColumns(dataGrid, GetAttachedColumns(dataGrid));
dataGrid.Loaded -= _handler;
}
public void Unsubscribe()
{
dataGrid.Loaded -= _handler;
}
}
See also other questions:
How to remove a lambda event handler

How to break event handler

I don't know how can I break event handler method list. For example I have follow code. What should i write in IF statement?
public event EventHandler myEvent;
...
myEvent += new EventHandler(met1);
myEvent += new EventHandler(met2);
myEvent += new EventHandler(met3);
...
public void met2(object sender, EventArgs e)
{
...
if(myCondition)
{
//there I want to break execution of all methods assiciated with myEvent event
//I want to break met2 and don't allow to execute met3
}
...
}
You can define your delegate, so your custom event handler, whith its custom EventArgs, with boolean value.
Example:
public class MyEventArg : EventArgs {
public bool Handle {get;set;}
}
myEvent += new MyEventHandler(met1);
public void met2(object sender, MyEventArgs e)
{
if(e.Handled)
return;
if(myCondition)
{
e.Handled = true;
return;
}
...
}
In this way, if we in any other event handlder before processing it, check if Handled == true, one time it's set into that state from one of them, others would skip that event processing.
Just an idea example, you have to change it to fit your code exact needs.
Look into KeyDownEventArgs, there is an Property Handled wich can be set to true.
You could do something similar:
class myClass {
public event EventHandler myEvent;
myEvent += new EventHandler(met1);
myEvent += new EventHandler(met2);
myEvent += new EventHandler(met3);
public void metN(object sender, MyCustomEventArgs e)
{
if(e.Cancel)
return;
// Do whatever you like
if(<someBooleanStatement>)
{
e.Cancel = true;
return;
}
// Do whatever you like
}
}

C#, How to create an event and listen for it in another class?

I can't figure out how to do this, heres sample code. Of what I wish to do.
public Class MainForm : Form
{
MyUserControl MyControl = new MyUserControl;
private void Button_Click(object sender, EventArgs e)
{
//Create MyEvent
}
}
public Class MyUserControl : UserControl
{
//listen for MyEvent from MainForm, and perform MyMethod
public void MyMethod()
{
//Do Stuff here
}
}
Step 1) Expose an event on MainForm... say..
public event Action simpleEvent
Step 2) Give MyUserControl a constructor that takes an instance of MainForm and bind an action to that event
public MyUserControl(MainForm form) {
form += () => Console.WriteLine("We're doing something!")
}
Step 3) raise the event in MainForm.Button_Click
if(simpleEvent != null) simpleEvent();
Note: You could register your own delegates and work with something other than lambda expressions. See http://msdn.microsoft.com/en-us/library/17sde2xt.aspx for a more thorough explanation
Your end result would look like...
public Class MainForm : Form
{
public event Action MyEvent;
MyUserControl MyControl = new MyUserControl(this);
private void Button_Click(object sender, EventArgs e)
{
if(simpleEvent != null) simpleEvent();
}
}
public Class MyUserControl : UserControl
{
//listen for MyEvent from MainForm, and perform MyMethod
public MyUserControl(MainForm form) {
simpleEvent += () => MyMethod();
}
public void MyMethod()
{
//Do Stuff here
}
}
This is how to delegate to an event of a private member, so the outside can listen to it.
public event EventHandlerType EventHandlerName
{
add
{
this._privateControl.EventHandlerName += value;
}
remove
{
this._privateControl.EventHandlerName -= value;
}
}
Another option would be to have an event in your form class:
public event EventHandler MyEvent;
And listen to the private member's event:
this._customControl.SomeEvent += this.SomeEventHandler;
With this:
private void SomeEventHandler(object sender, EventArgs e)
{
if (this.MyEvent != null)
{
this.MyEvent(this, e);
}
}
The usage from the outside in both cases will be the same:
var form = new Form1();
form1.MyEvent += (o, e) => { Console.WriteLine("Event called!"); };
The bottom line is the you must implement functionality inside your form to allow the outside subscribe/listen to inner events.
//listen for MyEvent from MainForm, and perform MyMethod
That's the wrong way around. Publishing an event in control is useful, the control cannot possibly guess how it is going to get used. It however most certainly should not know anything about an event that may or may not be available in the form that it gets dropped on. That has the nasty habit of blowing up when the form just doesn't (yet) have the event. The bad kind too, a crash at design time that puts up the White Screen of Darn and prevents you from fixing the problem.
A form doesn't have to guess, it knows exactly what controls it has. So where ever in the form you might want to raise the event, just call the control's MyMethod method directly. And if that's wrong for some reason, like removing the control but not the call, then you just get a compile error that's easy to fix.

Declaring event in user control asp.net?

I have declared a event on my user control
public event EventHandler<AddressEventArgs> SaveButtonClick;
protected void ButtonSave_Click(object sender, EventArgs e)
{
if (SaveButtonClick != null)
{
SaveButtonClick(this, new AddressEventArgs) ;
}
}
After I have added the user control to the new page, how would I trap the
event raised by the user control?
Either you can [Browsable] property on the event, or you can imperatively bind to the events.
userControl.SaveButtonClick += new EventHandler(handlerFunctionName);
public void handlerFunctionName(AddressEventArgs args)
{
// Here, you have access to args in response the the event of your user control
}
Control.SaveButtonClick += controlClicked;
protected void controlClicked(object sender, EventArgs e)
{
//Do work
}
First you need to subscribe to your event, and give it a method to call when the event is raised.

How to subscribe to other class' events in C#?

A simple scenario: a custom class that raises an event. I wish to consume this event inside a form and react to it.
How do I do that?
Note that the form and custom class are separate classes.
public class EventThrower
{
public delegate void EventHandler(object sender, EventArgs args) ;
public event EventHandler ThrowEvent = delegate{};
public void SomethingHappened() => ThrowEvent(this, new EventArgs());
}
public class EventSubscriber
{
private EventThrower _Thrower;
public EventSubscriber()
{
_Thrower = new EventThrower();
// using lambda expression..could use method like other answers on here
_Thrower.ThrowEvent += (sender, args) => { DoSomething(); };
}
private void DoSomething()
{
// Handle event.....
}
}
Inside your form:
private void SubscribeToEvent(OtherClass theInstance) => theInstance.SomeEvent += this.MyEventHandler;
private void MyEventHandler(object sender, EventArgs args)
{
// Do something on the event
}
You just subscribe to the event on the other class the same way you would to an event in your form. The three important things to remember:
You need to make sure your method (event handler) has the appropriate declaration to match up with the delegate type of the event on the other class.
The event on the other class needs to be visible to you (ie: public or internal).
Subscribe on a valid instance of the class, not the class itself.
Assuming your event is handled by EventHandler, this code works:
protected void Page_Load(object sender, EventArgs e)
{
var myObj = new MyClass();
myObj.MyEvent += new EventHandler(this.HandleCustomEvent);
}
private void HandleCustomEvent(object sender, EventArgs e)
{
// handle the event
}
If your "custom event" requires some other signature to handle, you'll need to use that one instead.

Categories

Resources