Visual C#. NET 2003 Unleashed

This next section is all about dynamic context menus. You might think that context menus are a small little piece of Windows Forms, and they're easy to doso why bother dedicating a whole section to them in a chapter on advanced graphical user interfaces?

As you will see in the next section, the reason is that contextual functionality is often overlooked by many application developers. This section will show you how to create dynamic context menus and where they are appropriate.

Introduction to Contextual, Adaptive User Interfaces

Contextual functionality isn't just about making sure that you can right-click a control. It is also about providing relevant functionality that is related to whatever the user is working on.

If a user is editing a contact, and that contact can appear in 10 different other areas of that application, the application should provide some kind of contextual menu either through a ContextMenu (right-click menu) or through some other kind of dynamically changing menu that directs the user to those other areas. If your application also stores things such as email addresses, phone numbers, and personal information on a contact, but you don't have room to display all that information on the screen at once, a contextual menu or navigational system of some kind should be provided to get the user to those related areas.

The bottom line is that within the context of whatever action the user is performing, he should be able to perform any other tasks that are relevant to that action without requiring a significant amount of effort on his part. This is usually accomplished with contextual menus that appear when you right-click something that you are working on.

A Sample Dynamic Context Menu in Action

The next code sample is more an illustration of a design pattern than it is coding. The code is fairly simple, but the pattern it uses to provide dynamic contextual functionality is something that every Windows Forms application can benefit from.

To create this sample, follow the following steps:

1.

Create a new Windows Forms application called DynamicContext.

2.

Drop a ListView control onto the form and call it lvUsers.

3.

Add the following columns to the ListView and place it in Report mode: Name, Address, Email.

4.

Next, drop a ContextMenu onto the form and call it cmUserContext.

5.

Add the following items to it: Assign Work, View Details, Send Email.

6.

Associate the contextual menu with the list view by setting the list view's ContextMenu property to cmUserContext.

You should be ready to write some code for the dynamic context menu. Rig up an event handler (click the lightning-bolt icon in the Properties window and double-click the empty space next to the event name) for the Popup event. The code for the Popup event is shown in Listing 17.3.

Listing 17.3. The Pop-up Context Menu Event Handler

private void cmUserContext_Popup(object sender, System.EventArgs e) { if (lvUsers.SelectedItems.Count == 0) Disable_AllItems(); else { Reset_DefaultItems(); // evaluate which items to show based on the currently selected user cmiAssignWork.Enabled = (bool)lvUsers.SelectedItems[0].Tag; if (lvUsers.SelectedItems[0].SubItems[2].Text == "") cmiSendEmail.Enabled = false; } } private void Reset_DefaultItems() { foreach (MenuItem mni in cmUserContext.MenuItems) { mni.Enabled = true; } } private void Disable_AllItems() { foreach (MenuItem mni in cmUserContext.MenuItems) { mni.Enabled = false; } }

There are a couple of $I~pop-up context menu event handler>things going on in this pop-up handler. If there is no selected item, there can be no user context, so all the context menu's menu items need to be disabled. If there is a user context, the menu items are first restored to their default position. Our sample has them all enabled, but the default position for your application could be anything; it could even be determined by a database query.

Finally, the code examines the selected ListViewItem. If the Tag property evaluates to true, the Assign Work menu item is enabled. The Send Email menu item is enabled only if the current user's email address isn't blank.

Before running the application, set up the following code (shown in Listing 17.4) for the form's constructor to populate some dummy values into the ListView.

Listing 17.4. Populating the ListView with Sample Values

public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // SetupFakeUsers(); } private void SetupFakeUsers() { ListViewItem lvi = null; lvUsers.Items.Clear(); lvi = new ListViewItem(); lvi.Text = "Joe User"; lvi.SubItems.Add( "1 Joe St"); lvi.SubItems.Add("joe@joe.com"); lvi.Tag = true; lvUsers.Items.Add( lvi ); lvi = new ListViewItem(); lvi.Text = "John Doe"; lvi.SubItems.Add("???? Somewhere St"); lvi.SubItems.Add("anonymous@somewhere.com"); lvi.Tag = false; lvUsers.Items.Add( lvi ); lvi = new ListViewItem(); lvi.Text= "Jane Doe"; lvi.SubItems.Add("1 Jane St"); lvi.SubItems.Add(""); lvi.Tag = true; lvUsers.Items.Add( lvi ); }

Run the application and right-click in various places on the ListView control. You should notice that this kind of dynamic control gives the user a feeling of freedom. Rather than having to fumble through toolbar icons he might not recognize, the user can right-click anything he likes and intuitively know that he will be presented only with relevant options. After the user knows that your application supports context menus, he will immediately begin trying to right-click in other areas as he finds your application easier to use and navigate.

    Категории