A Simple Oxygene WPF Application

This series of steps requires the use of Oxygene within Visual Studio.

The result will be a program in which you can enter a forename, be greeted personally and use a button to clear the data. The following screenshot shows the program in action.

Screenshot

Screenshot

Here the layout is of the type used in a mobile phone, which uses a StackPanel to place the other components immediately under each other. (The default container is a Grid, which is used to position components by coordinates).

  1. Select menu item File > New > Project (or type Ctrl+Shift+N), highlight the WPF Application and name the project WPF_Greeting. Click the OK button.
  2. Click on the Grid inside the form and remove it by pressing the Delete key.
  3. Open the Toolbox and double click on StackPanel to add it to the form.
  4. Double click on Button to add it to the top of the StackPanel. Name it immediately by typing btnClear for its Name property at the top of the Properties panel at the bottom right of Visual Studio. Uncheck the box for its IsEnabled property. Change its Content property from Button to Clear.
  5. Double click on Label and name the new label lblInstruction. Change its Content property from Label to Please enter your forename in the box below.
  6. Double click on TextBox and name the new TextBox tbForename. Remove "TextBox" from its Text property.
  7. Select the Events (lightning icon) tab of the tbForename Properties pane and double click in the box for KeyDown.

    The empty procedure to handle KeyDown shows in the editor.

  8. Put the code below into the procedure to handle the KeyDown event of tbForename.
      btnClear.IsEnabled := True;
      if e.Key = e.Key.Enter then
        lblGreeting.Content := 'Hello, ' + tbForename.Text + '!';
  9. Double click on Label in the ToolBox and name the new label lblGreeting. Remove "Label" from its Contents property.

  10. Double click on the button in the design view of the form.

    The empty click-handling procedure of the form shows in the editor.

  11. Put the code below into click-handling procedure of the form:
    lblGreeting.Content := '';
    tbForename.Text := '';
    btnClear.IsEnabled := False;
  12. Press F5 or the green triangle to compile and run the application.

    The application should be functional, but can be improved most easily by editing the code in Window1.xaml. See the final version of the XAML and Oxygene code below. Note that this program compiles without the uses clause in the source code.

Oxygene code

namespace WPF_Greeting;

interface

type
  Window1 = public partial class(System.Windows.Window)
  private
    method btnClear_Click(sender: System.Object; e: System.Windows.RoutedEventArgs);
    method tbForename_KeyDown(sender: System.Object; e: System.Windows.Input.KeyEventArgs);
  public
    constructor;
  end;
  
implementation

constructor Window1;
begin
  InitializeComponent();
end;

method Window1.btnClear_Click(sender: System.Object; e: System.Windows.RoutedEventArgs);
begin
  lblGreeting.Content := '';
  tbForename.Text := '';
  btnClear.IsEnabled := False;
end;

method Window1.tbForename_KeyDown(sender: System.Object; e: System.Windows.Input.KeyEventArgs);
begin
  btnClear.IsEnabled := True;
  if e.Key = e.Key.Enter then
    lblGreeting.Content := 'Hello, ' + tbForename.Text + '!';
end;
  
end.

XAML code

XAML Code
Programming - a skill for life!

How to write programs in the Oxygene for .Net dialect of Pascal