How do I send custom EventHandler to this class?

gggggggg5555

So there is this class in gtk sharp:

http://docs.go-mono.com/?link=E%3aGtk.Button.Clicked

It accepts EventHandler type events. I want to use a custom event handler that passes strings to the function it calls.

The thing is, I don't seem to be able to feed it my CustomEventHandler. Even if I use EventHandler as an abstract class in its definition.

Why can't I do it? I think I am missing something obvious...

Edit, my code:

public MainWindow () : base (Gtk.WindowType.Toplevel)
{
    Button button = new Button ("button");
    button.Clicked += new EventHandler(my_function);
    this.Add (button);
    Build ();
}
static void my_function(object obj, EventArgs args){
        //some code
}

I've tried changing new EventHandler to MyEventHandler(my_function, MyCustomArghs) but no matter what combination I try, there are always some errors. Mainly related to trying to cast to type EventHandler - event though MyEventHandler is a child of EventHandler class.

Peter Duniho

The event handler subscribed to the event must conform to C#'s delegate type rules. In particular, while you can "broaden" the arguments, you can't "narrow" them. I.e. your arguments can be base types of the actual delegate-specific argument types, but they cannot be derived classes of them.

You're not specific about what you would actually put in your custom EventArgs class. But the usual way to approach this kind of scenario is to wrap your event handler method in an anonymous method that matches the delegate type, using the anonymous method as an adapter.

For example:

public MainWindow () : base (Gtk.WindowType.Toplevel)
{
    Button button = new Button ("button");
    button.Clicked += (sender, e) => my_function("some string literal");
    this.Add (button);
    Build ();
}
static void my_function(string text){
        //some code
}

Note that when you take this approach, your actual event handler doesn't have to have anything like the actual event signature at all. You just have to be able to write whatever code is required to adapt the actual event arguments to those your handler will take. If you want to completely ignore the event's actual arguments and just pass something else, you can.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I implement a custom @JsonDeserialize method on an endpoint class?

From Dev

How do I send arbitrary JSON data, with a custom header, to a REST server?

From Dev

How do I create a custom namespace or class in javascript

From Dev

How do I send custom EventHandler to this class?

From Dev

How do I specify custom CSS class for a Yii field?

From Dev

How do I set an initializer for a custom UITableViewCell class?

From Dev

How do I make my custom class compatible with For Each?

From Dev

How do I associate a Storyboard UIViewController with my custom UIViewController class?

From Dev

How do I use torch's class system to create a custom class

From Dev

How do I send 401 error codes instead of 403 with a custom permission class?

From Dev

How can I bind an eventhandler to a class method in typescript?

From Dev

How do I add a custom image to notify-send?

From Dev

How can I send a custom desktop notification?

From Dev

How do I Map a List of Maps into a List of my Custom Class?

From Dev

How do I add a custom image to notify-send?

From Dev

How can I send a custom desktop notification?

From Dev

How do I send arbitrary JSON data, with a custom header, to a REST server?

From Dev

How do I address custom view class from Activity?

From Dev

How do I create a custom namespace or class in javascript

From Dev

How do I create an equality test for a custom class in Swift?

From Dev

How do I map relationships in Custom NSManagedObject class

From Dev

How do I send a message from custom receiver to sender app with Chromecast?

From Dev

How do I add a class to a custom namespace?

From Dev

How do I create a custom UITableViewCell class in Swift?

From Dev

How do I cause Jenkins server to send custom message even on successful build

From Dev

JavaFX. Register eventHandler in custom class

From Dev

How do I send a message to a UIViewController in a separate file from a custom UIView

From Dev

How do I send a custom templatized e-mail from a Jenkins Pipeline script?

From Dev

How do I access properties or custom methods on a class in a view in NodeJs?

Related Related

  1. 1

    How do I implement a custom @JsonDeserialize method on an endpoint class?

  2. 2

    How do I send arbitrary JSON data, with a custom header, to a REST server?

  3. 3

    How do I create a custom namespace or class in javascript

  4. 4

    How do I send custom EventHandler to this class?

  5. 5

    How do I specify custom CSS class for a Yii field?

  6. 6

    How do I set an initializer for a custom UITableViewCell class?

  7. 7

    How do I make my custom class compatible with For Each?

  8. 8

    How do I associate a Storyboard UIViewController with my custom UIViewController class?

  9. 9

    How do I use torch's class system to create a custom class

  10. 10

    How do I send 401 error codes instead of 403 with a custom permission class?

  11. 11

    How can I bind an eventhandler to a class method in typescript?

  12. 12

    How do I add a custom image to notify-send?

  13. 13

    How can I send a custom desktop notification?

  14. 14

    How do I Map a List of Maps into a List of my Custom Class?

  15. 15

    How do I add a custom image to notify-send?

  16. 16

    How can I send a custom desktop notification?

  17. 17

    How do I send arbitrary JSON data, with a custom header, to a REST server?

  18. 18

    How do I address custom view class from Activity?

  19. 19

    How do I create a custom namespace or class in javascript

  20. 20

    How do I create an equality test for a custom class in Swift?

  21. 21

    How do I map relationships in Custom NSManagedObject class

  22. 22

    How do I send a message from custom receiver to sender app with Chromecast?

  23. 23

    How do I add a class to a custom namespace?

  24. 24

    How do I create a custom UITableViewCell class in Swift?

  25. 25

    How do I cause Jenkins server to send custom message even on successful build

  26. 26

    JavaFX. Register eventHandler in custom class

  27. 27

    How do I send a message to a UIViewController in a separate file from a custom UIView

  28. 28

    How do I send a custom templatized e-mail from a Jenkins Pipeline script?

  29. 29

    How do I access properties or custom methods on a class in a view in NodeJs?

HotTag

Archive