Solved

SubscribeCallbacks / Event Handler

Posted on 2006-07-11
4
223 Views
Last Modified: 2012-05-05
Using the below code example...   I have a sample app where..  as the dataevents occur...   the dataevent's eventhandler basically calls an event click from a button control named BtnData..  in order to run the handling code which contains a for loop...     questions are below after the code sample...   i'm "spacing" here as to what's possible with using events outside of using them with controls (i.e.  buttons, key press, etc..)


public Button BtnData;

//
// BtnData
//
this.BtnData.Name = "BtnData";
this.BtnData.Text = "BtnData";
this.BtnData.Click += new System.EventHandler(this.BtnData_Click);


dataevent = data;
dataevent.GBEvent += new GBLib.IGBEventVtbl_GBEventHandler(dataevent_GBEvent);
data.SubscribeCallback("DataEvents", 1);

private void dataevent_GBEvent (object pISrc, string bstrData, string bstrHint, object pIEventData)
{
       BtnData_Click(BtnData, new EventArgs());
}

private void BtnData_Click (Object eventSender, EventArgs eventArgs)
{
     for (int i = 0; i <= collection.size - 1; i++)
     {
           < ... do some stuff here ...  >
     }
}

----------------------------------------------------------------------------------------

1)   What would the reason be as to why the sample app needs to call a control's event click this in order to fire the event?
2)   Is there a way to fire an event in a similar fashion without using a control button?  or any controls event ?
3)   What is the flow of the event args as they are passed from function to function?



0
Comment
Question by:lblinc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 20

Assisted Solution

by:Daniel Van Der Werken
Daniel Van Der Werken earned 100 total points
ID: 17087134
I suspect you need to use delegates to create the events you want.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkEventsTutorial.asp
0
 

Author Comment

by:lblinc
ID: 17087175

Actually, I just recalled the support for this API's dataevent_GBEvent  saying that the reason for handling in this manner is because the data is too large to disseminate directly to the handler in realtime quantities...   and that i t is up to the client to request the data through another event at needed intervals, or whenever necessary..   does that make sense?
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 17087205
I've never heard of that size limitation.  It's possible.

I guess you answered question 1.

The primary thing you're interested in an event is the sender and eventargs.  Any appropriate control should work.

Event args are passed back via the callback.  When you click on a button, the specific details for that control defined by the program send back the current arguments.

0
 
LVL 13

Accepted Solution

by:
devsolns earned 400 total points
ID: 17087489
Here is an easy to understand example of how to create your own events and how to trigger them.


//create some custom event args.
public class AppleEventArgs : EventArgs
{
    public readonly string EatenBy;

    public AppleEventArgs(string eatenBy)
    {
        EatenBy = eatenBy;
    }
}

public class Apple
{
    //create event delegate(type safe function pointer)
    public delegate void AppleEatenDelegate(object sender, AppleEventArgs args);

    //create the event
    public event AppleEatenDelegate OnAppleEaten = null;

    //create the event invoker, should be protected if plan on not sealing class.
    protected void OnAppleEatenInvoke(string eatenBy)
    {
        //first line ensures its thread safe.
        AppleEatenDelegate appleEatenEvent = OnAppleEaten;
        if (appleEatenEvent != null) appleEatenEvent(this, new AppleEventArgs(eatenBy));
    }

    public void EatApple(string eatenBy)
    {
        //eat the apple here, now
        //trigger the event in this method.
        OnAppleEatenInvoke(eatenBy);
    }
}



and to use it,

Apple apple = new Apple();
apple.OnAppleEaten += new Apple.AppleEatenDelegate(apple_OnAppleEaten);
apple.EatApple();

void apple_OnAppleEaten(object sender, AppleEventArgs args)
{
    throw new Exception("The method or operation is not implemented.");
}


-gp
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question