Click here to Skip to main content
Click here to Skip to main content

Tagged as

Monodevelop: Howto add a WinForm project and file template

, 21 Oct 2010
Rate this:
Please Sign up or sign in to vote.
MonoDevelop 2.4 adding custom WinForm project and file templates

I am running MonoDevelop 2.4 on my acer aspire one x150 netbook running Ubuntu 9.04 (Jaunty).

From time to time I needed to write and run little test applications in C#. As I am alsos running another PC with Visual Studio 2005 and 2008, I like these for providing me a WinForm template, so I dont have to start from scratch.

Unfortunately MonoDevelop does not come with a WinForm template and I searched the internet on how to extend the templates provided by MonoDevelop to add a WinForm template. Although I found some resources on how to add templates to MonoDevelop, none of them worked as described.

Adding a template by using addins directory

Some sources state you can add templates by creating two files within the MonoDevelop home dir. For example adding HelloTemplate.xpt.xml and MyTemplates.addin.xml to “~/.config/MonoDevelop/addins”. I tried this approach, but it did not work as a WinForm project template.

Adding a template by using a packed template

Similar to the previous attempt but using mdtool to pack the files:
mdtool setup pack addins/addin.xml -d:addins
and
mdtool setup rep-build addins
where you finally get a mpack file with your xml template files. Unfortunately this did also not work for project templates.

Adding a template to the sources and recompile

As I already had the MonoDevelop sources as I have compiled and installed MonoDevelop 2.4 from sources, I looked for the existing templates and added my WinForm project and file template.

Screenshot Start New WinForm project

Screenshot Add New WinForm file

The source templates for CSharp are located on my netbook at: “/usr/local/src/monodevelop-2.4/src/addins/CSharpBinding/templates”. The Makefile references the template files in this directory. I had to add two template files to the directory and to the Makefile and the file CSharpBinding.add.xml. Then I did a make within the directory /usr/local/src/monodevelop-2.4/src/addins/CSharpBinding and then a “sudo make install” and finally got my WinForm project and file template available in MonoDevelop.

Makefile

RES = \
    CSharpBinding.addin.xml \
    gtk-gui/gui.stetic \
    gtk-gui/objects.xml \
    icons/C\#.FileIcon \
    icons/csharp-icon-32.png \
    icons/csharp-project-16.png \
    md1format.xml \
    MonoCSharpPolicy.xml \
    MonoDevelop.CSharp.Formatting/CSharpFormattingPolicy.xml \
    templates/AssemblyInfo.xft.xml \
    templates/ConsoleProject.xpt.xml \
    templates/WinFormProject.xpt.xml \
    templates/EmptyCSharpFile.xft.xml \
    templates/EmptyFormCSharpFile.xft.xml \
    templates/EmptyProject.xpt.xml \
    templates/GtkSharp2Project.xpt.xml \
    templates/GtkSharp2ProjectMac.xpt.xml \
    templates/Library.xpt.xml

CSharpBinding.addin.xml

    <Extension path = "/MonoDevelop/Ide/FileTemplates">
        <FileTemplate id = "CSharpEmptyFile" resource = "EmptyCSharpFile.xft.xml"/>
        <FileTemplate id = "CSharpEmptyFormFile" resource = "EmptyFormCSharpFile.xft.xml"/>
        <FileTemplate id = "CSharpAssemblyInfo" resource = "AssemblyInfo.xft.xml"/>
    </Extension>

    <Extension path = "/MonoDevelop/Ide/ProjectTemplates">
        <ProjectTemplate id = "CSharpConsoleProject" resource = "ConsoleProject.xpt.xml"/>
        <ProjectTemplate id = "CSharpWinFormProject" resource = "WinFormProject.xpt.xml"/>
        <ProjectTemplate id = "CSharpEmptyProject" resource = "EmptyProject.xpt.xml"/>

The new files WinFormProject.xpt.xml and EmptyFormCSharpFile.xft.xml are simply based on the Console project template and the EmptyFile template files. I just added some code.

WinFormProject.xpt.xml

...
    <!-- Template Content -->
    <Combine name = "${ProjectName}" directory = ".">
        <Options>
            <StartupProject>${ProjectName}</StartupProject>
        </Options>

        <Project name = "${ProjectName}" directory = ".">
            <Options ExternalConsole="False"/>
            <References>
                <Reference type="Gac" refto="System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <Reference type="Gac" refto="System.Windows.Forms, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
                <Reference type="Gac" refto="System.Drawing, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f711d50a3a" />
            </References>
            <Files>
                <File name="Main.cs" AddStandardHeader="True"><![CDATA[using System;
using System.Windows.Forms;

namespace ${Namespace}
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            WinForm form = new WinForm();
            Application.Run(form);
            //Console.WriteLine("Hello World!");
        }
    }
    public class WinForm:Form{
        public WinForm(){
            InitializeComponent();
        }

        private void InitializeComponent() {
            this.Width = 400;
            this.Height = 300;
            this.Text = "My Dialog";
            Button btnOK = new Button();
            btnOK.Text="OK";
            btnOK.Location=new System.Drawing.Point(10,10);
            btnOK.Size=new System.Drawing.Size(80,24);
            this.Controls.Add(btnOK);
            btnOK.Click+=new EventHandler(btnOK_Click);
        }

        private void btnOK_Click(object sender, System.EventArgs e){
            this.DialogResult=DialogResult.OK;
            this.Close();
        }

    }
}]]></File>
            <FileTemplateReference TemplateID="CSharpAssemblyInfo" name="AssemblyInfo.cs" />
            </Files>
        </Project>
...

EmptyFormCSharpFile.xft.xml

    <TemplateConfiguration>
        <_Name>Empty WinForm File</_Name>
        <Icon>md-text-file-icon</Icon>
        <_Category>General</_Category>
        <LanguageName>C#</LanguageName>
        <_Description>Creates an empty C# WinForm file.</_Description>
    </TemplateConfiguration>

    <TemplateFiles>
        <File DefaultExtension=".cs" DefaultName="EmptyCSharpfile" AddStandardHeader="True"><![CDATA[using System;
using System.Windows.Forms;

namespace ${Namespace}
{
    public class ${Name}:Form{
        public ${Name}(){
            InitializeComponent();
        }

        private void InitializeComponent() {
            this.Width = 400;
            this.Height = 300;
            this.Text = "My Dialog";
            Button btnOK = new Button();
            btnOK.Text="OK";
            btnOK.Location=new System.Drawing.Point(10,10);
            btnOK.Size=new System.Drawing.Size(80,24);
            this.Controls.Add(btnOK);
            btnOK.Click+=new EventHandler(btnOK_Click);
        }

        private void btnOK_Click(object sender, System.EventArgs e){
            this.DialogResult=DialogResult.OK;
            this.Close();
        }

    }
}]]>
        </File>
    </TemplateFiles>

    <FileOptions/>

</Template>

These templates just a simple Form constructor and add a button and a click event handler. You may change the code to your need.

Attached are the complete new files and patch files for Makefile and CSharpBinding.addin.xml and the changed compiled files: DOWNLOAD:MonoDevelop 2.4 WinForm Templates - (Hits: 0, size: 255.45 kB)

<!-- Social Bookmarks BEGIN --> <!-- Social Bookmarks END -->

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

hjgode

Germany Germany
No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 Pinmemberfmaeseele21-Oct-10 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 21 Oct 2010
Article Copyright 2010 by hjgode
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid