Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Hello Longhorn

0.00/5 (No votes)
1 Mar 2004 1  
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