MyEventHandler issue

X

XJ

Hi Experts,
recently i'm trying to do some event handle to manage my
application, the major purpose is, once my main form method get
calling, i need the rest of my sub instance user control function also
need to be trigger and doing the following action, i believe event
handling is the direction.

i have read some article and sample, but my code since still cannot
work, i need some expert to spend some little time to help to this,
billion thanks:

my problem is : 1) how to share the same action 2) i dont know where
to fire for the action.

code below:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace WpfApplication3
{
public class Class3
{
public delegate void MyEventHandler ( object o, EventArgs e );
public event MyEventHandler MyEventHandler_1;
public event MyEventHandler MyEventHandler_2;
}

public class MyClass1
{
public Class3 x = new Class3();

public void MyClass1_()
{
x.MyEventHandler_1 += new
Class3.MyEventHandler(OnMyEventHandler_1);
x.MyEventHandler_2 += new
Class3.MyEventHandler(OnMyEventHandler_2);
}

public void OnMyEventHandler_1( object o , EventArgs e )
{
MessageBox.Show("called OnMyEventHandler_1");
}

public void OnMyEventHandler_2(object o, EventArgs e)
{
MessageBox.Show("called OnMyEventHandler_2");
}

}

}
 
M

Marc Gravell

I'm not clear what you mean in "1", but for "2" you need some code that
can raise the event, for example:

public class Class3
{
public event MyEventHandler SomeEvent;
public void OnSomeEvent()
{
if(SomeEvent!=null) SomeEvent(this, {args});
}
}

Marc
 
X

XJ

I'm not clear what you mean in "1", but for "2" you need some code that
can raise the event, for example:

     public class Class3
     {
         public event MyEventHandler SomeEvent;
         public void OnSomeEvent()
         {
              if(SomeEvent!=null) SomeEvent(this, {args});
         }
     }

Marc

Hi Marc,
thanks for your feedback, i have few question here:
1) do u think i need to create a instance x to bcos of use the
method inside?
public Class3 x = new Class3();

2) if i include this method, the question, when this method will
get fire ?

public event MyEventHandler SomeEvent;

thanks for your feedback.
 
J

Jon Skeet [C# MVP]

          thanks for your feedback, i have few question here:
   1) do u think i need to create a instance x to bcos of use the
method inside?
                    public Class3 x = new Class3();

Well in order to use any instance members you'll have to create an
instance of the class *somewhere* - but it's not clear where would be
more appropriate in your app.
   2)  if i include this method, the question, when this method will
get fire ?

          public event MyEventHandler SomeEvent;

The event will get fired when you raise it. It won't just happen
automatically - you'll need to work out when you need to raise it, and
then do so as per Marc's previous message.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top