Share via


Walkthrough: Create and Run a Simple Windows Application In Managed Code

4/19/2010

In this walkthrough you will create, build and run a simple Hello World application in Visual Basic or C#.

To create a Windows Application project

  1. On the File menu, point to New, and then select Project.

  2. In the Project Types pane, expand the Visual Basic or Visual C# branch. Then expand the Smart Device branch. Select the appropriate Windows Mobile 6 project type.

  3. In the Templates pane, choose Device Application.

  4. In the Name box, name the project something unique to indicate the application's purpose. In the Location box, enter the directory in which you want to save your project, or click the Browse button to navigate to it.

The Windows Forms Designer opens, showing Form1 of the project you created.

To build the application

  1. In the Windows Forms Designer, click on the MainMenu1 control in order to start editing the menu. Click in the menu area on the form where it says "Type Here" and type "Quit" and press Enter. You may need to scroll the form down in order to view the menu area.

  2. Double-click on the word "Quit" to go to the event handler for the menu option. Add the following code to handle the event:

    VB

    Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
      Application.Exit()
    End Sub
    

    C#

    private void menuItem1_Click(object sender, EventArgs e)
    {
      Application.Exit(); 
    }
    
  3. Add the following code to the Form1 class to display "Hello World":

    VB

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
      ' Create string to draw.
      Dim drawString As [String] = "Hello World"
    
      ' Create font and brush.
      Dim drawFont As New Font("Arial", 10, FontStyle.Regular)
      Dim drawBrush As New SolidBrush(Color.Black)
    
      ' Create point for upper-left corner of drawing.
      Dim x As Single = 10.0F
      Dim y As Single = 10.0F
    
      ' Draw string to screen.
      e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y)
    
    End Sub
    

    C#

    protected override void OnPaint(PaintEventArgs e)
    {
      // Create string to draw.
      string drawString = "Hello World";
    
      // Create font and brush.
      Font drawFont = new Font("Arial", 10, FontStyle.Regular);
      SolidBrush drawBrush = new SolidBrush(Color.Black);
    
      // Create point for upper-left corner of drawing.
      float x = 10.0F;
      float y = 10.0F;
    
      // Draw string to screen.
      e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y);
    }
    
  4. From the Solution Configurations dropdown, located in the toolbar, select Debug.

  5. From the Target Device dropdown, located in the toolbar, select your device to test the application on. For this walkthrough, select Windows Mobile 6 Classic Emulator.

  6. Build your project by selecting Build Solution from the Build menu. Check the results of the build in the Output window. Fix errors as needed until the project builds successfully.

To run and debug the application

  1. Set your desired breakpoints. You can set a breakpoint by clicking in the left margin of the line of code you want to set the breakpoint on or by placing the cursor on a line of code and selecting Toggle Breakpoint from the Debug menu. F9 will also toggle breakpoints.

  2. Select Connect to Device from the Tools menu to set up communication with the debugging device, in this case the Windows Mobile 6 Classic Emulator Click the Connect button.

  3. The emulator window will open and start up. It may take a minute or so to set up the connection with the emulator. Click Close on the Connecting dialog when the connection has been successfully made.

  4. Select Start Debugging from the Debug menu or press F5 to start the application. The executable and any other needed files will be transferred to the emulator. It may take a minute or so to transfer the files.

  5. You can now run your program on the target device and debug it in the Visual Studio environment. You can use the Continue (F5), Step Over (F10), Step Into (F11), and Step Out (Shift+F11) commands to walk through the code.

  6. To stop debugging, you can exit your application or select Stop Debugging (Shift+F5) from the Debug menu.

  7. When you are ready to build the release version of your project, you can change the Solution Configuration dropdown to Release and rebuild the project to build the release version.

  8. When you close the emulator, you are given the option to save the emulator state. By saving the state, you can decrease the emulator start up time the next time you launch the emulator.

See Also

Tasks

Walkthrough: Create and Run a Simple Windows Application In Native Code

Other Resources

Welcome to Windows Mobile 6.5 Documentation