How to | Create a User Interface Using .NET/Link

.NET/Link lets you write sophisticated user interfaces by calling .NET types directly from the Wolfram Language. 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 the Wolfram Language. 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):

In[1]:=
Click for copyable input

Use InstallNET to launch the .NET runtime:

In[2]:=
Click for copyable input

Start off by creating a Windows Form, that is, a "window"; you can add controls such as menus to such a form:

In[3]:=
Click for copyable input

Display the form:

In[4]:=
Click for copyable input

Now the form appears. Watch what happens when the following properties are set for the width, height, and title:

In[5]:=
Click for copyable input
In[6]:=
Click for copyable input
In[7]:=
Click for copyable input

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:

In[8]:=
Click for copyable input

Set its default font family and font size:

In[9]:=
Click for copyable input

You will now dock the RTF box to the form.

First, load the .NET DockStyle type:

In[10]:=
Click for copyable input

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:

In[11]:=
Click for copyable input

Next, make some menu items and add them to the form.

Make a Save As menu item:

In[13]:=
Click for copyable input

Make a File menu item:

In[14]:=
Click for copyable input

Add the Save As menu item to the File menu item:

In[15]:=
Click for copyable input

Make menu items for selecting the font type and text color:

In[16]:=
Click for copyable input
In[17]:=
Click for copyable input

Make a Format menu item:

In[18]:=
Click for copyable input

Add the Select Font and Select Text Color menu items to the Format menu item:

In[19]:=
Click for copyable input

Make a main menu and add the File and Format menu items to it:

In[20]:=
Click for copyable input

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 Save As menu item will open a file browser to save a rich or plain text document:

In[22]:=
Click for copyable input
In[23]:=
Click for copyable input

Clicking the Select Font menu item calls the Windows Font dialog when text is selected:

In[24]:=
Click for copyable input

Similarly, clicking the Select Text Color menu item opens the Windows Color dialog box when text is selected:

In[26]:=
Click for copyable input

You can now type and format text in the form.

You can also set the form to display some initial text:

In[28]:=
Click for copyable input

In the form, choose Format Select Font, and display some parts of the text in bold or italics. Choose Format Select Text Color to add color to the text:

The file can now be saved by choosing File Save As in the form: