how to simulate a toolbar button click event programmatically

dannche
12-07-2004, 12:11 AM
Hi Everyone,

Is there a way to trigger a button click event for a toolbar button programmatically using vb.net?

Really appreciate any help but sample code will be greate.

Thank you in advance for your help.

Danny. :p

Himo
12-07-2004, 12:47 AM
Yup, use this:


Button1.PerformClick()

dannche
12-07-2004, 01:27 AM
Yup, use this:


Button1.PerformClick()


Hi Himo,

I tried your suggestion but it doesn't work.
My sample code as follows:
Me.tbarGRC_ButtonClick(Sender as Object, e As System.Windows.Forms.ToolBarButtonClickEventArgs)

I have the above method that requires 2 arguments. I have no idea what arguments are needed.

Hope the above make sense.

Regards,
Danny

Himo
12-07-2004, 01:59 AM
Wew, that cost me a while...First tried to get into the arguments, no PerformClick then tried to Send a Windows Message to the control, but that's more or less legacy stuff. This is the easiest way(too obvious):


Me.ToolBar1_ButtonClick(Me, Nothing)


Just pass the form as the object and no arguments.

dannche
12-07-2004, 02:44 AM
Hi Himo,

I tried but it gave me an error. Here is a portion of my code:

Private Sub tbarGRC_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles tbarGRC.ButtonClick
Select Case e.Button.Text
Case "New"
bSave = False
lblInfo.Text = "Adding New GRC."
grdGRCHdr.AllowAddNew = True
grdGRCHdr.AllowDelete = False
grdGRCHdr.AllowUpdate = True

grdGRCDet.AllowAddNew = True
grdGRCDet.AllowDelete = True
grdGRCDet.AllowUpdate = True

'Button Controls
Me.tbarbtnEdit.Enabled = False
Me.tbarbtnDelete.Enabled = False
sSQL = "Select * From GRC_Hdr Where CustID='XZY'"
Call RetrieveGRCHdrInfo()

Case "Edit"
bSave = False
lblInfo.Text = "Editing Country Information."
grdGRCHdr.AllowAddNew = False
grdGRCHdr.AllowDelete = False
grdGRCHdr.AllowUpdate = True
grdGRCDet.AllowAddNew = True
grdGRCDet.AllowDelete = True
grdGRCDet.AllowUpdate = True

<After the user click on Edit, I want to auto simulate the
Save Btn by using the code below>
Me.tbarGRC_ButtonClick(Me.tbarbtnSave, Nothing)

Case "Save"
'Perform some save work here...
End Select

Thank you for the speedy reply.

Best regards,
Danny

Himo
12-07-2004, 04:07 AM
Me.tbarGRC_ButtonClick(Me.tbarbtnSave, Nothing)

No, no, no, bad you ;)

Just pass the Form as object, not a subItem. It's:

Me.tbarbtnSave_ButtonClick(Me, Nothing)

Else what error are you getting? At what line?

dannche
12-07-2004, 05:42 AM
I tried exactly what you suggested. I got an error at
Select Case e.Button.text
and the error message is as follows:
An unhandled exception of type 'System.NullReferenceException' occurred in ERA DWS.exe

Additional information: Object reference not set to an instance of an object

Regards,
Danny

Iceplug
12-07-2004, 05:52 AM
Why don't you just copy the Save code to the end of the Edit code... or put the save code into a subroutine and just call that?

If you wanted to call this sub again but for the save button, you'd have to do this:
Me.tbarGRC_ButtonClick(Me.tbarbtnSave, New ToolBarButtonClickEventArgs(Me, Toolbar.Buttons(???))
Where ??? is the index of the button you want to click. :)

dannche
12-07-2004, 06:01 AM
Hi Iceplug,

Thanks for the info. I did this and it works perfectly. Thanks again.
Me.tbarGRC_ButtonClick(Me.tbarbtnSave, New ToolBarButtonClickEventArgs(Me.tbarbtnSave))

Himo
12-07-2004, 06:10 AM
I tried exactly what you suggested. I got an error at
Select Case e.Button.text
and the error message is as follows:
An unhandled exception of type 'System.NullReferenceException' occurred in ERA DWS.exe

Additional information: Object reference not set to an instance of an object

Regards,
Danny

Yes, you're trying to check the parameters, but you just passed Nothing to it, so they're empty. This should be more like it:


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim foo As System.Windows.Forms.ToolBarButtonClickEventArgs
foo = New ToolBarButtonClickEventArgs(Me.ToolBarButton1)

foo.Button.Tag = "Save"
Me.ToolBar1_ButtonClick(Me.ToolBarButton1, foo)
End Sub

Private Sub ToolBar1_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles ToolBar1.ButtonClick

Select Case e.Button.Tag
Case "Save"
MsgBox("Works")
End Select


End Sub

Himo
12-07-2004, 06:11 AM
Why don't you just copy the Save code to the end of the Edit code... or put the save code into a subroutine and just call that?

If you wanted to call this sub again but for the save button, you'd have to do this:
Me.tbarGRC_ButtonClick(Me.tbarbtnSave, New ToolBarButtonClickEventArgs(Me, Toolbar.Buttons(???))
Where ??? is the index of the button you want to click. :)

Lol, easy as pie, but didn't look into it. What if we hadn't you? :rolleyes:

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum