C# 如何在win forms项目中创建用户控件的dll?

C# 如何在win forms项目中创建用户控件的dll?,c#,C#,我在项目中创建了这个用户控件。 编译项目时,我会看到项目dll。 但是,当我编译这个项目时,它将创建一个用户控件的dll,这样在以后的其他项目中,我就可以将这个用户控件dll添加到我的工具箱中了 /*---------------------------------------------------------------- * Module Name : ListBoxControl * Description : Change listBox items color * Auth

我在项目中创建了这个用户控件。 编译项目时,我会看到项目dll。 但是,当我编译这个项目时,它将创建一个用户控件的dll,这样在以后的其他项目中,我就可以将这个用户控件dll添加到我的工具箱中了

/*----------------------------------------------------------------
 * Module Name  : ListBoxControl
 * Description  : Change listBox items color
 * Author       : Danny
 * Date         : 30/12/2012
 * Revision     : 1.00
 * --------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

/*
 *  Introduction :
 * 
 *  By default the color is red.
 *  Added a property to change the color.
 *  Right mouse click on item to change item color.
 *  Left mouse click on item to change the item color back. 
 * */

namespace ListBoxControl
{
    public partial class ListBoxControl : UserControl
    {
        Color m_MyListColor;
        private List<int> m_itemIndexes = new List<int>();

        public ListBoxControl()
        {
            InitializeComponent();

            for (int i = 0; i < 10; i++)
            {
                listBox1.Items.Add("Test " + i);
            }
        }

        private void listBox1_MouseDown(object sender, MouseEventArgs e)
        {
            int index = listBox1.IndexFromPoint(e.X, e.Y);
            listBox1.SelectedIndex = index;

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                if (m_itemIndexes.Contains(index))
                    return;

                m_itemIndexes.Add(index);
                DrawItem(index);
            }
            else if (e.Button == MouseButtons.Left)
            {
                if (!m_itemIndexes.Contains(index))
                    return;

                m_itemIndexes.Remove(index);
                DrawItem(index);
            }
        }
    }
}
/*----------------------------------------------------------------
*模块名称:ListBoxControl
*描述:更改列表框项目的颜色
*作者:丹尼
*日期:30/12/2012
*修订:1.00
* --------------------------------------------------------------*/
使用制度;
使用System.Collections.Generic;
使用系统组件模型;
使用系统图;
使用系统数据;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
/*
*导言:
* 
*默认情况下,颜色为红色。
*添加了一个属性以更改颜色。
*在项目上单击鼠标右键以更改项目颜色。
*在项目上单击鼠标左键以更改项目颜色。
* */
命名空间ListBoxControl
{
公共部分类ListBoxControl:UserControl
{
颜色m_MyListColor;
私有列表m_itemIndexes=新列表();
公共ListBoxControl()
{
初始化组件();
对于(int i=0;i<10;i++)
{
列表框1.项目。添加(“测试”+i);
}
}
私有无效列表框1u MouseDown(对象发送方,MouseEventArgs e)
{
int index=listBox1.IndexFromPoint(e.X,e.Y);
listBox1.SelectedIndex=索引;
if(e.Button==System.Windows.Forms.MouseButtons.Right)
{
if(m_itemIndexes.Contains(index))
返回;
m_itemIndexes.Add(索引);
项目(索引);
}
else if(e.Button==MouseButtons.Left)
{
如果(!m_itemIndexes.Contains(index))
返回;
m_itemIndexes.Remove(索引);
项目(索引);
}
}
}
}

您将需要创建一个单独的项目,该项目的类型为添加您的用户控件。它的输出类型为类库。编译后,您可以通过右键单击并选择“选择项”并浏览到dll的位置,将其添加到工具箱中。

有一条建议,不要给类(或任何类型)和命名空间赋予相同的名称。