Visual Studio Express and ironPython

For this example, we use the free version of Visual Studio, Visual Studio Express. Visual Studio Express doesn't have IronPython integration, so we have to find another way to work with it to create our dialog.

Depending on which version you download,9 it can generate Visual Basic, C#, C++, or Visual J#. Because they all compile to .NET assemblies and we won't be directly writing code in this example, it doesn't really matter which one you choose. Having generated code in another language can be an advantage; it means there's less temptation to fiddle with it!

You can create the dialog layout in Visual Studio, subclass it in IronPython, and program all the behavior in the subclass. You need to create an assembly containing a suitable base class for our dialog.

Our base class dialog will be a class library, so open Visual Studio Express and create a new class library. You'll be presented with the interface in figure 6.5.

Microsoft Visual Express Interface
Figure 6.5 Creating a RenameTabDialog class library in Visual Studio Express

9 See Note that you can install multiple versions side by side.

After creating the class library, preferably with a sensible name, you need to add a form to it. If you right-click the project in the Solution Explorer, you should see the menus shown in figure 6.6, and be able to add a form.

This will add the form to the project, and Visual Studio will whirr away for a few seconds while it adds references to relevant assemblies such as System.Windows. Forms. You can then resize the form and drag controls from the toolbox onto it. You'll also need to open the Properties pane by right-clicking the form and selecting Properties. You want to end up with an interface looking like figure 6.7.

By clicking each element (the form, the buttons, and the text box), you can configure the different properties for each control. For our dialog, you need to complete the following steps:

Figure 6.6 Adding a form to the new class library
Ironpython Forms
Figure 6.7 Designing the RenameTabDialog with the Visual Studio forms designer

1 Name the buttons and set their text.

2 Set the DialogResult properties on the buttons.

3 Make sure you name the TextBox textBox and set the modifier to Public rather than Private. You'll be able to access the property from the ShowDialog function.

4 Set the form border to FixedDialog.

5 Set the form name to RenameTabDialogBase.

6 Set the form ShowInTaskbar property to False.

7 Set the AcceptButton and CancelButton properties on the form to okButton and cancelButton.

Creating the dialog with Visual Studio takes considerably less time than trying to tweak the GUI by hand, but the results are almost identical.

NOTE In IronPython 1, you can access protected .NET members as if they were public.10 The IronPython 2 rules about accessing .NET members are closer to C#.11 You'll subclass the dialog we're creating, so protected status would allow you access from inside the subclass—but you also need external access so the text box must be public.

Pressing F6 in Visual Studio (or selecting the Build > Build Solution menu item) compiles our project into an assembly. The compiled assembly will then be available in the biri\Release folder of the project as RenameTabDialog.dll (assuming you named your project RenameTabDialog).

For you to use this assembly from IronPython, it needs to be somewhere on sys.path. The simplest solution is to put it in the same directory as the IronPython file using it. We can then add a reference to RenameTabDialog and import RenameTabDialogBase from the RenameTabDialog namespace. You'll need to tweak these names to match the ones you've used in the Visual Studio project.

Having done the layout and configuring in the Visual Studio project, we can reduce the amount of code in the dialog. Instead of subclassing Form, you can now create a subclass of RenameTabDialogBase. You still need to set the title and the initial text in the text box. The full code for the dialog is shown in listing 6.14.

+1 0


  • Florian
    Does ironpython work with visual studio express?
    6 years ago
  • Bryan Millar
    How to add wxPython to IronPython?
    6 years ago

Post a comment