Click here to Skip to main content
Click here to Skip to main content

Hello Longhorn

, 1 Mar 2004
Rate this:
Please Sign up or sign in to vote.
Introduction to programming under Longhorn.

Introduction

This article introduces you to programming for the next version of Windows codenamed Longhorn. (In order to understand the codename jargon, please read The perplexed guide for the new codenames of Microsoft.) Now getting back to the point, Longhorn when released will change the landscape for developers in a significant way. Although most of the old stuff from COM (isn’t that old enough), .NET (Bye Bye WinForms) will work smoothly, the new technologies such as Avalon, WinFS, Indigo provide a much better and richer base to work with.

I will take you through a very simple program, which responds to a click on a button control and displays "Hello Longhorn". The code is entirely written in C#. I have used MSBuild to compile my code (I still do not have my hands on Whidbey). In a sequel to this article, I will show you how to write the same program in a new revolutionary way using XAML. But for now, let’s stick to vanilla C#.

Getting to the business

The figure below is what our killer application will look like. By looking at it, if you figured out that it has a window, a button and a text box, then pat yourself, because that’s what there is to it. It has another goodie called Canvas, and I will tell you about it in a minute. Here’s the picture:

Okay, now that you are done looking at this piece of exceptional artistry, I will show you how it’s done.

As usual, we start with our import statements:

using System;
using MSAvalon.Windows;
using MSAvalon.Windows.Controls;

Next, we create a class:

public class TextBoxApp : Application

Application is the top-level object, and all applications should inherit from this class. You can also inherit from the NavigationApplication class for Longhorn Navigation Applications.

Next, I declare a few controls:

MSAvalon.Windows.Controls.TextBox myTextBox;
MSAvalon.Windows.Controls.Button myButton;
MSAvalon.Windows.Controls.Canvas myCanvas;

Although I have my import statements at the top, I still like to use the full namespace. I feel that this helps me in learning about the new framework.

We then override a method from the base class which is the Application class. This method is always called when an application is started.

protected override void OnStartingUp(StartingUpCancelEventArgs e)
{
    base.OnStartingUp(e); //call the method in the base class
    CreateWindow(); //call a private method to create the window
}

Now, we will implement our private method, which is the meat of the application. This method will create a window, put a Canvas and then put other controls on top of the Canvas. Canvas class, which is located in the MSAvalon.Windows.Controls namespace, is one of the panels available in Avalon (the new presentation system under Longhorn). Below is the complete method:

private void CreateWindow()
{
    //create main window
    try
    {
        MSAvalon.Windows.Window myWindow = new MSAvalon.Windows.Window();
        myWindow.Width = new Length(300);
        myWindow.Height = new Length(170);

        //Add a canvas to the window
        myCanvas = new MSAvalon.Windows.Controls.Canvas();

        myCanvas.Background =  MSAvalon.Windows.Media.Brushes.LightSteelBlue;
        myCanvas.Width = new Length(100,UnitType.Percent);
        myCanvas.Height = new Length(100,UnitType.Percent);
        myWindow.Children.Add(myCanvas);


        //Add a Text Box to the canvas
        myTextBox = new MSAvalon.Windows.Controls.TextBox();
        myTextBox.Width = new Length(200);
        myTextBox.Height = new Length(40);
        MSAvalon.Windows.Controls.Canvas.SetTop(myTextBox, new Length(30));
        MSAvalon.Windows.Controls.Canvas.SetLeft(myTextBox, new Length(50));

        myCanvas.Children.Add(myTextBox);

        //Add a button to the canvas
        myButton = new MSAvalon.Windows.Controls.Button();
        MSAvalon.Windows.Controls.Canvas.SetTop(myButton, new Length(100));
        MSAvalon.Windows.Controls.Canvas.SetLeft(myButton, new Length(110));
        myButton.Content = "Click Me";
        myCanvas.Children.Add(myButton);

        //Add a event handler for Click Event
        myButton.Click += new ClickEventHandler(OnClick);

        //show the window
        myWindow.Show();


    }
    catch(Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
}

I will point your attention to a particular line in the code:

myButton.Content = "Click Me";

Note that there is no Text property for the Button control.

I have also specified an event handler for myButton's Click event. All that this handler will do is change the text in my text box:

void OnClick(object sender, ClickEventArgs e)
{
    try
    {
        myTextBox.Text = "Hello Longhorn";
    }
    catch(Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
    }
}

Next, we need to provide a main method that will run our application. I have my main method in my EntryClass. I have marked it as sealed, because I never wish to inherit from this class (you don’t need to do this).

public sealed class EntryClass
{
    [System.STAThread()]
    private static void Main ()
    {
        TextBoxApp app = new TextBoxApp ();
        app.Run ();
    }
}

As I do not have Whidbey, I used MSBuild to compile my project. You will need a project file, which is included in the code download with this article. A project file tells MSBuild about what kind of application this is, what files are included, and a few other things. Make sure that you have both project and the source files in the same folder.

In order to build the application successfully, launch your build environment from Start - All Programs - Microsoft Longhorn SDK - Open Build Environment Window - Longhorn Build Environment. Change to the directory where you have the project and the source file, and type:

msbuild SimpleWinApp.csproj

and hit Enter. You should have an EXE in the Release folder under your current directory. Click on the EXE and enjoy the application.

For all Longhorn nuts, I recommend a daily visit to Longhorn Developer Centre.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Deepak Kapoor

Australia Australia
No Biography provided
Follow on   Twitter

Comments and Discussions

 
GeneralThank you! PinmemberFinishedOnTime22-Mar-07 6:15 
GeneralDoesn't work on PDC build PinmemberTom Archer - MSFT22-Sep-05 11:36 
GeneralExtending this example PinmemberTadejK16-Jan-05 9:09 
GeneralRe: Extending this example PinsussDeepak Kapoor16-Jan-05 16:57 
GeneralRe: Extending this example PinmemberTadejK16-Jan-05 21:08 
GeneralRe: Extending this example PinsussDeepak Kapoor17-Jan-05 0:46 
GeneralLonghorn sdk Pinmemberko_dk20-Apr-04 6:41 
GeneralRe: Longhorn sdk PinmemberDeepak Kapoor20-Apr-04 13:57 
GeneralRe: Longhorn sdk Pinmemberko_dk20-Apr-04 20:34 
GeneralRe: Longhorn sdk PinsussAnonymous1-Aug-04 17:06 
General.Net Framework v1.2.30703? :confused: PinmemberRisingPhoenix23-Mar-04 3:44 
GeneralRe: .Net Framework v1.2.30703? :confused: PinmemberDeepak Kapoor25-Mar-04 17:28 
GeneralRe: .Net Framework v1.2.30703? :confused: PinmemberRisingPhoenix25-Mar-04 19:38 
GeneralRe: .Net Framework v1.2.30703? :confused: PinmemberDeepak Kapoor25-Mar-04 19:45 
Generaltypical MS!! PinmemberPaul Griffin12-Mar-04 6:33 
GeneralRe: typical MS!! PinstaffMarcie Robillard (Datagrid Girl)12-Mar-04 7:10 
GeneralRe: typical MS!! PinmemberJoeMarini8-Apr-04 11:11 
GeneralRe: typical MS!! PinmemberWillemM7-May-04 1:08 
GeneralAbout the MSAvalon namespace PineditorMarc Clifton2-Mar-04 2:28 
GeneralRe: About the MSAvalon namespace Pinmemberdog_spawn2-Mar-04 14:19 
GeneralRe: About the MSAvalon namespace PinmemberDeepak Kapoor2-Mar-04 22:57 
GeneralRe: About the MSAvalon namespace PinmemberNorm Almond8-Mar-04 3:12 
GeneralRe: About the MSAvalon namespace PinmemberRyan Beesley27-Apr-04 0:40 
GeneralRe: About the MSAvalon namespace PinsussKeith Hill4-Mar-04 21:52 
GeneralRe: About the MSAvalon namespace PinmemberFrank Hileman9-Mar-04 5:00 
GeneralRe: About the MSAvalon namespace PinmemberMartin Stampe10-Mar-04 3:07 
GeneralWrong image URL Pinmemberszaddam2-Mar-04 1:48 
GeneralRe: Wrong image URL PineditorMarc Clifton2-Mar-04 2:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 2 Mar 2004
Article Copyright 2004 by Deepak Kapoor
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid