c# - 直接在 Visual Studio 项目中使用自定义控件

标签 c# listview custom-controls

我想使用此处找到的 ListView 闪烁“较少”控件 Link

直接在我的 C# 项目中。 我不想制作自定义用户控制项目,将其构建为 dll,然后将其导入我的项目。我只想在我正在制作的 C# 程序中使用这一切。

我想我必须在我的项目中添加一个类,并添加代码,但我现在如何直接在我的项目中使用该控件?

最佳答案

在 Visual Studio 中,右键单击您的项目,然后单击 ADD |用户控制。将新控件命名为 ListViewNF 并单击 ADD

查看新类的代码。更改此行:

public partial class ListViewNF : UserControl

为此:

public partial class ListViewNF : ListView

和重建。您将收到有关 AutoScaleMode 的编译器错误 - 只需删除 InitializeComponent 中导致错误的行:

// delete this line:
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

到目前为止,您的代码将如下所示:

public partial class ListViewNF : ListView 
{ 
    public ListViewNF() 
    {
        InitializeComponent();
    }
}

改成这样:

public partial class ListViewNF : ListView
{
    public ListViewNF()
    {
        InitializeComponent();

        //Activate double buffering
        this.SetStyle(ControlStyles.OptimizedDoubleBuffer | 
            ControlStyles.AllPaintingInWmPaint, true);

        //Enable the OnNotifyMessage event so we get a chance to filter out 
        // Windows messages before they get to the form's WndProc
        this.SetStyle(ControlStyles.EnableNotifyMessage, true);
    }

    protected override void OnNotifyMessage(Message m)
    {
        //Filter out the WM_ERASEBKGND message
        if (m.Msg != 0x14)
        {
            base.OnNotifyMessage(m);
        }
    }

}

重建您的项目,您现在应该在左侧(右上角)的控件工具箱中看到 ListViewNF。您可以将此控件拖到设计器中的窗体上,就像普通的 ListView 一样。

关于c# - 直接在 Visual Studio 项目中使用自定义控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2574021/

相关文章:

ios - 如何在 Interface Builder 中使用自定义 UIButton?

c# - 给定字体文件,如何获取字体系列的名称?

c# - c# dll 将错误返回给调用应用程序的好方法是什么?

delphi - 获取主题 TListView 的背景颜色

java - 无法获取 ListView 来刷新或删除项目

c++ - 如何在 Qt 中制作可展开/可折叠的部分小部件

c# - 关于 model-view-presenter (C#) 的一般性问题,模型应该知道 presenter 吗?

c# - 将时间跨度列表中的所有项目计入一个时间跨度

android:使用 ListAdapter 和 SimpleCursorAdapter 刷新 ListView

c# - 我可以在自定义 WPF Canvas 控件中绘制点网格吗?