Solved

SubscribeCallbacks / Event Handler

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

Independent Software Vendors: 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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 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