A Simple Oxygene WPF Application
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.
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).
- Select menu item WPF_Greeting. Click the OK button. (or type Ctrl+Shift+N), highlight the WPF Application and name the project
- Click on the Grid inside the form and remove it by pressing the Delete key.
- Open the Toolbox and double click on StackPanel to add it to the form.
- 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.
- 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.
- Double click on TextBox and name the new TextBox tbForename. Remove "TextBox" from its Text property.
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.
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 + '!';
Double click on Label in the ToolBox and name the new label lblGreeting. Remove "Label" from its Contents property.
Double click on the button in the design view of the form.
The empty click-handling procedure of the form shows in the editor.
Put the code below into click-handling procedure of the form:
lblGreeting.Content := ''; tbForename.Text := ''; btnClear.IsEnabled := False;
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.
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.