Unable to 'Call' from another location

jackmac

Member
Joined
Apr 20, 2005
Messages
9
Programming Experience
Beginner
Hi all,

Sorry for a newb question but this is driving me nuts. I have a form which has a button and a toolbar.

The button has code which works and deletes a record in a database:

=======================================
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If (Me.Validate() And Not (PlayersBindingSource Is Nothing)) Then
If (MsgBox("Delete?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes) Then
PlayersBindingSource.RemoveCurrent()
PlayersBindingSource.EndEdit()
End If
End If

End Sub
=======================================

I can put similar code into the toolbar button just changing the Button5_Click to the name of the toolbar button but I thought it would be better if I 'called' the already written code.

I changed the Private Sub Button5_Click etc. to Public Sub Button5_Click then in the Toolbar button click event I put 'call Sub Button5_Click()'

I get errors stating that ' argument not specified for parameter 'e' ' and can't progress.

I will be using this code in a few other forms so would like to 'call' possible.

Anyone please advise what I'm missing?

Thanks

Jack

ps. There's a reason for calling the button for the original code 'Button5', it's not lazy coding. Thanks.
 
As you can see from your code, Button5_Click has two parameters: 'sender' and 'e'. You can't call that method without passing those arguments. Apart from that, you shouldn't be calling event handlers directly, specifically because you don't have genuine arguments for those parameters. There are two real choices:

1. As it's a Button.Click event handler you can call the PerformClick method of the Button to raise its Click event and invoke the event handler.

2. Move that code out of that event handler and into its own method, then call that method from that event handler and anywhere else you to invoke the code from.
 
Back
Top