0

I have created a generic usercontrol like this:

public class MyControl<T>: TabControl
{

}

If T was of primitive types, I would make my usercontrol non-generic and I would give an option to the user as a property to select the type, however T can be any class type.

My usercontrol works perfectly when added programmatically, however, when I add it from toolbox there are 3 problems:

  1. When added shows a blank control (not a TabControl)

I Expect to see a normal TabControl and it has nothing with its being generic

  1. I have to go to Form.Designer.cs file and change its declarations to match the type I want

like:

private MyControl myControl1;

to

private MyControl<AClass> myControl1;

and

this. myControl1 = new MyControl();

to

this. myControl1 = new MyControl<AClass>();

I know that when control added it can not predict what the class would be, I'm not expecting some magic, but Is there a way that the user wouldn't need to go to the designer each time they add a control and somehow solve the problem with a property may be?

  1. After making changes in the Designer.cs the form designer shows errors and can not show the designer.

This one is on my nerves and why even having a generic declaration would cause error for designer (although when running the application it runs with no problem).

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
  • Have you considered adding designer properies or editors? I'm wondering if you need to create a proxy control first that allows the user to pick the generic type –  Mar 30 '19 at 01:28
  • it is not a proxy control, it basically generates some tables from tables filled with values of the given class (that I get using reflection), but yes I want the user be able to select a type – Ashkan Mobayen Khiabani Mar 30 '19 at 01:34
  • @MickyD I have looked in the question that you have suggested as duplicate before asking my question. It shows how to declare it programmatically which I had done it myself with no problem. It does not suggest anything about design time. – Ashkan Mobayen Khiabani Mar 30 '19 at 01:36
  • 1
    That duplicate *is* about design time. – LarsTech Mar 30 '19 at 01:39

0 Answers0