How to raise a ready control's event

Is there a way to raise button's click event by code.

For examples, button 1's click event maybe used by many place, Another button 2, if user click, is there a way to raise button 1's click event.

button1.Click += ......
....

void Button2_Click(Object send, , EventArgs e)
{
How to raise button 1 event
Don't know button1.Click event handler is used by other code
Want boradcast event to all listeners
}



Answer this question

How to raise a ready control's event

  • Robert Liu

    button1.PerformClick()

    -vj

  • Don Jelley

    Great post. But what if you don't know a priori that it is button1 that you want to click What if the program makes a choices of button1, button2, and button3 all of which are currently non-clickable so b.performclick is not usable

    Reflection probably would be the answer but I have not been able to make it work.

     rem mi is one of a variety of toolstripmenuitems
     Dim typ As Type = mi.GetType
     Dim ev As System.Reflection.EventInfo = typ.GetEvent("Click")
    If ev Is Nothing Then Throw LI.ErrHandler.Goof("Program error: could not find a click event for menu item~n{0}~nwhose text is~n{1}.", mi.Name, mi.Text.Replace("&", ""))
     Dim rm As System.Reflection.MethodInfo = ev.GetRaiseMethod()
     If rm Is Nothing Then Throw LI.ErrHandler.Goof("Program error: could not find a click raise method for menu item~n{0}~nwhose text is~n{1}.", mi.Name, mi.Text.Replace("&", ""))
     Dim ea As New EventArgs
     Dim params() As Object
     ReDim params(1)
     params(0) = mi
     params(1) = ea
     rm.Invoke(mi, params)

    One or the othr of the Goof's always get's thrown, I forget which at the moment. In this example MI is clickable and now that I've found performclick that's which I'll use.

    QUESTION: I assume that performclick is a synchronous activity (control doesn't return to the calling routine until after the click processing is completed.) CORRECT

    Regards,
    Al

  • kcvaio

    If the button is not selectable (Enabled, Visible, and Tab-To-Able) calling button.PerformClick() will not actually fire the click event. 

    You can still directly call the method that is hooked up to the click event, if you wish to perform the work.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    MessageBox.Show("Clicked")

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Button1_Click(Button1, EventArgs.Empty)

    End Sub



  • LarryNeedsHelp

    Thanks Jessica - it worked perfectly.
  • Roland51

    Do you know if it possible to use "PerformClick" on a button which is not visible   Sometimes I would like to run the code of a button which is temporarily hidden, but it doesn't seem to work.  I have to set the visible property of the button to True for PerformClick to run the code.

    Opps, I've just realized I'm in the wrong forum - my question relates to Visual Basic, not C#.  I don't seem to be able to delete the post though so maybe somebody here will have a suggestion.

  • How to raise a ready control's event