I’m not too much into Windows Application Programming because i tend to concentrate on building applications for the Web. However the other day i wanted to do a little application for Windows and i got stuck a bit. I usually write a small app with just 1 form but when your app contains more than one Form, then you really need to think how you are going to design it.
First things first – you can have a main Form which loads subsequent forms through an MDIForm interface. This means that you choose one form to be the container (MDIParent) and the other to be the Form you want to load (MDIChild) . I personally do not like the hassle of having to do this.
What do i do then? Well, i tend to have just one Form in my application which contains a panel. Instead of creating many other Forms, i create User Controls which i load on the main panel of my ONLY Form. All you need to make sure is that you clear all the controls of your main panel before loading a User Control on it.
pnlMain.Controls.Clear();
MyUserControl _muc = new MyUserControl();
pnlMain.Controls.Add(_muc);
If you have multiple Forms in your application though and you need to nominate one of them to be the default Form so that when you start your application, this particular Form is loaded, you need to edit your program.cs file as follows:
// we want to make Form2 load when application starts instead of Form1
Application.Run(new Form2());
One other thing i’ve noticed about Windows Programming is that if you wanted to have a separate config section in your application, it is not so easily doable. Say for example, i wanted to have an external config file for my connection strings instead of dumping them into the App.config file. If i store the external config file in a subfolder like Config/connections.config and reference it in my App.config as follows:
<connectionStrings configSource=”Config\connections.config”/>
The connection string will not be picked up by the program because it would not find the external file. When you compile your application, the App.config will be re-created as ProjectName.exe.config in your Debug/Release folder. Suppose my project is called Gices, i will have a Gices.exe.config file in my Debug folder which will reference a path to the external connections.config file and because my actual Config/connections.config is two level up, the program will not find it. Now you can say that if we write the path as ../../Config/Config.connections, it will work out fine. Well that’s not true because all path in the App.config or Web.config needs to be both relative and referencing a path in either the same directory or a sub-directory BUT not going any levels up. Therefore you will need to create the Config/connections.config in your Debug/Release folder for your application for it to work.
When you deploy your application now, you will need to have the config files attached. If you need to attach/include any files or folders for your application, the best thing to do is create a Setup Project for your application and add the Project Output to the setup. Once this is done, you can right click on Application Folder [located under File System on Target Machine] and select Add, followed by either folder/file and add it. This will ensure that your folder/file is included when somebody runs the setup for your application.