how to access Custom EventArgs class in Button click event?

后端 未结 3 770
余生分开走
余生分开走 2021-01-27 03:13

As a follow up to: access values within custom eventargs class

How do I access public variables within custom Eventargs class, using button click or any other method?

相关标签:
3条回答
  • 2021-01-27 03:25

    Solution: Pass custom event args through a custom delegate and event that returns the data (Datatable, Array, etc)...using whatever button click or other event necessary.

    So as a function of the delegate the correct data is returned.. I can not post the complete code here, but it is a modification of this very excellent example on event delegate usage...

    Core C# and .NET 3.7. Delegates and Events http://flylib.com/books/en/4.253.1.38/1/

    0 讨论(0)
  • 2021-01-27 03:26

    You can't do that. The EventArgs of the Click event is always of type EventArgs; you can't expect it to be of type TraderEventArgs, because the event args is created by the button, and the button doesn't know anything about TraderEventArgs. The only way it could work is if you create your own Button control that raises the event with a TraderEventArgs instead of an EventArgs

    0 讨论(0)
  • 2021-01-27 03:29

    When the button click event is raised, it's creating the EventArgs that gets passed into "e". That object was not created by you, but rather by the framework itself, and is of type EventArgs. This prevents you from being able to cast it to a different type.

    If you want to have an event that raises TraderEventArgs, you need to create an event, preferably of type EventHandler<TraderEventArgs>, and raise this event somewhere that is in your control. This allows you to generate the class of the correct type, then handle it (in a separate event handler) directly.

    0 讨论(0)
提交回复
热议问题