Syntax to send two strings in eventArgs - c#

In the following code I need to know the syntax of passing two strings when the event is raised.
[PublishEvent("Click")]
public event EventHandler<EventArgs<string>> MyEvent;
Thanks,
Saxon.

The cleanest way is to create your own class that derives from EventArgs:
public class MyEventArgs : EventArgs
{
private readonly string _myFirstString;
private readonly string _mySecondString;
public MyEventArgs(string myFirstString, string mySecondString)
{
_myFirstString = myFirstString;
_mySecondString = mySecondString;
}
public string MyFirstString
{
get { return _myFirstString; }
}
public string MySecondString
{
get { return _mySecondString; }
}
}
And use it like this:
public event EventHandler<MyEventArgs> MyEvent;
To raise the event, you can do something like this:
protected virtual void OnMyEvent(string myFirstString, string mySecondString)
{
EventHandler<MyEventArgs> handler = MyEvent;
if (handler != null)
handler(this, new MyEventArgs(myFirstString, mySecondString));
}

Make your class and extend for EventArgs, and pass it
public class YourCustomeEvent : EventArgs
{
public string yourVariable {get; }
}
Now you have to provide your custom class like this
public event EventHandler<YourCustomeEvent> MyEvent;

Related

The event can only appear in the left hand side of += or -+ [duplicate]

I have a base class that contains the following events:
public event EventHandler Loading;
public event EventHandler Finished;
In a class that inherits from this base class I try to raise the event:
this.Loading(this, new EventHandler()); // All we care about is which object is loading.
I receive the following error:
The event 'BaseClass.Loading' can only appear on the left hand side of += or -= (BaseClass')
I am assuming I cannot access these events the same as other inherited members?
What you have to do , is this:
In your base class (where you have declared the events), create protected methods which can be used to raise the events:
public class MyClass
{
public event EventHandler Loading;
public event EventHandler Finished;
protected virtual void OnLoading(EventArgs e)
{
EventHandler handler = Loading;
if( handler != null )
{
handler(this, e);
}
}
protected virtual void OnFinished(EventArgs e)
{
EventHandler handler = Finished;
if( handler != null )
{
handler(this, e);
}
}
}
(Note that you should probably change those methods, in order to check whether you have to Invoke the eventhandler or not).
Then, in classes that inherit from this base class, you can just call the OnFinished or OnLoading methods to raise the events:
public AnotherClass : MyClass
{
public void DoSomeStuff()
{
...
OnLoading(EventArgs.Empty);
...
OnFinished(EventArgs.Empty);
}
}
You can only access an event in the declaring class, as .NET creates private instance variables behind the scenes that actually hold the delegate. Doing this..
public event EventHandler MyPropertyChanged;
is actually doing this;
private EventHandler myPropertyChangedDelegate;
public event EventHandler MyPropertyChanged
{
add { myPropertyChangedDelegate += value; }
remove { myPropertyChangedDelegate -= value; }
}
and doing this...
MyPropertyChanged(this, EventArgs.Empty);
is actually this...
myPropertyChangedDelegate(this, EventArgs.Empty);
So you can (obviously) only access the private delegate instance variable from within the declaring class.
The convention is to provide something like this in the declaring class..
protected virtual void OnMyPropertyChanged(EventArgs e)
{
EventHandler invoker = MyPropertyChanged;
if(invoker != null) invoker(this, e);
}
You can then call OnMyPropertyChanged(EventArgs.Empty) from anywhere in that class or below the inheritance heirarchy to invoke the event.
I am assuming I cannot access these events the same as other inherited members?
Precisely. It's customary to provide a protected function OnXyz or RaiseXyz for each event in the base class to enable raising from inherited classes. For example:
public event EventHandler Loading;
protected virtual void OnLoading() {
EventHandler handler = Loading;
if (handler != null)
handler(this, EventArgs.Empty);
}
Called in the inherited class:
OnLoading();
You can try this way, It works for me:
public delegate void MyEventHaldler(object sender, EventArgs e);
public class B
{
public virtual event MyEventHaldler MyEvent;
protected override void OnChanged(EventArgs e)
{
if (MyEvent != null)
MyEvent(this, e);
}
}
public class D : B
{
public override event MyEventHaldler MyEvent;
protected override void OnChanged(EventArgs e)
{
if (MyEvent != null)
MyEvent(this, e);
}
}
not to resurrect an old thread but in case anybody is looking, what I did was
protected EventHandler myPropertyChangedDelegate;
public event EventHandler MyPropertyChanged
{
add { myPropertyChangedDelegate += value; }
remove { myPropertyChangedDelegate -= value; }
}
This lets you inherit the event in a derived class so you can invoke it without requiring to wrap the method while keeping the += syntax. I guess you could still do that with the wrapping methods if you did
public event EventHandler MyPropertyChanged
{
add { AddDelegate(value); }
remove { RemoveDelegate(value); }
}

Getting update from UserControl

Can someone tell me how can I have a feature in my UserControl, that can let the host windowsform know what is the control is doing?
For example my usercontrol has a filebrowser, and if user uses this file browser to open a file I want in the statusstrip bar of my form to write "Loading file(s)".
Will this require using events? if so, how can I have a single event inside usercontrol to report anything it does (then I guess I have to call this event in all methods in the usercontrol).
Simple
Yes, expose an event on the user control that the Form can subscribe to. You should use the standard event pattern:
class MyUserControl : UserControl
{
public event EventHandler<EventArgs> FileOpened;
protected virtual void OnFileOpened(EventArgs e)
{
EventHandler<EventArgs> handler = FileOpened;
if (handler != null)
handler(this, e);
}
}
Then when the file is opened you call OnFileOpened(EventArgs.Empty) which fires the event.
With custom EventArgs
Now the Form probably needs to know what file was opened. You could expose a property on the user control that the Form can use to find out, or you can provide that information in your event like so:
public class FileOpenedEventArgs : EventArgs
{
private string filename;
public FileOpenedEventArgs(string filename)
{
this.filename = filename;
}
public string Filename { get { return filename; } }
}
class MyUserControl : UserControl
{
public event EventHandler<FileOpenedEventArgs> FileOpened;
protected virtual void OnFileOpened(FileOpenedEventArgs e)
{
EventHandler<FileOpenedEventArgs> handler = FileOpened;
if (handler != null)
handler(this, e);
}
}
Then you fire the event with OnFileOpened(new FileOpenedEventArgs(filename)).
Optimal
When you create an event handler public event delegate Name;, you are allocating storage for the delegate on your object. Objects (especially Controls) often have a huge number of events that are never subscribed to. That's a whole lot of allocated storage not being used. There's an optimization built into the framework in the form of a EventHandlerList. This handy object stores event handlers only when they are actually used. All System.Windows.Forms.Control objects derive from System.ComponentModel.Component and it already provides an (protected) EventHandlerList that you can access in your derived Control.
To use it, you first create a static object that uniquely identifies your event, and then you provide the add {} and remove {} methods manually. Like so:
class MyUserControl : UserControl
{
private static readonly object FileOpenedKey = new Object();
public event EventHandler<FileOpenedEventArgs> FileOpened
{
add { Events.AddHandler(FileOpenedKey, value); }
remove { Events.RemoveHandler(FileOpenedKey, value); }
}
protected virtual void OnFileOpened(FileOpenedEventArgs e)
{
var handler = (EventHandler<FileOpenedEventArgs>)Events[FileOpenedKey];
if (handler != null)
handler(this, e);
}
}
Yes, you will need to create an event and subscribe to it. One suggestion following the standard pattern for events:
enum ControlStatus {Idle, LoadingFile, ...}
class StatusChangedEventArgs : EventArgs
{
public ControlStatus Status {get; private set;}
public StatusChangedEventArgs(ControlStatus status)
: base()
{
this.Status = status;
}
}
partial class MyControl : UserControl
{
public ControlStatus Status {get; private set;}
public event EventHandler<StatusChangedEventArgs> StatusChanged;
protected virtual void OnStatusChanged(StatusChangedEventArgs e)
{
var hand = StatusChanged;
if(hand != null) hand(this, e);
}
void LoadFiles()
{
...
Status = ControlStatus.LoadingFiles;
OnStatusChanged(new StatusChangedEventArgs(this.Status));
...
Status = ControlStatus.Idle;
OnStatusChanged(new StatusChangedEventArgs(this.Status));
}
}
partial class MyHostWindowsForm : Form
{
public MyHostWindowsForm()
{
var ctl = new MyControl();
...
ctl.StatusChanged += ctl_StatusChanged;
}
void ctl_StatusChanged(object sender, StatusChangedEventArgs e)
{
switch(e.Status)
{
case ControlStatus.Idle:
statusStripBar.Text = null;
break;
case ControlStatus.LoadingFiles:
statusStripBar.Text = "Loading file(s)";
break;
...
}
}
}

MVP Framework for winforms

i'm working in a new project and i want to implement MVP pattern. There is a framework for winforms that use this pattern? I checked CAB but my project isn't complex to implement it, i search for something more simple to implement and use.
Thanks!
If you are looking for something simple... then you really don't need a framework. You can roll your own MVP pattern.
Writing the base classes takes only a few minutes.
//Base Presenter Class
public class Presenter<TView> where TView : class, IView {
public TView View { get; private set; }
public Presenter(TView view) {
if (view == null)
throw new ArgumentNullException("view");
View = view;
View.Initialize += OnViewInitialize;
View.Load += OnViewLoad;
}
protected virtual void OnViewInitialize(object sender, EventArgs e) { }
protected virtual void OnViewLoad(object sender, EventArgs e) { }
}
//Base View
public interface IView {
event EventHandler Initialize;
event EventHandler Load;
}
That is all you need to get started. You can then define a new view to suit your needs.
public interface IPersonView : IView {
String PersonName { get; set; }
DateTime? DOB { get; set; }
event EventHandler SavePerson;
}
Create a presenter that uses the view.
public class PersonPresenter : Presenter<IPersonView> {
private IPersonDb PersonDB { get; set; }
public PersonPresenter(IPersonView view, IPersonDb personDB)
: base(view) {
if (personDB == null)
throw new ArgumentNullException("personDB");
PersonDB = personDB;
}
protected override void OnViewInitialize(object sender, EventArgs e) {
base.OnViewInitialize(sender, e);
View.PersonName = "Enter Name";
View.DOB = null;
View.SavePerson += View_SavePerson;
}
void View_SavePerson(object sender, EventArgs e) {
PersonDB.SavePerson(View.PersonName, View.DOB);
}
}
And finally put it into use in a new form.
public partial class Form1 : Form, IPersonView {
private PersonPresenter Presenter { get; set; }
public Form1() {
Presenter = new PersonPresenter(this, new PersonDb());
InitializeComponent();
InvokeInitialize(new EventArgs());
}
public string PersonName {
get { return tbName.Text; }
set { tbName.Text = value; }
}
public DateTime? DOB {
get {
return String.IsNullOrWhiteSpace(tbDOB.Text) ?
(DateTime?) null :
DateTime.Parse(tbDOB.Text);
}
set {
tbDOB.Text = String.Format("{0}", value);
}
}
public event EventHandler Initialize;
public void InvokeInitialize(EventArgs e) {
EventHandler handler = Initialize;
if (handler != null) {
handler(this, e);
}
}
public event EventHandler SavePerson;
public void InvokeSavePerson(EventArgs e) {
EventHandler handler = SavePerson;
if (handler != null) {
handler(this, e);
}
}
}
I like Jeremy Miller's stuff a lot. And I have used the Smart Client Software Factory... but those are about solving very large complicated problems. There are so many other patterns mixed in that it overshadows the simplicity of the MVP pattern to begin with.
Start simple and as you start to run into rough spots, then you can begin to add in things like Service Locators and Event Aggregators.
The MVP pattern is really very trivial to implement. I hope this can help to get you off to a running start more quickly.
Cheers,
Josh
This is not a framework, but I would read Jeremy Miller's Build Your Own Cab series before you settle on your design. He covers the various presentation patterns in WinForms.

How to create Event Handler for my Class

I have following class
public class ButtonChange
{
private int _buttonState;
public void SetButtonState(int state)
{
_buttonState = state;
}
}
I want to fire an event whenever _buttonState value changes, finaly I want to define an event handler in ButtonChange
Will you guys help me please??
P.S : I dont want to use INotifyPropertyChanged
How about:
public class ButtonChange
{
// Starting off with an empty handler avoids pesky null checks
public event EventHandler StateChanged = delegate {};
private int _buttonState;
// Do you really want a setter method instead of a property?
public void SetButtonState(int state)
{
if (_buttonState == state)
{
return;
}
_buttonState = state;
StateChanged(this, EventArgs.Empty);
}
}
If you wanted the StateChanged event handler to know the new state, you could derive your own class from EventArgs, e.g. ButtonStateEventArgs and then use an event type of EventHandler<ButtonStateEventArgs>.
Note that this implementation doesn't try to be thread-safe.
Property based event raising:
public class ButtonChange
{
private int _buttonState;
public int ButtonState
{
get { return _buttonState; }
set
{
if (_buttonState == value)
return;
_buttonState = value;
OnButtonStateChanged();
}
}
public event EventHandler ButtonStateChanged;
private void OnButtonStateChanged()
{
if (this.ButtonStateChanged != null)
this.ButtonStateChanged(this, new EventArgs());
}
}
Help yourself with google "c# events msdn"
Events tutorial (C#) - MSDN if you are using plain c#. INotifyPropertyChanged is for WPF - you don't need it for POCO/simple type events

How to add an event to a class

Say I have a class named Frog, it looks like:
public class Frog
{
public int Location { get; set; }
public int JumpCount { get; set; }
public void OnJump()
{
JumpCount++;
}
}
I need help with 2 things:
I want to create an event named Jump in the class definition.
I want to create an instance of the Frog class, and then create another method that will be called when the Frog jumps.
public event EventHandler Jump;
public void OnJump()
{
EventHandler handler = Jump;
if (null != handler) handler(this, EventArgs.Empty);
}
then
Frog frog = new Frog();
frog.Jump += new EventHandler(yourMethod);
private void yourMethod(object s, EventArgs e)
{
Console.WriteLine("Frog has Jumped!");
}
Here is a sample of how to use a normal EventHandler, or a custom delegate. Note that ?. is used instead of . to insure that if the event is null, it will fail cleanly (return null)
public delegate void MyAwesomeEventHandler(int rawr);
public event MyAwesomeEventHandler AwesomeJump;
public event EventHandler Jump;
public void OnJump()
{
AwesomeJump?.Invoke(42);
Jump?.Invoke(this, EventArgs.Empty);
}
Note that the event itself is only null if there are no subscribers, and that once invoked, the event is thread safe. So you can also assign a default empty handler to insure the event is not null. Note that this is technically vulnerable to someone else wiping out all of the events (using GetInvocationList), so use with caution.
public event EventHandler Jump = delegate { };
public void OnJump()
{
Jump(this, EventArgs.Empty);
}

Categories

Resources