如何在Win Forms项目中创建一个用户控件的DLL?

7

我有一个在项目中创建的用户控件。 当我编译项目时,我会看到项目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);
            }
        }
    }
}

一个建议,不要给类(或任何类型)和命名空间取相同的名称。 - Mike Zboray
1个回答

12

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接