Simulating an event

This is a discussion on Simulating an event within the C# Programming forums, part of the General Programming Boards category; Is there any way that I can raise an already defined event, e.g. button1_Click event? I've looked in MSDN but ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    11

    Simulating an event

    Is there any way that I can raise an already defined event, e.g. button1_Click event? I've looked in MSDN but it only shows how to define your own events but doesn't show how to call a already defined event. I need it because I don't know of any way to sort a DataGrid by values in some column so I'll simulate a button click on its header. (Easier way?)

    Also, is there a way to make a multi-line column header in DataGrid? I need it because my header caption won't fit and I don't have space to make a cloumn wider.

    Thanks!

  2. #2
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    Is there any way that I can raise an already defined event, e.g. button1_Click event?
    If you know the event, just call it like any other function:

    button1_Click( yourButton, null );
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    "button1_Click" is not an event, it'a an event handler, i. e. a function or method. But "button1.Click" is. What if I don't know what function is event handler for a specific event. How do I raise that kind of event?

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,138
    You can call the defined event like any other function of type delegate that you defined for the event. Make sure there is at least one eventhandler attached by comparing your event to null before you fire it.

    I'm not sure if it will work if you are outside the button class and call the click event though.

    if( button1.Click != null ) button1.Click( null );

    I don't have a compiler at hand, so if this results in a compiler error, post it please
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  5. #5
    Banned nickname_changed's Avatar
    Join Date
    Feb 2003
    Location
    Australia
    Posts
    986
    You want to force button1 to raise the 'Click' event handler?

    button1.PerformClick(); (I think).

    For most forms or controls, if you inherit from it, you can call the OnEventName method... so to raise the Closing event, call the OnClosing method, which raises the events (OnClosing isn't an event handler - it's actually an event raiser).

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    11
    @nvoigt:

    Well, button1.Click(null) gives "The event 'event' can only appear on the left hand side of += or -=" error

    @stovellp:

    Ok, but those are both events that have methods that raise them. What about those that don't? How do I raise, e.g., dataGrid1.MouseUp event? For example, if I want items in DataGrig sorted by values in one column, I could just raise dataGrid1.MouseUp event with specific coordinates. Just don't tell me of a way to sort items in a DataGrid, this is just an example.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 02:27 AM
  2. Event driven thread programming
    By jaxen in forum C++ Programming
    Replies: 6
    Last Post: 11-22-2006, 07:46 AM
  3. XWindows- Close window event?
    By Exile in forum Linux Programming
    Replies: 8
    Last Post: 01-09-2005, 09:39 PM
  4. Replies: 2
    Last Post: 09-22-2003, 01:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21