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
LVL 8
plqAsked:
Who is Participating?
 
SanclerCommented:
This is what I meant

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

    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 mnuActionMenu As System.Windows.Forms.MainMenu
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.mnuActionMenu = New System.Windows.Forms.MainMenu
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Menu = Me.mnuActionMenu
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Dim WithEvents mnu As Windows.Forms.MenuItem

    Private Sub Form1_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

End Class

As I said, I don't know if it works for Mobile.

Roger
0
 
SanclerCommented:
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
0
 
plqAuthor Commented:
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."

0
 
plqAuthor Commented:
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

0
 
SanclerCommented:
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

 
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.