Solved

SubscribeCallbacks / Event Handler

Posted on 2006-07-11
4
222 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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

829 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