[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-05-18
5
Medium Priority
?
267 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:plq
  • 3
  • 2
5 Comments
 
LVL 34

Expert Comment

by:Sancler
ID: 16714094
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
 
LVL 8

Author Comment

by:plq
ID: 16714149
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
 
LVL 34

Accepted Solution

by:
Sancler earned 2000 total points
ID: 16715848
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
 
LVL 8

Author Comment

by:plq
ID: 16716072
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
 
LVL 34

Expert Comment

by:Sancler
ID: 16716187
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question