Background:
I have a C# Windows Forms application that contains a Windows service and an interface used to configure system settings as well as communicate with the service.
Desired Outcome:
I would like to build two versions of the solution – a client version with all the Windows service related code and form elements and a server version that contains everything.
The form contains a tabbed control, where one tab contains elements used to interface with the Windows service using sockets. All I’m really trying to achieve is that for a full build the tab containing service related elements is compiled, while for a conditional build the same tab is excluded.
Problem:
At this stage I’ve used #if directives around the Windows service related code. For example:
#if SERVERBUILD
//Code relating to Windows service that I do not want to compile
//for a client version.
#endif
In the above example, ‘SERVERBUILD’ corresponds to a build configuration that I can select via the Configuration Manager (as opposed to the standard ‘Release’ build option).
The issue I’m having is that some of the code I’ve had to wrap this #if directive around lies in the WinForm.Designer.cs file in the region titled ‘Windows Form Designer generated code‘.
What seems to be occurring is that when I make a change to some of the form properties, this entire region of code seems to be deleted and re-generated, thereby removing the #if sections I had added.
- Am I going about this the right way?
- Is there a way to avoid the situation where I am losing the changes I’ve made in the WinForm.Designer.cs code?
I would really appreciate any advice from anyone with experience with conditional compilation and this sort of stuff.
Nuff said. Solve your problem by putting the code in the form constructor:
Note that using a panel is an easy way to make all the controls that are on it invisible.