Jump to content


Photo

Add Event Handler to Dynamic ContextMenu Vb.net 2010


  • Please log in to reply
2 replies to this topic

#1 timdav83

timdav83

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 28 March 2011 - 01:39 AM

I can add sub menu items from an xml document, however adding proper code to handle on click events of the sub menu items is confusing me. First off, whenever I use addHandler ViewAlarms.DropDownItemClicked, AddressOf ViewAlarms_Click, the message box displays twice. Second, how do I display the nameAttribute in the message box which is commented out? Lastly, how do you hide the form? The current method does not work. An icon still displays in taskbar which can be maximized.

Imports System
Imports System.Xml
Imports System.IO

Public Class Main

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'Hides the Main Form
        Me.WindowState = FormWindowState.Minimized
        Me.Hide()

        'Handles Notify Icon
        Dim AlarmIcon As New NotifyIcon()
        AlarmIcon.Icon = New System.Drawing.Icon("C:\Users\Tim\Pictures\alarm.ico")
        AlarmIcon.Visible = True
        AlarmIcon.Text = "Test"

        'Alarm Context Menu
        Dim AlarmMenu As New ContextMenuStrip()
        Dim AddAlarm As ToolStripMenuItem = AlarmMenu.Items.Add("Add Alarm")
        Dim ViewAlarms As ToolStripMenuItem = AlarmMenu.Items.Add("View Alarms")

        Dim xmlDoc As XmlDocument = New XmlDocument()
        Dim AlarmNodes As XmlNodeList = xmlDoc.SelectNodes("/alarms/alarm")
        'Create the XML Document

        'Load the Xml file
        xmlDoc.Load("C:\Users\Tim\Documents\Alarm\alarm.xml")
        'Get the list of name nodes 

        'Loop through the nodes
        For Each AlarmNode As XmlNode In AlarmNodes
            'Get the Name Attribute Value
            Dim nameAttribute = AlarmNode.Attributes.GetNamedItem("name").Value
            'Add Name Attribute As Sub Item in ViewAlarms
            ViewAlarms.DropDownItems.Add(nameAttribute)

            'Add handler to Menu
            AddHandler ViewAlarms.DropDownItemClicked, AddressOf ViewAlarms_Click
        Next

        'Complete Context Menu
        Dim AlarmExit As ToolStripMenuItem = AlarmMenu.Items.Add("&Exit")

        'Links Tray Icon AlarmIcon to Context Menu Strip AlarmMenu
        AlarmIcon.ContextMenuStrip = AlarmMenu

    End Sub

    Sub ViewAlarms_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        'MessageBox.Show()
    End Sub
End Class



#2 timdav83

timdav83

    Young Padawan

  • Members
  • Pip
  • 17 posts

Posted 28 March 2011 - 12:59 PM

Solved first and second question I had. However, still having issues with hiding the form.

        For Each AlarmNode As XmlNode In AlarmNodes
            'Get the Name Attribute Value
            Dim getAlarm As New ToolStripMenuItem
            Dim nameAttribute = AlarmNode.Attributes.GetNamedItem("name").Value
            getAlarm.Text = nameAttribute

            'Add Name Attribute As Sub Item in ViewAlarms
            ViewAlarms.DropDownItems.Add(getAlarm)
            'Add Handler
            AddHandler getAlarm.Click, AddressOf ViewAlarms_Click
        Next
  Sub ViewAlarms_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        MessageBox.Show(CType(sender, ToolStripMenuItem).Text)
    End Sub

Edited by timdav83, 28 March 2011 - 01:02 PM.


#3 JoeyMagz

JoeyMagz

    Young Padawan

  • Members
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Chesapeake, VA

Posted 01 April 2011 - 10:23 PM

add "Me.ShowInTaskbar = false" underneath "Me.Hide()" that should solve your final problem.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users