I've recently been working on an application that is being converted from a green-screen program to a Windows Form. In place of a subfile, I am using the Microsoft DataGridView control that is provided with the .NET Framework 2.0. I quickly noticed the need to provide for an "option" capability, similar to subfile support. Subfile options are typically displayed on the left side of the subfile and are usually 2-position alpha or numeric options. The options for a subfile are usually described above the subfile; when there are many options, it is not unusual to have to devote two or three lines of the record format to the text describing the options.
Although it would be possible to provide the same feature in the DataGridView, I decided to see if there were a more Windows-like technique. I found that I could create a pop-up menu that appears when I right-click on a row in the DataGridView. Figure 1 [2] shows an example of the pop-up menu. All the menu options were previously used on the subfile. As you can see, there are a lot of menu options; on the subfile, three lines of the record format were needed to display the options, and most of the options were cryptically abbreviated.
Before going on with the description of this technique, I want to point out that I purposely limited the DataGridView to a single row selection at a time. That means that any menu option I select will only apply to the selected row. This is unlike System i subfiles in which you can key subfile options on as many subfile rows as you want (for example, using option 4 to delete a list of spool files). However, in the application I am working with, there is no requirement to allow for multiple option specification.
After laying out the DataGridView and getting it to display some data, I then started to work with the ContextMenuStrip control. That control is located in the Visual Studio 2005 Toolbox in the Menus & Toolbars section, as shown in Figure 2 [3]. You drag-and-drop the tool from the Toolbox onto your Windows form, at which point Visual Studio relocates the tool to the tool strip shown below your form (see Figure 3 [4]). The tool strip is used to visually represent tools that, at run-time, don't have a visual appearance. For example, Figure 3 [5] shows several tools used for database operations. The ContextMenuStrip is included in the toolstrip because it is not visible on the Windows form until you make it visible.
After you're on the tool strip, click the "smart tag" (the small arrow icon in the upper right corner of the tool) to open the ContextMenuStrip Tasks menu. On that menu, click the Edit Items menu item to go to the Items Collection Editor shown in Figure 4 [6].
The Items Collection Editor is relatively easy to work with, despite the abundance of properties displayed for menu items. In Figure 4 [7], you first click the Add button to add a new MenuItem to the list of members on the menu. You then set the Text property, which is the text that is displayed on the menu. The Items Collection Editor will assign a name property for the menu item, which you can change if you want to. The name is important because it will be used in your code to determine which menu item is clicked.
As shown in Figure 1 [8], you can also set other features of the menu. For example, I set some of the menu items to bold text and also included menu separators to show groups of menu items. You use the properties section of the Items Collection Editor to set properties for each menu item. You use the Add button with the ToolStrip Separator item selected to add the separators. You can use the up and down arrows and the delete button in the Items Collection Editor to move menu items and separators into position as you are developing the menu.
An alternative to using the Items Collection Editor is to directly type the menu, as shown in Figure 5 [9]. When you select the ContextMenuStrip item in the tool strip, Visual Studio displays the menu on top of the form you're working with. You can type into the "Type Here" space and use the drop-down list to select the type of item (menu item or separator). You can't set the properties of a menu item using the direct entry technique, but you can quickly get a pop-up menu laid out.
Figure 6 [10] shows a Visual Basic code excerpt to display the menu in response to a right-click on the DataGridView and to handle the click event for a menu item.
To get the menu to display as a pop-up when I click on a cell in the DataGridView, I use the code shown in DataGridView1_CellMouseClick. The code in the subroutine first determines if the right mouse button was clicked. If it was, the previous current row's Selected property is set to False, and the row that I right-clicked on is set to Selected. The "e" object passed to the subroutine is based on the DataGridViewCellMouseEventArgs class. That class provides the Button property that lets me determine which mouse button was clicked and the RowIndex property to indicate which row the mouse was on when clicked.
After setting the CurrentCell property (to highlight the cell that was right-clicked), I use the Show method on the ContextMenuStrip1 object to display the pop-up menu. The Show method used here takes two parameters: DataGridView1 to indicate which control the menu applies to and the e.Location property to indicate where on the form the menu is to be displayed (at the location of the mouse when clicked). After executing the Show method, the menu is displayed, as seen in Figure 1 [11].
It is easy to generate the code for the click event for a menu item: with the menu visible as shown in Figure 5 [12] (at design time), double-click on the menu item for which you want to write code. As with other controls, Visual Studio generates the code to handle the click event. For example, the DetailToolStripMenuItem_Click subroutine shown in Figure 6 [13] is the code to process the Detail item (the first item on the menu) when I click it at runtime.
Pop-up menus have been around in GUIs for a number of years. If you are want to develop applications to replace green-screen programs, you may want to consider using the technique described in this article as an alternative to the subfile options technique. Users who are experienced with Windows will find it easy to use pop-up menus because they most likely have seen them in other Windows applications they are familiar with.
Craig Pelkie has worked as a programmer with IBM midrange computers for many years. He has also written and lectured extensively on AS/400 and System i technologies, including client/server programming, Client Access, Java WebSphere, .NET applications for the System i, and web development. You can reach him at cpelkie@systeminetwork.com.
Links:
[1] http://systeminetwork.com/author/craig-pelkie
[2] http://pentontech.com/IBMContent/Images/article/55906_50497_cms001.jpg
[3] http://pentontech.com/IBMContent/Images/article/55906_50498_cms002.jpg
[4] http://pentontech.com/IBMContent/Images/article/55906_50499_cms003.jpg
[5] http://pentontech.com/IBMContent/Images/article/55906_50499_cms003.jpg
[6] http://pentontech.com/IBMContent/Images/article/55906_50500_cms004.jpg
[7] http://pentontech.com/IBMContent/Images/article/55906_50500_cms004.jpg
[8] http://pentontech.com/IBMContent/Images/article/55906_50497_cms001.jpg
[9] http://pentontech.com/IBMContent/Images/article/55906_50501_cms005.jpg
[10] http://pentontech.com/IBMContent/Documents/article/55906_413_cms006.txt
[11] http://pentontech.com/IBMContent/Images/article/55906_50497_cms001.jpg
[12] http://pentontech.com/IBMContent/Images/article/55906_50501_cms005.jpg
[13] http://pentontech.com/IBMContent/Documents/article/55906_413_cms006.txt