Creating Custom Controls with generics

让人想犯罪 __ 提交于 2019-12-10 09:54:08

问题


We wish to create a WinForms Custom Control (which is derived from a .net control) and be able to drag and drop it from the tool box in the designer view. However we are unable to this whenever we have a control with generics because when the designer tries to create an instance of the class, for obvious reasons, it doesn't know what type the instance must be of.

Any one knows of a way around this ?

Thanks in advance

======edit===========

What we would like to do is have example:

public class MyDataGridView<T> : DataGridView where T : class{
...
}

by removing the generics we can see the preview in the designer as if it where a normal DataGridView but as I explained above with generics we are unable to use the designer.


回答1:


I have faced the same problem and the upshot was: Don't use generic controls in the designer view! You can programatically create instances of it, but you have no preview. Maybe it works when you add a derived class which explicitly sets the generic parameter.



来源:https://stackoverflow.com/questions/14563612/creating-custom-controls-with-generics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!