De Tout Et De Rien

To content | To menu | To search

Tag - Visual Studio

Entries feed - Comments feed

Friday, December 18 2009

AddDependentFiles Addin updated for Visual Studio 2008

I have update my Visual Studio adding named "Add Dependent Files" (See the original entry). It is available for download here.

Thursday, February 1 2007

Add Link to a file in Visual Studio 2005

Maybe you have already seen that in some Visual Studio 2005 projects : a item that represents a link to a file (see image below).

A linked file

It is very handy to reference through a link a file : for example, if you have three projects that uses the same file, just put a link. But when I add item to my project, I cannot see a Link or a similar option. This is because it is a bit after. Here are the steps :

1) Go to the Solution Explorer

Solution Explorer

2) Select "Add an Existing Item"

Add Existing Item

3) Select the file to add and click on the arrow at the right of the button "Add", and click on "Add as Link"

Add a file as a link

4) In Solution Explorer, you see the linked file

The linked file

Friday, December 8 2006

Dependent Files under Visual Studio 2005

A nice feature of Visual Studio 2005 is the ability to group source files by dependencies. This is what Visual Studio 2005 does when it creates a Form class or a visual control : a dependent file named from the class name is created and linked to the primary source file (as shown in the picture below).

Dependent Files generated by Visual Studio 2005

Now imagine that you have a very complicated visual control with a lot of properties, methods and events. It would be nice to split all these things in tiny source files, to make the source code browsing easier (as shown in the picture below).

Dependent Files generated by the Addin

Unfortunately, there is no option do apply the same treatment to arbitrary files in the project. That's why I have created a Visual Studio 2005 Addin to allow dependent files for any arbitrary files. It is quite straightforward, but the result is a cleaner project tree and an ease of code navigation.

Here are the steps :

  1. Create the files you want to be dependent, like UserControl1.Properties.cs. I use this convention to mirror what Visual Studio does.
  2. Make sure the new file contains partial definition of the class. The partial definition is a new feature available in the .NET Framework 2.0.
  3. Open the master file. Select the Tools/Add Dependent Files menu. Select one or more dependent files.
  4. The selected files are now dependent of the master file.

Download the Add Dependent Files Addin.

Thursday, October 5 2006

Designing generic controls in Visual Studio 2005

Maybe you have just spotted the following problem with Visual Studio 2005 : you can create an abstract form, which inherits System.Windows.Forms.Form, and modify it in the visual designer. But if you inherit it, you cannot modify it in the visual designer. You think it is kind of strange, but the explanation is pretty clear. And so is the workaround. You are now armed to modify visual control, even if they inherit from an abstract control.

But the things are slightly different when dealing with generics : you can create a generic form and modify it in the visual designer. But if you create a specialized child of this form, the visual designer refuses to open it. Even with the previous trick, there is nothing to do. After some search and some experiment, I came to the conclusion that the behaviour of the CodeDomDesignerLoader is far away from my understanding. So, are we lost in space when dealing with generic controls ?

No. The workaround to this behaviour is pretty ugly, but it has one huge advantage : it works. So here is an example.

  1. Define your generic form, like any other generic class :

        1 using System.Windows.Forms;

        2 

        3 namespace AbstractAndGeneric

        4 {

        5     public partial class GenericForm<T> : Form

        6     {

        7         public GenericForm()

        8         {

        9             InitializeComponent();

       10         }

       11     }

       12 }

  2. Define your specific form, by inheriting the generic form and specialize it with a type :

        1 using System;

        2 

        3 namespace AbstractAndGeneric

        4 {

        5     public partial class SpecificForm : GenericForm<String>

        6     {

        7         public SpecificForm()

        8         {

        9             InitializeComponent();

       10         }

       11     }

       12 }

  3. At this point, if you try to open SpecificForm in the designer, you will get a nice error message.
  4. Add the following code to the SpecificForm :

        1 using System;

        2 

        3 namespace AbstractAndGeneric

        4 {

        5 #if DEBUG

        6     public partial class SpecificForm : __SpecificForm

        7 #else

        8     public partial class SpecificForm : GenericForm<String>

        9 #endif

       10     {

       11         public SpecificForm()

       12         {

       13             InitializeComponent();

       14         }

       15     }

       16 

       17 #if DEBUG

       18     public partial class __SpecificForm : GenericForm<String>

       19     {

       20     }

       21 #endif

       22 }

The explanation is straighforward :

  • Considering the behaviour of the designer, if the base class of the control you want to design is not designable, you got an error. That is why, an intermediate class is inserted. Its sole purpose is to provide a designable class for the visual designer.
  • The use of compilation directives allows to use the trick in the Debug configuration and to have a direct inheritance in the Release configuration.
  • And I repeat, this trick only works if you design in Debug mode. Otherwise, you will get the usual error.

This is the solution I found, but I am looking forward to see an alternative solution, simpler and more elegant.