Inherit Generic Forms

How can generic forms / controls be inherited

I made some generic forms and controls i want to use in my application as base forms/controls.

When i inherit these forms/controls i cannot view the designer anymore.

Is it possible to inherit these generics and how



Answer this question

Inherit Generic Forms

  • SuperJames

    Nightmare_BE wrote:
    How do u write the code, when u look at the code of a new form both the form and the designer class are partial classes. Do u put the public class MyCustomIntForm : MyGenericForm<int> { } on top of the form class, at the designer or into a external code file

    It can't be the first class in the new form's .cs file, because the designer will crack it anyway if you do that.

    I just put that single line of code at the very bottom of the new form's .cs file. So:

    namespace Fred
    {
    public partial class MyIntForm : MyCustomIntForm
    {
    /* ... */
    }

    public class MyCustomIntForm : MyGenericForm<int> { }
    }

    ... just like that.


  • Dave Perry

    You're in luck - I discovered a workaround for this just the other day, and blogged about it:

    http://www.madprops.org/cs/blogs/mabster/archive/2006/09/19/Designing-Generic-Forms.aspx

    hth,
    Matt


  • ershad

    Yeah, reusing UI-logic with different "business classes" was exactly the reason I looked at this stuff.

    Good luck with the toolbar/resource thing. Having to do it in code would almost be an acceptable trade-off given how "nice" the inheritence thing works, and how little duplicated code you end up with.


  • Rattlerr

    Now i have an other issue with the toolbar ... so my other post

    http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=763020&SiteID=1

     

    This is what i'm trying to do:

    I'm using data business objects, so i made little framework for it.
    Each data class inherits the class record
    Then i have datalist and datasource classes, all generic.

    Example: customers.

    class Customer : Record
    class CustomerData : DataSource<Customer>

    in my form i have this code

    private DataList<Customer> m_data;
    private CustomerData m_dataSource;

     

    So then i have for each data from the add, edit and remove buttons, so i only want to write the main code once, using a generic form.

    This is my goal:

    class Customer : Record
    class CustomerData : DataSource<Customer>

    class DataForm<DataType, DataSource> : Form
    {
       private DataList<DataType> m_data;
       private DataSource m_dataSource;

       private void RefreshData()
       {
          m_dataSource.FillData(m_data);
       }
    }

     

    now the refreshdata method i have to rewrite in each data from while the code is allways the same, only the datasource class type is different.


  • NeederOfVBHelp

    yeah, might work to get the images from the global resources, but when adding the buttons trough the designer u get a compilation error

    It allwasy changes the designer code back to

    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BaseForm));

    So u have to change it manually back to

    System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(BaseForm<T>));

    Here's how i do it now

    I have base form (just i form inheriting System.Windows.Forms.Form) that contains the toolbar
    Then i made the generic form inheriting my base form, so inheriting the toolbar
    Then i made my datatype specific classes inheriting the generic form

    /************************************************
    * BaseForm.cs
    ************************************************/

    namespace
    GenericForms1
    {
    public partial class BaseForm : Form
    {
    public BaseForm()
    {
    InitializeComponent();
    }
    }
    }

    /************************************************
    * GenericBaseForm.cs
    ************************************************/

    namespace
    GenericForms1
    {
    public partial class GenericBaseForm<DataType> : BaseForm
    {
    public GenericBaseForm()
    {
    InitializeComponent();
    }
    }

    public class GenericBaseFormIntWrapper : GenericBaseForm<int> { }
    public class GenericBaseFormStringWrapper : GenericBaseForm<string> { }
    }

    /************************************************
    * IntForm.cs
    ************************************************/
    namespace
    GenericForms1
    {
    public partial class IntForm : GenericBaseFormIntWrapper
    {
    public IntForm()
    {
    InitializeComponent();
    }
    }
    }


  • Paras Dhawan

    I got it working, but i ran into another problem with the workaround, resources.

    On my main (generic) form i placed a toolbar.

    Now i get the error:

    Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "GenericForms1.GForm`1.resources" was correctly embedded or linked into assembly "GenericForms1" at compile time, or that all the satellite assemblies required are loadable and fully signed.

    Maybe i need to change the generic form designer code to load the resources from somewhere else or start with a non generic form as base form, inherit the base form into the generic form, make the generic form wrappen and inherit the final form by the wrapper (what a mess )


  • Tom Dierickx

    Great workaround :)

    thanks.

    How do u write the code, when u look at the code of a new form both the form and the designer class are partial classes. Do u put the public class MyCustomIntForm : MyGenericForm<int> { } on top of the form class, at the designer or into a external code file


  • morgan_

    Damn - yeah, you're in new territory now. The successes I've had with this stuff haven't included any forms or usercontrols with embedded resources.

    Inheriting from a non-generic base form sounds reasonable.

    Are these resources just the button images on the toolbar Could you maybe add those as project resources instead, and just point the buttons at those That might work.


  • Inherit Generic Forms