This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
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):
In[134]:=
Click for copyable input
Use InstallNET to launch the .NET runtime:
In[135]:=
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[136]:=
Click for copyable input
Display the form:
In[140]:=
Click for copyable input
Now the form appears. Watch what happens when the following properties are set for the width, height, and title:
In[137]:=
Click for copyable input
In[138]:=
Click for copyable input
In[139]:=
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[141]:=
Click for copyable input
Set its default font family and font size:
In[142]:=
Click for copyable input
You will now dock the RTF box to the form.
First, load the .NET DockStyle type:
In[143]:=
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[144]:=
Click for copyable input
Next, make some menu items and add them to the form.
Make a Save As... menu item:
In[146]:=
Click for copyable input
Make a File menu item:
In[148]:=
Click for copyable input
Add the Save As... menu item to the File menu item:
In[150]:=
Click for copyable input
Make menu items for selecting the font type and text color:
In[151]:=
Click for copyable input
In[153]:=
Click for copyable input
Make a Format menu item:
In[155]:=
Click for copyable input
Add the Select Font and Select Text Color menu items to the Format menu item:
In[157]:=
Click for copyable input
Make a main menu and add the File and Format menu items to it:
In[158]:=
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[160]:=
Click for copyable input
In[161]:=
Click for copyable input
Clicking the Select Font menu item calls the Windows font dialog when text is selected:
In[162]:=
Click for copyable input
Similarly, clicking the Select Text Color menu item opens the Windows color dialog box when text is selected:
In[164]:=
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[166]:=
Click for copyable input
In the form, choose FormatSelect Font, and display some parts of the text in bold or italics. Choose FormatSelect Text Color to add color to the text:
The file can now be saved by choosing FileSave As... in the form: