ContextMenu CodeSample - Dynamic ContextMenu on a ListView

From Visual WebGui Wiki

Jump to: navigation, search


Contents

Overview

This CodeSample will demonstrate how you can use the ContextMenu.PopUp() event, along with ContextMenu.Show() to create dynamic ContextMenu on ListViewItems. The dynamic ContextMenus are created on demand, when a ListViewItem is Right-Clicked.


Samples of use

For the complete code, please view the demo application. The piece of code shown here, is the event handler for the ContextMenu.Popup event, which handles the dynamic creation of the ContextMenus and shows it.

VB.NET Code

Private Sub ContextMenu1_Popup(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup
        Dim cm1 As Gizmox.WebGUI.Forms.ContextMenu = New Gizmox.WebGUI.Forms.ContextMenu
        Dim mi1 As Gizmox.WebGUI.Forms.MenuItem = New Gizmox.WebGUI.Forms.MenuItem
        Dim mi2 As Gizmox.WebGUI.Forms.MenuItem = New Gizmox.WebGUI.Forms.MenuItem
        cm1.MenuItems.AddRange(New Gizmox.WebGUI.Forms.MenuItem() {mi1, mi2})
        mi1.Index = 0
        mi1.Text = "A " + DateTime.Now.ToString()
        mi2.Index = 1
        mi2.Text = "B " + DateTime.Now.ToString()
        If ListView1.SelectedItem Is Nothing Then
            cm1.Show(ListView1, New System.Drawing.Point(0, 0), DialogAlignment.Below)
        Else
            cm1.Show(ListView1.SelectedItem, New System.Drawing.Point(0, 0), DialogAlignment.Below)
        End If
 
    End Sub

C# Code

private void contextMenu1_Popup(object sender, EventArgs e)
        {
            Gizmox.WebGUI.Forms.ContextMenu  cm1 = new Gizmox.WebGUI.Forms.ContextMenu() ;
            Gizmox.WebGUI.Forms.MenuItem mi1 = new Gizmox.WebGUI.Forms.MenuItem();
            Gizmox.WebGUI.Forms.MenuItem mi2 = new Gizmox.WebGUI.Forms.MenuItem();
            cm1.MenuItems.AddRange(new Gizmox.WebGUI.Forms.MenuItem[] {mi1,mi2});
 
            mi1.Index = 0;
            mi1.Text = "A " + DateTime.Now.ToString();
            mi2.Index = 1;
            mi2.Text = "B " + DateTime.Now.ToString();
            if (ListView1.SelectedItem == null ) 
                {
                    cm1.Show(ListView1, new System.Drawing.Point(0, 0), DialogAlignment.Below);
                }
                else
                {
                    cm1.Show(ListView1.SelectedItem, new System.Drawing.Point(0, 0), DialogAlignment.Below);
                }
 
        }

The demo application

A small trick is used in the sample to make the ContextMenu.PopUp getting fired. If you do not assign any ContextMenu to a ListViewItem, no popup menu will fire. So, by creating an empty ContextMenu and assigning that same ContextMenu to the ListViewItem, the event gets fired. You will not use that ContextMenu at all for other purposes, and you should leave it empty, else you might get flickering in your app, and need to take care of hiding it, right after it is shown, to get your dynamic menu shown instead.

SDK Version highlights

Rererences

Code samples



Personal tools