Link to home
Start Free TrialLog in
Avatar of plq
plqFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Windows Mobile New MenuItem() doesn't let me add an eventhandler

This is a VB.NET2005 windows mobile project. In this code:

        Dim mnu As New Windows.Forms.MenuItem()         '"&Test", New EventHandler(AddressOf ActionMenuItem_Click))

        mnu.Text = "Test Menu"
        mnuActionMenu.MenuItems.Add(mnu)

.. the commented out bit won't compile because MenuItem only supports new with no parameters.

It seems that new MenuItem(..) does support parameters in windows but not mobile

So how can I attach an eventhandler to the new menu item so that it runs code when its clicked ?

thanks
Avatar of Sancler
Sancler

I don't know Mobile at all, so this may be way off beam.  But how about - at form level -

   Dim WithEvents mnu As Windows.Forms.MenuItem()

and then - where your current code is -

        mnu = New Windows.Forms.MenuItem()
        mnu.Text = "Test Menu"
        mnuActionMenu.MenuItems.Add(mnu)

In "ordinary" VB.NET that would allow me to code the control's click event even before I'd created an instance.

Roger
Avatar of plq

ASKER

OK I put the dim at form level (and took it out of form_load) put this in form_load...

        mnu = New Windows.Forms.MenuItem()
        mnu.Text = "Test Menu"
        mnu.Enabled = True
        mnu.Click += New EventHandler(AddressOf ActionMenuItem_Click)
        mnuActionMenu.MenuItems.Add(mnu)

and mnu.Click is showing a syntax error with tooltip..

   "Public Event Click (....) is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event."

ASKER CERTIFIED SOLUTION
Avatar of Sancler
Sancler

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of plq

ASKER

Brilliant - it works. Thank you for your effort there. I didn't know about AddHandler. Been doing .net for a couple of years years but always in dlls not forms/mobile !

thanks again
Paul

Paul

Glad it helped.  Here's a revision that might help, too.

    Private Sub Form3_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        mnu = New Windows.Forms.MenuItem
        mnu.Text = "Test Menu"
        mnu.Enabled = True
        'AddHandler mnu.Click, AddressOf ActionMenuItem_Click
        mnuActionMenu.MenuItems.Add(mnu)
    End Sub

    'Private Sub ActionMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    '    MsgBox("Menu test OK")
    'End Sub

    Private Sub mnu_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnu.Click
        MsgBox("Menu test OK")
    End Sub

Once you've added a WithEvents declaration for a control Class at form level, the Class appears in the left hand dropdown in the code window and its events appear in the right hand dropdown.  So, rather than having to use AddHandler, you can simply click on the event in the right hand dropdown and code directly into the sub that appears.  There is, of course, a limitation to this approach, because all instances of the Class that you then create with New in the body of the code will have the same code for their click event.  But you can get round that by doing a Select Case within the sub based some property of sender.

It's an alternative.  Sometimes useful.

Thanks for the points.

Roger