如何使用c#

时间:2015-07-21 02:49:24

标签: c#

如何创建没有图形的用户控件?

当我使用Windows Form Control Library创建用户控件时,我看到了这个命令行:public partial class UserControl1: UserControl。 我的朋友告诉我用UserControl1: UserControl替换UserControl1:Component,IDisposable。 我不知道Component,IDisposable是什么。

2 个答案:

答案 0 :(得分:2)

如果你看一下你可以放在没有自己界面的表格上的其他组件,你可以看到你的朋友在谈论什么:

public class BackgroundWorker : Component

public class ErrorProvider : Component, IExtenderProvider, ISupportInitialize

public class Timer : Component

您可以轻松创建自己的:

public class MyVeryOwnComponent : Component
{
    public string Setting1 { get; set; }
    public string Setting2 { get; set; }

    public void SomeImportantMethodYouCanCall()
    {
       // ...
    }
}

将其放在您的表单上:(您必须先重建,才能让它在工具箱中显示)

enter image description here

答案 1 :(得分:1)

你所说的是一个“组件”,它是从System.ComponentModel.Component派生的任何类。控件是一种特殊类型的组件,它派生自类System.Windows.Forms.Control

如果您想制作自己的组件,只需让您的班级来自Component,这就是您的朋友对:Component,IDisposable所做的事情。不需要IDisposable部分,因为Component已经实现IDisposeable并且可能会被取消。