使用泛型创建自定义控件

编程入门 行业动态 更新时间:2024-10-04 15:26:14
本文介绍了使用泛型创建自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我们希望创建一个WinForms自定义控件(从控件派生),并能够将其从设计器视图的工具箱中拖放。但是,只要有通用控件,我们就无法做到这一点,因为当设计者试图创建类的实例时,出于明显的原因,它不知道实例必须属于哪种类型。

We wish to create a WinForms Custom Control (which is derived from a 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 ?

预先感谢

======编辑========== =

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

我们想做的例子:

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

泛型,我们可以在设计器中看到预览,就像在普通DataGridView一样,但是正如我上面对泛型所做的解释,我们无法使用设计器。

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.

推荐答案

我遇到了同样的问题,结果是:不要在设计器视图中使用通用控件! 您可以以编程方式创建它的实例,但没有预览。 也许在添加显式设置泛型参数的派生类时有效。

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.

更多推荐

使用泛型创建自定义控件

本文发布于:2023-11-26 23:22:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635627.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   控件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!