0

I want to make a public sub in my module to to store a repeating procedure. Specifically key press events.

Private Sub txtPass_KeyPress(sender As Object, e As KeyPressEventArgs) _
    Handles txtPass.KeyPress

    FunctionKeys(Me, sender, e)

End Sub

Public Sub  FunctionKeys(form as object, sender as object, _
    e as KeyPressEventArgs)

    With form
        If e.KeyChar = ChrW(Keys.Enter) Then .btnOk_Click(sender, e)
        If e.KeyChar = ChrW(Keys.Escape) Then .btnClose_Click(sender, e)
    end with

End sub

I guess it would look something like this. Unfortunately, this tells me it can't find a public sub for formname.btnok_click etc. I want to know if there's a way around this. Looking around the net I found I can use the AcceptButton and CancelButton property. But only if i actually have a button to press. My MDI does not have buttons. Just menu. Also, I'm aware I can use formname.close() for the Keys.Escape. But I'd still have a problem with the OK button.

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
YinYangKim
  • 31
  • 3
  • 12
  • 1
    The event handler should not be private. It can call private methods within the same scope, but the event handler itself needs to be visible (protected or public) – Allan S. Hansen Jun 24 '14 at 09:03

1 Answers1

0

You can make btnOk_Click and btnClose_Click public on all your forms and that'll work.

Alireza
  • 4,976
  • 1
  • 23
  • 36
  • Is there any drawback to this? I've thought about that actually but making a sub public might let me make the mistake of calling it somewhere else if i forget that i made it public. Any drawbacks aside from this? – YinYangKim Jun 24 '14 at 10:06
  • Tell me something: should you call btnOk_Click from the outside for just one form? Or there are many forms that `FunctionKeys` can call btnOk_Click on them? – Alireza Jun 24 '14 at 10:35
  • I don't think I'd have multiple forms accessible at one point. Right now most forms run at .ShowDialog(), making all forms inaccessible until I close the most recent one. Is this a problem? – YinYangKim Jun 24 '14 at 10:54
  • Just make the methods public and you will be fine. Maybe better to give them new names. Just for clarification. – Alireza Jun 24 '14 at 11:12
  • Guess that's worth a try. Thanks! – YinYangKim Jun 24 '14 at 11:27