1

I'm dealing with a Winforms problem while I'm trying to refactor the code. The code-behind of the form looks like this:

namespace SIC.AplicacionEscritorio.MySolution
{
    public partial class A : B
    {
        public A(Document document)
        : base(document)
        {
            //definition for constructor
        }

        public override void Method1()
        {
            //definition for method1 in form
        }

        protected override void Method2()
        {
            //definition for method2 in form
        }        
    }

    public class B : C <Scheme.SomeDataTable, Scheme.SomeRow>
    {
        public B() { }
        public B(Document document) : base(document) { }

        public override void Method1()
        {
            throw new NotImplementedException(); //this is SO ugly
        }

        protected override void Method2()
        {
            throw new NotImplementedException();
        }                
    }

    public abstract class C <T, R> : Form
        where T : DataTable, new()
        where R : DataRow
    {

        public C(Document document)
            : this()
        {
              //main constructor
        }

        public abstract void Method1();

        protected abstract void Method2();        
    }
}

If you look the code, it seems that class B is not really needed. C is the class which inherits from Form.

According with those questions:

"Illegal characters in path." Visual Studio WinForm Design View "the designer doesn't support base forms that are generic"

Generic forms and VS designer "Generics break the designer because it cannot instantiate the class without a type T"


So, In order to refactor the code, I tried to do something like this, but it throws an error "Illegal characters in path." (same as 1st question).

namespace SIC.AplicacionEscritorio.MySolution
{
    public partial class A : C <Scheme.SomeDataTable, Scheme.SomeRow>
    {
        public A(Document document)
        : base(document)
        {
            //definition for constructor
        }

        public override void Method1()
        {
            //definition for method1 in form
        }

        protected override void Method2()
        {
            //definition for method2 in form
        }        
    }

    public abstract class C <T, R> : Form
        where T : DataTable, new()
        where R : DataRow
    {

        public C(Document document)
            : this()
        {
              //main constructor
        }

        public abstract void Method1();

        protected abstract void Method2();        
    }
}

Apparently, we have to create an intermediary class that allows the designer to create the instance, but I need to say that it doesn't looks really nice. The question is:

Do I need to leave the code as it was? Is there some object-oriented trick to solve it?

halfer
  • 19,824
  • 17
  • 99
  • 186
Mauro Bilotti
  • 5,628
  • 4
  • 44
  • 65
  • 1
    Currently it's not possible to use the designer if you want to design generic winforms. Maybe in future, but actually not. – vandango Aug 13 '14 at 13:07
  • @vandango That's not entirely accurate. Generally it is correct, but there is a workaround or two. – Adam Houldsworth Oct 31 '14 at 12:54
  • @Mauro `B` is needed for exactly this problem, it is the workaround in action. Do what the previous developer didn't and comment it as such, warning people not to remove it and providing links to resources on the topic. There is also [this solution](http://wonkitect.wordpress.com/2008/06/20/using-visual-studio-whidbey-to-design-abstract-forms/) linked in the answer to the question you linked. My solution involved compiler flags to remove it at release time to remove junk code for designer support. – Adam Houldsworth Oct 31 '14 at 12:56
  • @Adam Houldsworth: There are only working workarounds for generic controls but not for generic windows. – vandango Dec 05 '14 at 13:17
  • @vandango I used to have generic forms, it's possible. – Adam Houldsworth Dec 05 '14 at 13:31
  • @Adam Houldsworth: Do you have a link to an example? I'm interessting in it... – vandango Dec 05 '14 at 13:47
  • @vandango The second link in the question features an answer from me on how to do it and get designer support. The linked duplicate answer also uses the same mechanism, so it'll work for both controls and forms. – Adam Houldsworth Dec 05 '14 at 13:49

0 Answers0