here will be times when you’ll need to hand-code your .NET Compact Framework controls, without using the drag-and-drop feature of Visual Studio .NET. For example, you may not know how many controls to create during design time, and thus during runtime you need to create them explicitly using code.
A good example is the use of a context menu. The options in a context menu may be obtained from a Web service, and so you need to dynamically create the context menu when the program is running.
In this article, I will show you how to create controls dynamically and how to “wire” them up to event handlers.
I need to create a control for my handheld application on the fly at runtime, without going back to the visual environment.
Hand-code your controls directly into your project and then connect them with event handlers. 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.
|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:
- You want to display a list of options generated at runtime (such as from a Web service) using a context menu.
- 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:
- Declare the control variables and add them to your form using the Me.Controls.Add() method.
- 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.