Event Handling.. Enact a click event for a MenuItem

I need to use a menuitem to go to a movefirst button on a navigation bar. I am just refering this menuitem to do the samething as the button. My sub that this button goes to is:

Private Sub BindingNavigatorMoveFirstItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMoveFirstItem.Click

Tne MenuItem sub that the MenuItem goes to is:

Private Sub FirstToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FirstTo8:07 AM 6/22/20066olStripMenuItem.Click

Again, I just want to go to the MoveFirst event. So, the two are the same function

Comments

  • You don't have to put the handler in the function signiture... instead add handler(s) to the function when the code loads or in an init function. The events must have the same signiture.


    AddHandler ., AddressOf

    'and sometimes you will want to manually remove a handler.

    RemoveHandler ., AddressOf

    ~rlc
  • I not sure how an example would look like. Could you provide an example?

    BaJabba
  • in component init section

    Addhandler BindingNavigatorMoveFirstItem.Click, MySub
    Addhandler FirstTo8:07 AM 6/22/20066olStripMenuItem.Click, MySub


    You sub that handles both

    Private Sub MySub(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'whatever you are doing
    end sub


    ~rlc
  • I have read the MSDN on this Statement. I am not sure about a couple of things.
    1). Do I need to use AdddHandler? I see most of it as something to create new events and utilizing them, not like WithEvents. I have the click events you show me, I just don't know what MySub is referring to. I am thinking the MoveFirst sub that I wish to refer to for both the button and MenuItem.
    2). I , of course, just want to POINT to the sub that has the movenect statement (like using the CALL statement)
    I will read futher. I am being enlightened by sll of this. If you shed some more light on what MySub refers to, I might get it.
    : in component init section

    Thanks

    Bar Jabba
    :
    : Addhandler BindingNavigatorMoveFirstItem.Click, MySub
    : Addhandler FirstTool.Click, MySub
    :
    :
    : You sub that handles both
    :
    : Private Sub MySub(ByVal sender As System.Object, ByVal e As System.EventArgs)
    : 'whatever you are doing
    : end sub
    :
    :
    : ~rlc
    :


  • : 1). Do I need to use AdddHandler? I see most of it as something to create new events and utilizing them, not like WithEvents. I have the click events you show me, I just don't know what MySub is referring to. I am thinking the MoveFirst sub that I wish to refer to for both the button and MenuItem.

    Do you have to... Well no but it is the cleanest way to have two seperate events fire the code... I.E. MySub. You could use the handles statement on the signiture for the function, like you were... then call sub or function that you create which has the code you want to run... These two ways will both produce the same code being runwhen either event is fired.

    : 2). I , of course, just want to POINT to the sub that has the movenect statement (like using the CALL statement)
    : I will read futher. I am being enlightened by sll of this. If you shed some more light on what MySub refers to, I might get it.

    'MySub' Is simply a custom sub or function you are making... The name could be anything I used MySub to indicate it is a sub you are creating. The only requirement you will need is that it must have the same input parameters as the event you are calling if you want to add a handler. You can add as many handlers as you need to one function in this manner. The result is multiple events may call the same sub... Make sense?


    : : in component init section
    :
    : Thanks
    :
    : Bar Jabba
    : :
    : : Addhandler BindingNavigatorMoveFirstItem.Click, MySub
    : : Addhandler FirstTool.Click, MySub
    : :
    : :
    : : You sub that handles both
    : :
    : : Private Sub MySub(ByVal sender As System.Object, ByVal e As System.EventArgs)
    : : 'whatever you are doing
    : : end sub
    : :
    : :
    : : ~rlc
    : :
    :
    :

  • It does make sense. I understand the Syntax. I do not have a Sub of my own making, Except for the signatures for the Click event. I am trying the AddHandler at the top of my frmStudentInfo Class. I still get a sytax error. The sytax I am using is:

    AddHandler NextToolStripMenuItem,BindingNavigatorMoveNextItem

    Are you saying I need to cut and paste the parameters for these event handlers? Like:

    BindingNavigatorMoveNextItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Am I antwhere close in my thinking?

    Thanks for explaining all this to me.

    Bar Jabba
  • This should work.... Put the add handlers either in the forms "load" event or after this line in the pre-grenerated code. " ' Add any initialization after the InitializeComponent() call"

    AddHandler NextToolStripMenuItem, BindingNavigatorMoveNextItem

    Private Sub BindingNavigatorMoveNextItem(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    : It does make sense. I understand the Syntax. I do not have a Sub of my own making, Except for the signatures for the Click event. I am trying the AddHandler at the top of my frmStudentInfo Class. I still get a sytax error. The sytax I am using is:
    :
    : AddHandler NextToolStripMenuItem,BindingNavigatorMoveNextItem
    :
    : Are you saying I need to cut and paste the parameters for these event handlers? Like:
    :
    : BindingNavigatorMoveNextItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    :
    : Am I antwhere close in my thinking?
    :
    : Thanks for explaining all this to me.
    :
    : Bar Jabba
    :

  • I think I am getting close to the solution. I put the following in my Form_Load Event:
    AddHandler BindingNavigatorMoveNextItem.Click, NextToolStripMenuItem_Click()

    I get an eror message for the "NextToolStripMenuItem_Click()" portion of the statement. I really thought this would be right. Other permutations I did , all with errors:

    AddHandler NextToolStripMenuItem.Click, BindingNavigatorMoveNextItem.Click
    (I do not think this is correct, also, because of it being the wrong delegate. I am actually confused as which would be a delegate.)
    AddHandler BindingNavigatorMoveNextItem.Click, NextToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

    I am looking into putting this in the StudentInfo.Designer.vb where the IComponent section is. There are a lot of "WithEvents" there, so, that Might be where to put it.

    I appreciate all this help, I am learning a lot...I love it!
    Any thoughts?

    Bar Jabba



  • Made a test app here is the code...

    [code]
    Public Class Form1
    Inherits System.Windows.Forms.Form

    #Region " Windows Form Designer generated code "

    Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
    AddHandler MenuItem2.Click, AddressOf HandlesBoth
    AddHandler Button1.Click, AddressOf HandlesBoth

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
    If Not (components Is Nothing) Then
    components.Dispose()
    End If
    End If
    MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.
    'Do not modify it using the code editor.
    Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu
    Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem
    Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Private Sub InitializeComponent()
    Me.MainMenu1 = New System.Windows.Forms.MainMenu
    Me.MenuItem1 = New System.Windows.Forms.MenuItem
    Me.MenuItem2 = New System.Windows.Forms.MenuItem
    Me.Button1 = New System.Windows.Forms.Button
    Me.SuspendLayout()
    '
    'MainMenu1
    '
    Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1})
    '
    'MenuItem1
    '
    Me.MenuItem1.Index = 0
    Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2})
    Me.MenuItem1.Text = "someItem"
    '
    'MenuItem2
    '
    Me.MenuItem2.Index = 0
    Me.MenuItem2.Text = "event"
    '
    'Button1
    '
    Me.Button1.Location = New System.Drawing.Point(56, 48)
    Me.Button1.Name = "Button1"
    Me.Button1.TabIndex = 0
    Me.Button1.Text = "Button1"
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 266)
    Me.Controls.Add(Me.Button1)
    Me.Menu = Me.MainMenu1
    Me.Name = "Form1"
    Me.Text = "Form1"
    Me.ResumeLayout(False)

    End Sub

    #End Region



    Private Sub HandlesBoth(ByVal sender As System.Object, ByVal e As System.EventArgs)
    MsgBox("Works for either")
    End Sub
    End Class
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion