Mike Trethowan

Pen Truth Contributor

To continue with the idea presented in my last two posts, I will show how to group Context Menu item selections under the same event.

Just as with button events this is useful when you have several selections preforming related functions.  One example would be opening up several different forms.

Start a new project and save it.  Rename the main form to frmMain and set the default location to Center Screen from the Properties Panel.  Add three new items to your project by right clicking on the project name in the Solution Explorer and selecting Add  and selecting New Item.  From the template select Windows Form. Name the three forms frmOne, frmTwo and frmThree.  Don’t change the locations of these three forms as we don’t want them opening on top of the Main Form.

Also, add a Context Menu Strip control to the main form from the toolbox and name it cmsForms, then and add a button to the main form and name it btnOpen.  We can use a mouse right click to open the menu, but for now we will use a button click.  Change the button text to Open CMS and double click the button to open the code page.  In the button click event we will add the code to open the context menu:

    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        cmsForms.Show()
    End Sub

The Context Menu Strip doesn’t have any items assigned to it yet.  From the Designer window for the main form click on cmsForms.  cmsForms will open up.  From the properties panel select Items collection and add three menu items and name them cmsFormOne, cmsFormTwo, and cmsFormThree.  Change their text to Open Form One, Open Form Two, and Open Form Three.  Next double click on each to create their click events so that

Public Class frmMain

    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        cmsForms.Show()
    End Sub

    Private Sub cmsFormOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmsFormOne.Click

    End Sub

    Private Sub cmsFormTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmsFormTwo.Click

    End Sub

    Private Sub cmsFormThree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmsFormThree.Click

    End Sub

End Class

Next, edit the code into one event:

Public Class frmMain

    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        cmsForms.Show()
    End Sub

    Private Sub cmsFormOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
    cmsFormOne.Click, cmsFormTwo.Click, cmsFormThree.Click

    End Sub

End Class

Here we will select for a case of matching text. Just as in the last post, to do this we need to discover which object fired the event, or in the programming parlance, the sender. This information is again provided by the statement; ByVal sender As System.Object. So sender is where we find the value that we want to evaluate in our Select Case statement.   In this example we do  not use message boxes as we will be opening forms.

Select Case sender.Name
            Case "cmsFormOne"
                frmOne.Show()
                frmTwo.Close()
                frmThree.Close()
            Case "cmsFormTwo"
                frmOne.Close()
                frmTwo.Show()
                frmThree.Close()
            Case "cmsFormThree"
                frmOne.Close()
                frmTwo.Close()
                frmThree.Show()
        End Select

One last thing before you run your code.  We want to control the location of our context menu.  The Context Menu Strip takes an x,y coordinate to locate the menu, however, we want the menu to open up in relation to our main form and not in relation to the screen.   So to do this we us “Me” for our reference.

    Private Sub btnOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOpen.Click
        cmsForms.Show()
        cmsForms.Show(Me, 20, 20) ' Location of Context Menu
    End Sub

Now you can save your project and run Debug.  if the code ran as expected, when ever you click on Open CNS, the context menu pops up with your three selections.  Each opened up the desired window while closing the previous window.  If you want to try to repeat this exercise using the Mouse Click event, create a new event as follows:

    Private Sub frmMain_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick

        If Windows.Forms.MouseButtons.Right Then
            cmsForms.Show()
            cmsForms.Show(650, 100) ' Location of Context Menu
        End If

    End Sub

This time I left out the reference to “Me” and changed the x,y to 650,100.  This now locates the menu using screen coordinates rather than using form coordinates.  I hope this was helpful, enjoy.

Disclaimer:  The code in this tutorial is not intended as a final product or by no means the only way this project can be coded. The presented code is for educational purposes only and no warranty is granted or implied. I/we are not responsible for any damages as a result of using this code. Use at your own risk.