Export (0) Print
Expand All

IronPython Windows Forms Designer and CodeDOM Sample

Visual Studio 2005
[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

This PythonProject sample demonstrates how to connect the Windows Forms Designer to the IronPython CodeDOM. This connection lets you drag and drop controls onto the form, change to code view to see the generated code, change the code, and return to design view to see the resulting changes.

Security noteSecurity Note

This sample code is provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices.  Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.

To understand the sample code, you must be familiar with a few terms:

  • A Using the CodeDOM is a code file that has been parsed into a code tree, much like the syntax diagram of a sentence.

  • A CodeDOM provider creates a CodeDOM, given the project hierarchy and an item (code file) id.

  • A designer loader manages the connection between the design view and a CodeDOM.

  • VSLangProj.VSProject provides references to existing classes and other information to a CodeDOM provider.

The code that integrates the designer with the CodeDOM is distributed throughout the PythonProject project sample:

  • The PythonProject class has two important read-only properties:

    • The PythonCodeDom property, which returns an IronPython.CodeDom.PythonProvider CodeDom provider. The PythonProvider class, which is based on CodeDomProvider, is located in the IronPython engine assembly.

      NoteNote

      The PythonCodeDom property returns a CodeDOM provider, not a CodeDOM.

    • The VSProject property, which returns an implementation of VSLangProj.VSProject.

  • The PythonProjectNode class adds services to provide a VSLangProj.Project and a CodeDOM provider. The service provider that provides these services can be obtained from PythonProjectNode.GetItemContext. This service provider is the connection point between the designer and the CodeDOM.

  • The decision whether to display a code file in code view or design view is made in EditorFactory.CreateDocumentView, which is called by EditorFactory.CreateEditorInstance.

  • EditorFactory.CreateFormView creates an IVSMDDesignerService, which it uses to create an IVSMDDesignerLoader (designer loader). The designer loader is initialized with the project hierarchy, the item (code file) ID, and the service provider obtained from PythonProjectNode.GetItemContext.

NoteNote

You cannot generate a CodeDOM from a file unless the first class in the file derives from Form, UserControl, or Component.

To build and run the sample

  1. Open the IronPython.sln solution in the <Installation Path>\VisualStudioIntegration\Samples\IronPythonIntegration folder.

  2. Press F5 to build the sample, register it in the experimental hive, and start Visual Studio from the experimental hive.

To see the sample's functionality

  1. On the File menu, click New Project to display the New Project dialog box.

  2. Create a new PythonProject Windows Application.

    The new solution appears in Solution Explorer, and the file Form1.py is opened in the Windows Forms Designer.

  3. Open the Toolbox and drag a Button control onto the form.

    The Form1.resx file is added to the project.

  4. Right-click the form, and then click View Code.

    The Form1.py file is opened in the code editor. Button code appears in the file:

    self.button1 = System.Windows.Forms.Button()
    

Demonstrates

Classes and class methods Interface methods

CodeDomProvider

VSProject

GetItemContext

CreateEditorInstance

See Also

Other Resources

IronPython Samples

Community Additions

ADD
Show:
© 2015 Microsoft