C# Custom Event Handler - c#

I am a person learning c#, and I have a program with a Parent form and a Child form. I want the child form to raise an event so that the Parent form can do something. I copied some code, but I am not smart enough to see what is wrong. I don't know how to correctly code the event in the child form. The error is DatasourceUpdated is not defined. Can anyone help me out with a suggested fix?
In the Child form I have
public partial class Form2 : Form
{
public EventHandler DataSourceUpdated;
...
private void button2_Click(object sender, EventArgs e) //Done button
{
if (this.DataSourceUpdated != null) //raise the event
{
this.DatasourceUpdated();
}
this.Close();
}
In the parent form I have this:
private void myAddRecord()
{
string myID = string.Empty;
string myMessage = "Insert";
Form2 myForm = new Form2(myID, myMessage);
Form2.DatasourceUpdated += ChildUpdated;
myForm.Show();

Right now, you're declaring an EventHandler, not an event. Change this to:
public partial class Form2 : Form
{
public event EventHandler DataSourceUpdated;
...
private void button2_Click(object sender, EventArgs e) //Done button
{
if (this.DataSourceUpdated != null) //raise the event
{
this.DataSourceUpdated(this, EventArgs.Empty);
}
this.Close();
}
Also, when you go to subscribe to your event, you need to subscribe to the event on the instance, not on the class:
Form2 myForm = new Form2(myID, myMessage);
myForm.DataSourceUpdated+= ChildUpdated;
myForm.Show();
This is because the event is declared at the instance level, not statically.

Form2.DatasourceUpdated += ...
you are trying to attach your handler to the class try this instead
myForm.DatasourceUpdated += ...

Your code looks right, as far as I can tell, as long as you have an actual handler; you have not included that in your code. ChildUpdated needs to be a method that with the signature void (object sender, EventArgs e), and you should also raise the event like that this.DataSourceUpdated(this, null);
The signature is being specified by the fact that you're declaring the event as being handled by System.EventHandler, which has that signature. You can create your own delegates as well, if you want it to receive some special parameters or no parameters at all.
Also, you have an inaccurate casing in your example, this.DatasourceUpdated -> this.DataSourceUpdated, but I'll assume that's just in your example...?

.NET events have both a "sender" object and an "EventArgs" object. These need to be included when your event is called.
for example:
private void button2_Click(object sender, EventArgs e) //Done button
{
if (this.DataSourceUpdated != null) //raise the event
{
this.DatasourceUpdated(this, EventArgs.Empty);
}
this.Close();
}

First of all there's a small typo: DatasourceUpdated vs DataSourceUpdated. See the capital S? Also, don't forget the args and to declare the DataSourceUpdated as an event:
public event EventHandler DataSourceUpdated;
...
this.DataSourceUpdated(this, EventArgs.Empty);
Another problem I notice is that your calling a static member when you should be calling an instance member:
Form2.DatasourceUpdated += ChildUpdated;
to
myForm.DatasourceUpdated += ChildUpdated;

Related

NullPointerException is thrown when a user control's button is clicked at clicked(this ,e)

In this code, Form1 supposes to listen to the Add button in user control and displays the message in the Form1. When i run it in a debugging mode, it return NullPointerReference at clicked(this,e). Can someone help me with this? thanks.
User Control:
public event EventHandler clicked;
public DataInput()
{
InitializeComponent();
Add.Click+= new EventHandler(Add_Click);
}
private void Add_Click(object sender, EventArgs e)
{
items = textBox1.Text.PadRight(15) + textBox2.Text.PadRight(15) + textBox3.Text.PadRight(15);
clicked(this, e);
}
Form:
public Form1()
{
InitializeComponent();
dataInput.clicked+= new EventHandler(OnChanged);
}
public void OnChanged(Object sender, EventArgs e)
{
MessageBox.Show("testing");
}
Exception is thrown because there are no subscriptions to your clicked event. Either Form1 is no yet created, maybe you're using different constructor, or you unsubscribed later.
Anyway, you should always check for subscription before invoking an event delegate.
Change your code in Add_Click to:
EventHandler evnt = clicked;
if (evnt != null)
evnt(this, e);
not sure why the "clicked" EventHandler is null.
You should always make sure the EventHandler has been initialized before using.
i.e.
if(clicked != null)
{
clicked(this, e);
}

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.

How to add a control to a panel on a form from another user control

I have a form1.cs and in that form I have a panel1, in the load event of the form1.cs I am adding a control to the panel1. Now my issue is, I have a control called Numbers.cs, I need to add another control to that panel1 but from this control in a button event. How can I do this?
public partial class Number : UserControl
{
public Number()
{
InitializeComponent();
}
private void btnAcceptWelcome_Click(object sender, EventArgs e)
{
//HERE I NEED TO PASS A CONTROL TO THE PANEL1 IN FORM1.CS
//NOT SURE HOW TO DO THIS.
}
}
MORE INFO
So basically I have a folder called UserControls and in that folder I have
Numbers.cs
Letters.cs
Welcome.cs
All of them user controls, then i have a form
Form1.cs
Form1.cs instantiates Welcome and it is added to a Panel1 on the Form1.cs on form load. Then Welcome.cs has a button, when I click this button I need to swap to Numbers.cs. But I dont know how to do this from Welcome.cs
Another way would be to use a Custom Event raised by Numbers and handled by Form1 to pass the control and add it to your Panel's Control Collection.
This is an example of an Custom Event added to UserControl1
Form1
public partial class Form1 : Form
{
UserControl2 mySecondControl = new UserControl2();
public Form1()
{
InitializeComponent();
userControl11.AddControl+=new EventHandler(SwapControls);
}
private void SwapControls(object sender, EventArgs e)
{
panel1.Controls.Remove(userControl11);
userControl11.AddControl -= new EventHandler(SwapControls);
panel1.Controls.Add(mySecondControl);
}
}
UserControl
public partial class UserControl1 : UserControl
{
public event EventHandler AddControl;
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.AddControl(this, new EventArgs());
}
}
Note:
Untested code
Assuming Form1 has (or can get) a reference to Number
Add an event handler to Number:
public partial class Number : UserControl
{
// event handler Form1 will subscribe to
public EventHandler<EventArgs> OnWelcomeAccepted = (o, e) => { };
public Number()
{
InitializeComponent();
}
private void btnAcceptWelcome_Click(object sender, EventArgs e)
{
// raise the event
OnWelcomeAccepted(sender, e);
}
}
...Form1 will have a subscription after InitializeComponent(); note the additional subscription to ControlAdded:
public partial class Form1 : Form {
public Form1()
{
InitializeComponent();
this.ControlAdded += Control_Added;
// subscribe to the event and provide the implementation
Number.OnWelcomAccepted += (o, e) => { Controls.Add(GetControl( )); }
}
private void Control_Added(object sender, System.Windows.Forms.ControlEventArgs e)
{
// process size and placement and show
}
}
No other control should be adding anything directly to Form1. Let Form1 control it's children.
One way is to have a reference to panel1 within numbers since both of them are created within form1 you can pass one as an argument to the other's constructor.
It's not very clear from your description but you can just pass the control you want in the constructor or a property. Since in C# objects are always by reference you will be action on the same control in the Button event. You can always write your own event and have the panel register for it. A more complete code sample would be great.
I'm still a little unsure of what you're doing exactly but that's OK. I think the most flexible approach is to create your own custom event. Outside of any class create a delegate:
public delegate void WelcomeClick(object sender, EventArgs e);
Inside Welcome you need to create the event handler, it can be either static or part of the instance:
public event WelcomeClick OnClick;
Inside the Button Click event in welcome you can just call that event with the same parameters:
if (OnClick != null)
OnClick(sender, e);

How to detect call to methods from another form?

I have a winform application that goes with name myForm. Within this form I open another form:
private OtherForm otherForm; //this is a field
private OpenOtherForm()
{
if (otherForm == null)
{
otherForm = new OtherForm();
otherForm.FormClosing += delegate { MessageBox.Show("OtherForm will be closed"); otherForm = null};
}
MessageBox.Show("Form is already active!");
}
This works fine. But I have some methods in the second form as well. I would like to try capturing theire call.
For example if OtherForm.DoSomething() is called within the second form, I want a message box to show thi.
I tried to assign OtherForm.DoSomething() += delegate { /* mesagebox */}; But this does not compile
otherForm.FormClosing += delegate { .. } is compiling because FormClosing is of type Event. An event can be subscribed to and when it's fired your code will run.
You can't use this syntax on a method like DoSomething(). A method can only be called with something like otherForm.DoSomething(). The code in DoSomething() will then be executed.
What you can do however is create your own event and fire it when DoSomething() is executed in the second form.
Here is the MSDN Documentation on publishing your own Event.
It would be something like:
public event EventHandler RaiseCustomEvent;
public void DoSomething()
{
OnRaiseCustomEvent();
}
protected virtual void OnRaiseCustomEvent()
{
EventHandler handler = RaiseCustomEvent;
if (handler != null)
{
handler(this, EventArgs.Empty););
}
}
If you want to respond to a call in another form you could add an event to the other form and raise it in the method you are trying to respond to.
class Form1: Form
{
public void Button1_Click(object sender, EventArgs e)
{
var form2 = new Form2();
form2.SomeMethodCalled += Form2_SomeMethodCalled;
}
public void Form2_SomeMethodCalled(object sender, EventArgs e)
{
// method in form2 called
}
}
class Form2 : Form
{
public event EventHandler SomeMethodCalled;
public void SomeMethod()
{
OnSomeMethodCalled();
// .....
}
private void OnSomeMethodCalled()
{
var s = SomeMethodCalled;
if(s != null)
{
s(this, EventArgs.Empty);
}
}
}

Error on my user control

HI could anyone advice how to solve this error?
I got the response from this thread here but unfortunately there is no more response from the author. So i decided to post here again with his solution.
ERROR: Error 1 Cannot implicitly convert type 'project1.Utility.AdminController.AdminControllerEvent' to 'System.EventHandler'
Error happen when i want to hook the
//btnDelete.Click += new AdminControllerEvent(btnDelete_Click);
namespace project1.Utility
{
public partial class AdminController : UserControl
{
public delegate void AdminControllerEvent(object sender, AdminControllerEventArgs e);
public event AdminControllerEvent SaveClick;
public event AdminControllerEvent DeleteClick;
public AdminController()
{
InitializeComponent();
//btnDelete.Click += new AdminControllerEvent(btnDelete_Click);
}
private void btnDelete_Click(object sender, AdminControllerEventArgs e)
{
if (DeleteClick != null)
{
if (MessageBox.Show(CoreMessages.DeleteAsk, CoreMessages.DeleteAsk, MessageBoxButtons.OKCancel) == DialogResult.OK)
{
DeleteClick(sender, e);
if (AdminControllerEventArgs.Success)
{
MessageBox.Show(CoreMessages.DeleteSuccess, CoreMessages.Successful, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(CoreMessages.DeleteFailed, CoreMessages.Failed, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
}
public class AdminControllerEventArgs : EventArgs
{
public static bool Success;
public AdminControllerEventArgs()
: base()
{
Success = true;
}
}
}
In my Form delete UI
private void adminController_DeleteClick(object sender, AdminControllerEventArgs e)
{
Repository.Delete(user);
}
The event Click expects a handler implementing the signature described by System.EventHandler. That won't work - you have to change the signature or implement your own additional handler for Click that raises another event calling your custom handler. I'm not really sure what you're tryin to do here mixing event handler code with other UI messages etc.
The problem here is the signature of the method. The button click event has no event data to pass and by convention they have two parameters.
Since it has no event data to pass it uses EventArgs,so since you have made your implementation the Button Click is unaware of that and so the error
Button.Click will raise Click event with plain EventArgs arguments. You can't expect Click(object sender, EventArgs e) to work with more specific method signature (using AdminControllerEventArgs arguments). How do you convert EventArgs to AdminControllerEventArgs?
Imagine it worked tho. What happens when:
Button.Click creates new EventArgs()
Button.Click calls your btnDelete_Click(this, args)
args are expected to be of type AdminControllerEventArgs
...but are in fact EventArgs
your code in btnDelete_Click tries to access args.Success property
...which isn't there, because EventArgs doesn't have one
Unfortunatelly, you'll need a workaround for this.

Categories

Resources