Refering to other sub in Visual Basic

How to you refer to another sub in VB

I want to be able to press Enter and have a certain sub initiate, that has already been defined as handling a click, without having to rewrite all the code again.

A perfect example of this is in Calculator, when you press Enter it does the same thing as when you click the = button.

I had a look in the properties of the button that, when clicked, is handled the sub that I'm trying to initiate, but to no avail.

If there is a way to give a button an accesskey, please let me know.

Otherwise, here's an illustration of what I'm trying to achieve.

Private Sub btn_b_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_b.Click

<All the code that I want to initiate goes in here>

End Sub

Private Sub some_other_sub...

If e.KeyCode = Keys.Enter Then

<Initiate Sub btn_b_Click> ' What do I put here

End If

End Sub

Cheers



Answer this question

Refering to other sub in Visual Basic

  • DeBiese

    Renee and Jason both gave you the answer.

    Renee showed you how to call another sub and Jason showed you a method where you can have a single routine check for two different buttons being clicked ( not at the same time of course) and if you have code that needs to be executed for both buttons you can have it all in a single place. The btn_b.PreformClick() just starts btn_b's sub as if the button itself had been clicked.

    Hope this clears things up a bit for you.

    james

    aka:Trucker


  • tarmac321

    Aye, I noticed that after I posted and edited with the PerformClick method. Sorry 'bout that.

  • Iyana

    I think there was a problem with jason's response.

    He was right about the event handlers having the same arguments, which in Dot Net language is called the same signatures.

    The two handlers do not have similar signatures. I checked before I responded ot I would have shown that to you.



  • settinghead

    If both subs have the same arguments then you could just add the new handler.

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

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    End Sub

     

    Combined:

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

    End Sub

    Notice the ", Button2.Click" at then end of the first line.  That will allow this sub to handle both button1's and button2's clicks.

    Or if you would like to fire the event of the button click you can just use this:

    <button_name>.PerformClick



  • Vhii

    Thank you so much your submissions everyone, I've extracted what I wanted, and here's my solution.

    Private ent As Boolean

    Private Sub btn_b_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_b.Click
    'Code to be run
    End Sub

    Private Sub txt_box_KeyDown...
    If e.KeyCode = Keys.Enter Then
    ent = True
    End Sub

    Private Sub txt_box_KeyPress...
    If ent = True Then
    btn_b.PerformClick() 'Short and sweet :)
    End If
    End Sub

  • Paul Woodgate

    Jason, I think you're still missing something.

    "I want to be able to press Enter and have a certain sub initiate"

    The requester doesn't want two buttons. He wants a button and textbox,box Enter Key Event handlers.

    The two do not have the same signature.



  • Dreamwinter

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As _

    System.EventArgs) Handles Me.Load

    'The following properties must be set thusly:

    TextBox1.Multiline = True

    TextBox1.AcceptsReturn = True

    End Sub

    Private Sub Textbox1_KeyPress(ByVal sender As Object, ByVal e As _

    System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    If e.KeyChar = Chr(Keys.Enter) Then

    BobTheSubRoutine()

    e.Handled = True

    End If

    End Sub

    Private Sub BobTheSubRoutine()

    MsgBox("Hi I'm Bob")

    End Sub

    End Class



  • mol233

    replace "<Initiate Sub btn_b_Click> ' What do I put here " with btn_b.PerformClick()

  • Refering to other sub in Visual Basic