Creating Controls by Hand in the .NET Compact Framework

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:

  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.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a