Click here to Skip to main content
15,867,308 members
Articles / Programming Languages / C#
Article

Hello Longhorn

Rate me:
Please Sign up or sign in to vote.
3.32/5 (32 votes)
1 Mar 20043 min read 126.2K   281   22   28
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:

C#
using System;
using MSAvalon.Windows;
using MSAvalon.Windows.Controls;

Next, we create a class:

C#
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:

C#
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.

C#
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:

C#
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:

C#
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:

C#
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).

C#
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


Written By
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThank you! Pin
FinishedOnTime22-Mar-07 5:15
FinishedOnTime22-Mar-07 5:15 
GeneralDoesn't work on PDC build Pin
Tom Archer22-Sep-05 10:36
Tom Archer22-Sep-05 10:36 
GeneralExtending this example Pin
TadejK16-Jan-05 8:09
TadejK16-Jan-05 8:09 
GeneralRe: Extending this example Pin
Deepak Kapoor16-Jan-05 15:57
Deepak Kapoor16-Jan-05 15:57 
GeneralRe: Extending this example Pin
TadejK16-Jan-05 20:08
TadejK16-Jan-05 20:08 
GeneralRe: Extending this example Pin
Deepak Kapoor16-Jan-05 23:46
Deepak Kapoor16-Jan-05 23:46 
GeneralLonghorn sdk Pin
RATC20-Apr-04 5:41
RATC20-Apr-04 5:41 
GeneralRe: Longhorn sdk Pin
Deepak Kapoor20-Apr-04 12:57
Deepak Kapoor20-Apr-04 12:57 
GeneralRe: Longhorn sdk Pin
RATC20-Apr-04 19:34
RATC20-Apr-04 19:34 
GeneralRe: Longhorn sdk Pin
Anonymous1-Aug-04 16:06
Anonymous1-Aug-04 16:06 
General.Net Framework v1.2.30703? :confused: Pin
Wenff23-Mar-04 2:44
professionalWenff23-Mar-04 2:44 
GeneralRe: .Net Framework v1.2.30703? :confused: Pin
Deepak Kapoor25-Mar-04 16:28
Deepak Kapoor25-Mar-04 16:28 
GeneralRe: .Net Framework v1.2.30703? :confused: Pin
Wenff25-Mar-04 18:38
professionalWenff25-Mar-04 18:38 
GeneralRe: .Net Framework v1.2.30703? :confused: Pin
Deepak Kapoor25-Mar-04 18:45
Deepak Kapoor25-Mar-04 18:45 
Generaltypical MS!! Pin
Paul Griffin12-Mar-04 5:33
Paul Griffin12-Mar-04 5:33 
GeneralRe: typical MS!! Pin
Marcie Jones12-Mar-04 6:10
Marcie Jones12-Mar-04 6:10 
GeneralRe: typical MS!! Pin
JoeMarini8-Apr-04 10:11
JoeMarini8-Apr-04 10:11 
GeneralRe: typical MS!! Pin
WillemM7-May-04 0:08
WillemM7-May-04 0:08 
GeneralAbout the MSAvalon namespace Pin
Marc Clifton2-Mar-04 1:28
mvaMarc Clifton2-Mar-04 1:28 
GeneralRe: About the MSAvalon namespace Pin
dog_spawn2-Mar-04 13:19
dog_spawn2-Mar-04 13:19 
GeneralRe: About the MSAvalon namespace Pin
Deepak Kapoor2-Mar-04 21:57
Deepak Kapoor2-Mar-04 21:57 
GeneralRe: About the MSAvalon namespace Pin
NormDroid8-Mar-04 2:12
professionalNormDroid8-Mar-04 2:12 
GeneralRe: About the MSAvalon namespace Pin
Ryan Beesley26-Apr-04 23:40
Ryan Beesley26-Apr-04 23:40 
GeneralRe: About the MSAvalon namespace Pin
Keith Hill4-Mar-04 20:52
Keith Hill4-Mar-04 20:52 
GeneralRe: About the MSAvalon namespace Pin
Frank Hileman9-Mar-04 4:00
Frank Hileman9-Mar-04 4:00 

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

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