Dynamic ContextMenu

RayGlover

New member
Joined
Jul 16, 2009
Messages
2
Programming Experience
10+
I have a ContextMenu attached to a tray notifyIcon and I already have it on the form in design mode. If I look at it in design mode I can see evereything that I have on the main pop-up context menu. What I am trying to do is add to one of those choices programatically. Worked fine in VB6.

I am reading in a list of filenames from a directory and want to place them as menu items in the 2nd level of the context menu.
Example:

Show Quote Window Main Context Menu Item
Always On Top Main Context Menu Item
--Seperator--
.
.
.
Choose RQG data file(s) Main Context Menu Item
Aristotle.txt dynamically loaded
Mark Twain.txt user can check or un-check
Will Rogers.txt each of these
Automatic change interval
.
.
.
Exit

Primary problem is what changed in VB.NET from VB6 that broke my program and what do I need to do to fix the dynamic build.
I will include the part where i am getting errors -

VB.NET:
    Public Sub BuildChooseFileMenuItems()
        Dim k As Integer
        Dim numChecked As Integer
        Dim RandomSelectEnabled As Boolean = False
        ReDim ChooseFileMenu(UBound(QuoteFiles) + 1)
        numChecked = 0
        For k = 0 To UBound(QuoteFiles)
            ChooseFileMenu(k) = Me.mnuChooseRQGFiles.MenuItems.Add(QuoteFiles(k), AddressOf mnuChooseRQGFiles_Click)
            ChooseFileMenu(k).Enabled = True
            If QuoteFilesChecked(k) = "T" Then
                ChooseFileMenu(k).Checked = True
                numChecked = numChecked + 1
            Else
                ChooseFileMenu(k).Checked = False
            End If
            If RandomSelectEnabled = True Then
                If ChooseFileMenu(k).Text <> "-" Then
                    ChooseFileMenu(k).Enabled = False
                End If
            End If
            If ChooseFileMenu(k).Text = "Random Select" Then
                If ChooseFileMenu(k).Checked = True Then
                    RandomSelectEnabled = True
                End If
            End If
        Next
        If numChecked < 1 Then
            ChooseFileMenu(0).Checked = True
            QuoteFilesChecked(0) = "T"
            For k = 2 To UBound(QuoteFiles) - 1
                ChooseFileMenu(k).Enabled = False
            Next
        End If
    End Sub

and the on-click handler

VB.NET:
    Private Sub mnuChooseRQGFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuChooseRQGFiles.Click
        Dim miClicked As MenuItem = CType(sender, MenuItem)
        Dim m As Integer
        For m = 0 To UBound(QuoteFiles)
            If QuoteFiles(m) = miClicked.Text Then
                If miClicked.Checked = True Then
                    miClicked.Checked = False
                    QuoteFilesChecked(m) = "F"
                Else
                    miClicked.Checked = True
                    QuoteFilesChecked(m) = "T"
                End If
            End If
        Next
        If miClicked.Text = "Random Select" Then
            If miClicked.Checked = False Then
                For m = 2 To UBound(QuoteFiles) - 1
                    ChooseFileMenu(m).Enabled = True
                Next
            Else
                For m = 2 To UBound(QuoteFiles) - 1
                    ChooseFileMenu(m).Enabled = False
                Next

            End If
        End If
        Open_QuoteFile()
    End Sub

The error I am getting in Visual Studio is -
NullReferenceException was unhandled
Object reference not set to an instance of an object.
Exception detail -
System.NullReferenceException was unhandled
Message="Object reference not set to an instance of an object."
Source="RQG"
StackTrace:
at RQG.RQGForm.BuildChooseFileMenuItems() in C:\Documents and Settings\Ray\Desktop\RQG\frmQuotes.vb:line 671
at RQG.RQGForm.RQGForm_Load(Object eventSender, EventArgs eventArgs) in C:\Documents and Settings\Ray\Desktop\RQG\frmQuotes.vb:line 463
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.SafeNativeMethods.ShowWindow(HandleRef hWnd, Int32 nCmdShow)
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at RQG.RQGMainModule.Main() in C:\Documents and Settings\Ray\Desktop\RQG\RQGMainModule.vb:line 61
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:

Thanks,
Ray
 
Your stack trace has given you 3 lines to check. Debug with a breakpoint and mouse over the diff objects as you step into the code - it will show you the values or lack there of. Maybe this will give some clues.
 
I have checked those lines in the stack trace

I have checked everything in the stack trace and can't find a problem. All of the types and values seem to be correct. Following are the elements of the line where the error occurs and their types.

Please, if anyone has any idea why this is not working in VB.NET, but was working in VB6, give me some guidance.

It seems a simple thing to do. I just need to process the ChooseFilesMenu in a loop, because there are a variable number of them that go on the sub-menu of the NotifyIcon ContextMenu menu item choose files.

If you know of another way of doing what I am trying to do I open to suggestion. I have looked at ContextMenuStrip but I am not sure it would work any diffently from this, and I am not sure what it would look like when displayed


ChooseFileMenu() -
Public ChooseFileMenu System.Windows.Forms.MenuItem
The data in the array are all "Nothing" which I would expect since I am buildingit.

Me - Class RQGForm

mnuChooseRQGFiles - Friend WithEvents mnuChooseRQGFiles As System.Windows.Forms.MenuItem

MenuItems - Public ReadOnly Property MenuItems() As System.Windows.Forms.Menu.MenuItemCollection

Add - Public Overridable Function Add(caption As String, onClick As System.EventHandler) As System.Windows.Forms.MenuItem

QuoteFiles() - QuoteFiles() As String
The values are the .txt files names, also expected.

k, the subscript, is zero. Also expected since it is erroring out on the first loop.

mnuChooseRQGFiles_Click - Private Sub mnuChooseRQGFiles_Click(sender As Object, e As System.EventArgs)
 

Latest posts

Back
Top