This is our internal check list to create a new Windows Forms Project…

Create a ‘Visual C#’ > 'Windows' > ‘Windows Forms Application’.

Once created:-

Re-name the created form to frmMain.h by right clicking it in the solution explorer and selecting 'Rename'  Click Yes to updating all references.

Things To Add To The Default Form Class

Class constructor:

    public partial class frmMain : Form
    {
		//---------------------------
		//----- PRIVATE DEFINES -----
		//---------------------------
		//private 

		//--------------------------
		//----- PUBLIC DEFINES -----
		//--------------------------
		//public 
	
		//---------------------------
		//----- PRIVATE OBJECTS -----
		//---------------------------
		//private 

	
		//--------------------------
		//----- PUBLIC OBJECTS -----
		//--------------------------
		//public 

		//*********************************
		//*********************************
		//********** CONSTRUCTOR **********
		//*********************************
		//*********************************
		public frmMain()
		{
Project > Add Class > Create ApMain.cs and set it up like this:


//*************************************
//*************************************
//********** VERSION HISTORY **********
//*************************************
//*************************************
//
//V1.00	##/##/##		Developer Name
//- Original release

//----- NEW VERSION CHECKLIST -----
//- Set MyProjectName > Properties > AssemblyInfo.cs > AssemblyFileVersion and AssemblyVersion (use the "1.0.*" format)


namespace MyProjectNamespaceName
{
    public static class ApMain      //<<<<< ADD 'public static' TO THIS CLASS TO MAKE IT GLOBAL (STATIC SO NO INSTANCES CAN BE CREATED OF IT) <<<<
    {
        //############################
        //############################
        //##### GLOBAL CONSTANTS #####
        //############################
        //############################
        //Use 'const' (no need for 'static' as const is static by nature)
        public const string MyGlobalString1 = "Hello1";
        public const int MyGlobalInt1 = 1;

        //############################
        //############################
        //##### GLOBAL VARIABLES #####
        //############################
        //############################
        //Use 'static' (all members inside a static class must also be static)
        public static string MyGlobalString2 = "Hello2";
        public static int MyGlobalInt2 = 2;

        //Use them in your code like this:
        //SomeString = ApMain.MyGlobalString1;
        //SomeValue = ApMain.MyGlobalInt2;
    }
frmMain.cs

Set the 'Form Border Style' to Fixed Single (or as required)
Set 'MaximiseBox' and 'MinimizeBox' properties
Set StartPostion (typically WindowsDefaultLocation)
Set 'Text'
Set 'icon' (or turn off ShowIcon)

Program.cs

Add the following try block around the main function contents:-


    static void Main()
    {
        try     //Using a try block here to try and get details of any windows errors that are not properly handled
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmMain());
        }
        catch (Exception err)
        {
            MessageBox.Show("Critical Application Error\nPlease copy this message when reporting:\n\n" + err, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
Program.cs

If you want to prevent multiple instances of the application from running then add this before the Application::Run(gcnew frmMain());. You need to do this here and not in the main form constructor as the application won't exit correctly.


    //----- CHECK FOR APPLICATION ALREADY RUNNING (MULTIPLE INSTANCES ARE NOT PERMITTED) -----
    if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() > 1)
    {
        MessageBox.Show("There is already another instance of the application running", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        return;
    }
	Application.EnableVisualStyles();
	Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new frmMain());
File Version

For the .exe to have a file version you have to do this
YourProjectName > Properties (there is an item in the project tree called properties!) > AssemblyInfo.cs > Double click it.
Set the following:

AssemblyTitle and AssemblyProduct =  as requried (ap name) (used in about form)
AssemblyDescription = [blank] or give description of what ap is
AssemblyCompany = as required
AssemblyCopyright as required (used in about form)

Icon

Copy your icon file (e.g. app.ico) into your project files directory

Project Properties > Application.  In the 'Resources' box select it using the '…' button.

 

 

 

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.