How to | Create a User Interface Using
.NET/Link
.NET/Link lets you write sophisticated user interfaces by calling .NET types directly from
Mathematica. Doing so allows you to evaluate code as you add it, either one or multiple lines at a time, much like when writing a program in
Mathematica. This results in an extremely powerful development environment that allows you to experiment with your user interface
while it is running.
You must load the
.NET/Link package before you can use
.NET/Link (which can only be loaded on the Windows platform):
Use
InstallNET to launch the .NET runtime:
Start off by creating a Windows Form, that is, a "window"; you can add controls such as menus to such a form:
Now the form appears. Watch what happens when the following properties are set for the width, height, and title:
You have just set standard .NET properties, much as you would in C# or Visual Basic. However, unlike these programming languages, the changes appear immediately in the window.
Next, create a Rich Text (
RTF) box to enter and display text. This will later be added to the form:
Set its default font family and font size:
You will now dock the
RTF box to the form.
First, load the .NET DockStyle type:
Dock the
RTF box to the edges of the form, with the

set to

. This makes the
RTF box fill the form, regardless of how the form is resized:
Next, make some menu items and add them to the form.
Add the menu item to the menu item:
Make menu items for selecting the font type and text color:
Add the and menu items to the menu item:
Make a main menu and add the and menu items to it:
Add the main menu to the form:
You must now specify how the events, in this case clicking the specific menu items, will be handled.
Clicking the menu item will open a file browser to save a rich or plain text document:
Clicking the menu item calls the Windows font dialog when text is selected:
Similarly, clicking the menu item opens the Windows color dialog box when text is selected:
You can now type and format text in the form.
You can also set the form to display some initial text:
In the form, choose , and display some parts of the text in bold or italics. Choose to add color to the text:
The file can now be saved by choosing in the form: