advertisement
Login | Register   
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Partners & Affiliates
advertisement
advertisement
advertisement
advertisement
Average Rating: 2/5 | Rate this item | 2 users have rated this item.
Creating Controls by Hand in the .NET Compact Framework (cont'd)
Four Buttons and Menu
For my example, I will create four Button controls by code and assign each button to a context menu. This will create functionality for a user of the application to tap and hold a control with the stylus to dynamically generate a context menu displaying a list of options for the user to choose from. This end result is shown in Figure 1.
advertisement

Figure 1. Button Click: The sample application creates a popup context menu for each of four button controls.
To create the application for this article, first launch Visual Studio .NET 2003 and create a new Smart Device application. Switch to the code editing mode.

First, declare two global variables for creating Button and ContextMenu controls:


Dim mybutton As Button
Dim cmenu As ContextMenu
When the form is loaded, create the Button controls manually and add them to the form using the Me.Controls.Add() method. The Button code is shown below. The code also associates each Button control with a Context Menu control by setting the ContextMenu property for each one.

For i = 0 To 3
Select Case i
Case 1
startPt.X = 120
startPt.Y = 50
Case 2
startPt.X = 50
startPt.Y = 100
Case 3
startPt.X = 120
startPt.Y = 100
End Select
mybutton = New Button
mybutton.Location = startPt
mybutton.Text = i
mybutton.ContextMenu = cmenu
'---add the button to the form
Me.Controls.Add(mybutton)
...
...
...
Next
Besides creating the buttons, you need to add an event handler for each one to tell them what behavior to perform when clicked. The AddHandler statement associates an event (e.g. mybutton.Click) with an event handler (e.g. button_Click).

'---add an event handler for the button
AddHandler mybutton.Click, AddressOf button_Click
You also need to add an event handler to display the context menu ( cmenu_popup) when a user taps and 'holds down' the buttons.

'---add an event handler for the contextmenu
AddHandler cmenu.Popup, AddressOf cmenu_popup
Here is the complete source for the Form1_Load event:

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Dim startPt As New Point(50, 50)
cmenu = New ContextMenu
Dim i As Integer
'---displays the buttons dynamically
For i = 0 To 3
Select Case i
Case 1
startPt.X = 120
startPt.Y = 50
Case 2
startPt.X = 50
startPt.Y = 100
Case 3
startPt.X = 120
startPt.Y = 100
End Select
mybutton = New Button
mybutton.Location = startPt
mybutton.Text = i
mybutton.ContextMenu = cmenu
'---add the button to the form
Me.Controls.Add(mybutton)
'---add an event handler for the button
AddHandler mybutton.Click, AddressOf button_Click
Next
'---add an event handler for the contextmenu
AddHandler cmenu.Popup, AddressOf cmenu_popup
End Sub
To display the Context Menu control dynamically, you have to add the menu items individually. You also need to add an event handler ( menuItem_Click) to each item so they will perform the proper action when clicked.

Protected Sub cmenu_popup(ByVal sender As System.Object, _
ByVal e As System.EventArgs)
'---event handler for the contextmenu
Dim i As Integer
cmenu.MenuItems.Clear()
For i = 65 To 70
Dim mItem As MenuItem = New MenuItem
mItem.Text = Chr(i)
cmenu.MenuItems.Add(mItem)
AddHandler mItem.Click, AddressOf menuItem_Click
Next
End Sub
You can write the code to perform the required task when the menu item within the Context Menu control is clicked. You can use CType(sender, MenuItem).Text to retrieve the text property of the menu item that was clicked:

Protected Sub menuItem_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs)
'---event handler for menuitem click
MsgBox(CType(sender, MenuItem).Text)
'--do something for this item
...
...
...
End Sub
The same goes for the button_click event for the Button control:

Protected Sub button_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs)
'---event handler for the buttons
MsgBox(CType(sender, Button).Text)
'--do something for this item
...
...
...
End Sub
Even though Visual Studio .NET makes it very easy to develop mobile applications by dragging and dropping controls onto your form, at times you may need to create controls during runtime. Some scenarios include:
  1. You want to display a list of options generated at runtime (such as from a Web service) using a context menu.
  2. You do not know in advance how many controls you need to display; the users may specify that during runtime (such as displaying calendar entries).

The ability to generate controls at runtime has another advantage—if you are adding a large number of controls (such as the Button control), it is generally easier to create them by hand and wire them up using the AddHandler statement. Imagine having to add 20 controls onto your form and write each event individually.

The steps to creating controls during runtime are:

  1. Declare the control variables and add them to your form using the Me.Controls.Add() method.
  2. Associate the control with an event handler so that you can service the events raised by the control during runtime. The AddHandler statement associates an event (e.g. mybutton.Click) with an event handler (e.g. button_Click).

The only challenge to adding controls during runtime is the positioning of each control—you have to manually specify the exact location on the form.

You can use this same process in innumerable ways to add controls to your .NET CF applications on the fly. Mastering this technique should make you a far more nimble and efficient purveyor of handheld applications.

Previous Page: Introduction  
Wei-Meng Lee is a Microsoft .NET MVP and co-founder of Active Developer, a technology company specializing in hands-on training on the latest technologies. He is an established developer and trainer specializing in .NET and wireless technologies. He speaks and writes frequently on topics ranging from .NET to Mac OS X. He is the author of "Windows XP Unwired" (OReilly & Associates) and is currently working on "Programming the .NET Compact Framework," also from OReilly. He can be reached at weimeng@activedevelop.com.
Page 1: IntroductionPage 2: Four Buttons and a Menu
Please rate this item (5=best)
 1  2  3  4  5
advertisement