{"id":19,"date":"2016-02-10T16:25:07","date_gmt":"2016-02-10T16:25:07","guid":{"rendered":"https:\/\/ibex.tech\/csharp\/?p=19"},"modified":"2022-09-13T15:59:22","modified_gmt":"2022-09-13T14:59:22","slug":"creating-a-windows-forms-project-from-scratch","status":"publish","type":"post","link":"https:\/\/ibex.tech\/csharp\/c-sharp\/projects-general-c-sharp\/creating-a-windows-forms-project-from-scratch","title":{"rendered":"Creating A Windows Forms Project From scratch"},"content":{"rendered":"<h4>\n\tThis is our internal check list to create a new Windows Forms Project&#8230;<\/h4>\n<p>Create a \u2018Visual C#\u2019 &gt; &#8216;Windows&#8217; &gt; \u2018Windows Forms Application\u2019.<\/p>\n<p>Once created:-<\/p>\n<p style=\"padding-left: 30px;\">\n\tRe-name the created form to frmMain.h by right clicking it in the solution explorer and selecting &#8216;Rename&#8217; &nbsp;Click Yes to updating all references.<\/p>\n<h4>\n\tThings To Add To The Default Form Class<\/h4>\n<h5>\n\tClass constructor:<\/h5>\n<pre><code>\n    public partial class frmMain : Form\n    {\n\t\t\/\/---------------------------\n\t\t\/\/----- PRIVATE DEFINES -----\n\t\t\/\/---------------------------\n\t\t\/\/private \n\n\t\t\/\/--------------------------\n\t\t\/\/----- PUBLIC DEFINES -----\n\t\t\/\/--------------------------\n\t\t\/\/public \n\t\n\t\t\/\/---------------------------\n\t\t\/\/----- PRIVATE OBJECTS -----\n\t\t\/\/---------------------------\n\t\t\/\/private \n\n\t\n\t\t\/\/--------------------------\n\t\t\/\/----- PUBLIC OBJECTS -----\n\t\t\/\/--------------------------\n\t\t\/\/public \n\n\t\t\/\/*********************************\n\t\t\/\/*********************************\n\t\t\/\/********** CONSTRUCTOR **********\n\t\t\/\/*********************************\n\t\t\/\/*********************************\n\t\tpublic frmMain()\n\t\t{\n<\/code><\/pre>\n<h5>\n\tProject &gt; Add Class &gt; Create ApMain.cs&nbsp;and set it up like this:<\/h5>\n<pre><code>\n\n\/\/*************************************\n\/\/*************************************\n\/\/********** VERSION HISTORY **********\n\/\/*************************************\n\/\/*************************************\n\/\/\n\/\/V1.00\t##\/##\/##\t\tDeveloper Name\n\/\/- Original release\n\n\/\/----- NEW VERSION CHECKLIST -----\n\/\/- Set MyProjectName &gt; Properties &gt; AssemblyInfo.cs &gt; AssemblyFileVersion and AssemblyVersion (use the \"1.0.*\" format)\n\n\nnamespace MyProjectNamespaceName\n{\n    public static class ApMain      \/\/&lt;&lt;&lt;&lt;&lt; ADD 'public static' TO THIS CLASS TO MAKE IT GLOBAL (STATIC SO NO INSTANCES CAN BE CREATED OF IT) &lt;&lt;&lt;&lt;\n    {\n        \/\/############################\n        \/\/############################\n        \/\/##### GLOBAL CONSTANTS #####\n        \/\/############################\n        \/\/############################\n        \/\/Use 'const' (no need for 'static' as const is static by nature)\n        public const string MyGlobalString1 = \"Hello1\";\n        public const int MyGlobalInt1 = 1;\n\n        \/\/############################\n        \/\/############################\n        \/\/##### GLOBAL VARIABLES #####\n        \/\/############################\n        \/\/############################\n        \/\/Use 'static' (all members inside a static class must also be static)\n        public static string MyGlobalString2 = \"Hello2\";\n        public static int MyGlobalInt2 = 2;\n\n        \/\/Use them in your code like this:\n        \/\/SomeString = ApMain.MyGlobalString1;\n        \/\/SomeValue = ApMain.MyGlobalInt2;\n    }\n<\/code><\/pre>\n<h5>\n\tfrmMain.cs<\/h5>\n<p>Set the &#8216;Form Border Style&#8217; to Fixed Single (or as required)<br \/>\nSet &#8216;MaximiseBox&#8217; and &#8216;MinimizeBox&#8217; properties<br \/>\nSet StartPostion (typically WindowsDefaultLocation)<br \/>\nSet &#8216;Text&#8217;<br \/>\nSet &#8216;icon&#8217; (or turn off ShowIcon)<\/p>\n<h5>\n\tProgram.cs<\/h5>\n<p>Add the following try block around the main function contents:-<\/p>\n<pre><code>\n    static void Main()\n    {\n        try     \/\/Using a try block here to try and get details of any windows errors that are not properly handled\n        {\n            Application.EnableVisualStyles();\n            Application.SetCompatibleTextRenderingDefault(false);\n            Application.Run(new frmMain());\n        }\n        catch (Exception err)\n        {\n            MessageBox.Show(\"Critical Application Error\\nPlease copy this message when reporting:\\n\\n\" + err, \"Error\", MessageBoxButtons.OK, MessageBoxIcon.Error);\n        }\n    }\n<\/code><\/pre>\n<h5>\n\tProgram.cs<\/h5>\n<p>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&#8217;t exit correctly.<\/p>\n<pre><code>\n    \/\/----- CHECK FOR APPLICATION ALREADY RUNNING (MULTIPLE INSTANCES ARE NOT PERMITTED) -----\n    if (System.Diagnostics.Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Count() &gt; 1)\n    {\n        MessageBox.Show(\"There is already another instance of the application running\", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\n        return;\n    }\n\tApplication.EnableVisualStyles();\n\tApplication.SetCompatibleTextRenderingDefault(false);\n    Application.Run(new frmMain());\n<\/code><\/pre>\n<h5>\n\tFile Version<\/h5>\n<p>For the .exe to have a file version you have to do this<br \/>\nYourProjectName &gt; Properties (there is an item in the project tree called properties!) &gt; AssemblyInfo.cs &gt;&nbsp;Double click it.<br \/>\nSet the following:<\/p>\n<p style=\"padding-left: 30px;\">\n\tAssemblyTitle and AssemblyProduct = &nbsp;as requried (ap name) (used in about form)<br \/>\nAssemblyDescription = [blank] or give description of what ap is<br \/>\nAssemblyCompany = as required<br \/>\nAssemblyCopyright as required (used in about form)<\/p>\n<h5>\n\tIcon<\/h5>\n<p>Copy your icon file (e.g. app.ico) into your project files directory<\/p>\n<p>Project Properties &gt; Application. &nbsp;In the &#8216;Resources&#8217; box select it using the &#8216;&#8230;&#8217; button.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is our internal check list to create a new Windows Forms Project&#8230; Create a \u2018Visual C#\u2019 &gt; &#8216;Windows&#8217; &gt; \u2018Windows Forms Application\u2019. Once created:- Re-name the created form to frmMain.h by right clicking it in the solution explorer and selecting &#8216;Rename&#8217; &nbsp;Click Yes to updating all references. Things To Add To The Default Form [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[124],"tags":[],"class_list":["post-19","post","type-post","status-publish","format-standard","hentry","category-projects-general-c-sharp"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/19","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":10,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":1212,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/posts\/19\/revisions\/1212"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/csharp\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}