0

Is it possible to do it? For example like,

Private Sub inputno_tb_MouseDown(sender As Object, e As MouseEventArgs)
     'My code
End Sub

Private Sub stacker1apcs_tb_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs)
     'Calling Private Sub inputno_tb_MouseDown
End Sub
stillLearning
  • 84
  • 2
  • 12
  • 3
    Those arent just private methods, they are event handlers. If you want them both to do something similar create a new sub with that code in it and call that from each event – Ňɏssa Pøngjǣrdenlarp Jun 14 '17 at 01:27
  • This is impossible to answer without more information. If you want to give the appearance of an event being called, simply call the `Sub` like any other method, this makes readability more difficult, a better approach would be to have the 2 event handlers call a 3rd 'shared' method to handle both. – Mike Jun 14 '17 at 04:37

1 Answers1

0

It is certainly possible to do, like so:

Private Sub inputno_tb_MouseDown(sender As Object, e As MouseEventArgs)
    'My code
End Sub

Private Sub stacker1apcs_tb_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs)
    inputno_tb_MouseDown(sender, MouseEventArgs.Empty)
End Sub

If you use sender in your code, you might want to update it to stacker1apcs_tb instead, or whatever you prefer.

Magnus
  • 201
  • 1
  • 7
  • *"As long as your code doesn't use the sender or the e object, you can just send Nothing as the arguments."* A pretty significant (and dangerous) assumption. In this case, it looks like the sender is `stacker1apcs_tb`, so why not pass that? And the event args should probably just be `EventArgs.Empty` instead of `Nothing`. – Cody Gray - on strike Jun 14 '17 at 09:49
  • Dangerous? If he knows he isn't using it, then there is nothing to worry about. It's not like he is raising an event that is being handled by any external code. But I'll update my code to include your points.. – Magnus Jun 14 '17 at 10:15
  • Dangerous because the code in the event handler could be updated at any time, by any other programmer, without any reason to believe that it was being called unsafely. I mean, no, it's not like the computer will blow up, but you'll get a crash and you'll have to debug to figure out why, which is a waste of time. Always better to write robust code, rather than fragile code. – Cody Gray - on strike Jun 14 '17 at 10:19
  • Yeah, I didn't consider the fact that it might be a bigger environment with multiple developers, so you are correct. I still haven't really encountered a use of the sender object without having multiple object using the same event handler, and that doesn't seem likely here. But I updated the code to be on the safe side. – Magnus Jun 14 '17 at 10:24