Search KB Filter article types
ContextMenu CodeSample - Dynamic ContextMenu on a ListView
Categories: Menus, Lists & Grids, Skinnable and Inherited Controls
Tags: Developers, Navigation, 1. Beginner, 2. Intermediate, Navigation, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
Revision: 1
Posted: 11/Dec/2009
Updated: 23/July/2010
Status: Publish
Types: Code

This article will have a few sections added to it soon, based on the following article type skeleton: CodeSample
Overview

This CodeSample will demonstrate how you can use the ContextMenu.PopUp() event, along with ContextMenu.Show() to create dynamic ContextMenuListViewItems. 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 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 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 highlightsRererencesCode samples



About the author

Related Articles

Menus  
Title Update Author
This video demonstrates how to create and use a context menu in Visual WebGui.
Tags: Developers, 1. Beginner, Pre v6.3, v6.3, v6.4 and Later, 2. Intermediate, 3. Advanced
01/Jan/2009    2009/01/01
Tags: Developers, Navigation, 1. Beginner, 2. Intermediate, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
23/July/2010    2010/07/23
Tags: Developers, Navigation, 1. Beginner, 2. Intermediate, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
23/July/2010    2010/07/23
Tags: Architects, Developers, Navigation, 1. Beginner, 2. Intermediate, 3. Advanced, Navigation, Pre v6.3, v6.3, v6.4 and Later
04/Dec/2010    2010/12/04
Tags: Architects, Developers, Navigation, 1. Beginner, 2. Intermediate, 3. Advanced, Pre v6.3, v6.3, v6.4 and Later
04/Dec/2010    2010/12/04
Tags: Developers, 1. Beginner, 2. Intermediate, Customization, Pre v6.3, v6.3, v6.4 and Later, 3. Advanced
24/May/2010    2010/05/24
.NET Web, Cloud and Mobile application delivery platform | Sitemap | Terms of Use | Privacy Statement | Copyright © 2005-2011 Visual WebGui®       Visual WebGui weblog on ASP.NET Visual WebGui Group on LinkedIn Visual WebGui updates on Twitter Visual WebGui Page on Facebook Visual WebGui YouTube Channel Visual WebGui Platform News RSS