Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Write Apps for the Smartphone without Dumbing Down Your UI : Page 2

Learn how to change the text input modes and improvise the ContextMenu and TabControl controls using the Smartphone 2003 SDK for the .NET Compact Framework.


advertisement
Improvising Context Menus
The ContextMenu control in the .NET Compact Framework is a useful control to display contextual information related to a particular control. For example, you can tap and hold onto an item in the TreeView control and a context menu may be displayed to show the list of options that you can select pertaining to the item selected.
Figure 4. Populating the Windows Form: The MainMenu control has a sub-menu item called "dummy."
The Smartphone 2003 SDK does not support the ContextMenu control. To support context menu in the Smartphone platform, you need to improvise one using the MainMenu control. To illustrate this example, I will create a new Smartphone application and use a ListView control to display a list of items. When an item in the ListView control is selected, pressing the right softkey will display a list of menu items related to the selected item.

Populate the Windows Form with the following controls:
a. ListView
b. MainMenu


The MainMenu control should have two menu items named Exit and Menu. The Menu item should have a sub-item named dummy, which I'll explain in a moment (see Figure 4 ).

Two events will be fired when a MainMenu item is clicked. The Click event will be fired the first time the item is clicked. Subsequent clicking will fire the Popup event.

In order to dynamically create a list of MenuItem controls under the Menu item during runtime, it is necessary to create a dummy item. If you remove sub-items during runtime, at least one item must be retained.

In the code window, code the createContextMenu() function. The createContextMenu() function will display a list of context menu items relating to the item highlighted in the ListView control at the time.

Public Sub createContextMenu() '====Remove existing menuitems Dim k As Integer 'mainmenu must contain at least one menuitem; 'else it will crash If mnuMenu.MenuItems.Count > 1 Then ' remove all but left 1 item For k = 0 To mnuMenu.MenuItems.Count - 2 ' always remove the first; the rest of ' the items will be pushed upwards mnuMenu.MenuItems.RemoveAt(0) Next End If '---the last item is now the first item '===Create a new context menu Dim j As _ System.Windows.Forms.ListView.SelectedIndexCollection ' get the index of the item selected in ' the ListView1 j = ListView1.SelectedIndices Dim i As Integer '===You can modify this section to show the items=== '===you want to display=== For i = 0 To 2 ' create 3 items in "context menu" If i = 0 Then ' the first item will be modified mnuMenu.MenuItems(0).Text = "Menu for " & _ ListView1.Items(j(0)).Text & "-0" Else '--the rest of the items will be added in Dim mni As New MenuItem mni.Text = "Menu for " & _ ListView1.Items(j(0)).Text _ & "-" & i AddHandler mni.Click, AddressOf EventHandler mnuMenu.MenuItems.Add(mni) End If Next End Sub

The EventHandler() subroutine will display a message when a context menu item is clicked. You can add your own code here to suit your business logic:

Sub EventHandler(ByVal sender As Object, _ ByVal e As EventArgs) ' handles events raised by the object Obj MsgBox("EventHandler caught event." & _ CType(sender, MenuItem).Text) ' Handle the event End Sub

In the Click event of the Menu item, code the following to display the context menu:


Private Sub mnuMenu_Click(ByVal sender As _ System.Object, _ ByVal e As System.EventArgs) _ Handles mnuMenu.Click createContextMenu() End Sub

In the Popup event of the Menu item, code the following to also display the context menu:

Private Sub mnuMenu_Popup(ByVal sender As Object, _ ByVal e As System.EventArgs) _ Handles mnuMenu.Popup createContextMenu() End Sub

In the Load event of the Windows Form, code the following to load the form and display a list of items:
Figure 5. Just Like Normal: Though improvised, this ContextMenu control operates just like a "real" one would.

Private Sub Form8_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Dim i As Integer ListView1.View = View.List ListView1.FullRowSelect = True '--add some items to the ListView control For i = 0 To 4 Dim lvi As New ListViewItem("Item " & i) ListView1.Items.Add(lvi) Next '--assign an event handler to the dummy item AddHandler mnuMenu.MenuItems(0).Click, _ AddressOf EventHandler '--select the first item when loaded ListView1.Items(0).Selected = True End Sub

Press F5 to debug and run the application. Figure 5 shows what the improvised ContextMenu looks like when it's run.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap