簡體   English   中英

如何在Visual Studio 2015中創建自定義控件並將其添加到工具箱

[英]how to create custom control in visual studio 2015 and add it to toolbox

我想創建一個在類庫項目中繪制表格的控件,並將此dll添加到工具箱中,然后在我的Windows窗體應用中使用它。 我嘗試谷歌搜索,但找不到。 我該怎么辦 ?

我在課程庫項目中創建了該課程

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
namespace ClassLibrary1
{
class PanelZ : System.Windows.Forms.Panel
{
    private Color color1 = Color.SteelBlue;
    private Color color2 = Color.DarkBlue;
    private int color1Transparent = 150;
    private int color2Transparent = 150;
    private int angle = 90;

    public Color StartColor
    {
        get { return color1; }
        set { color1 = value; Invalidate(); }
    }
    public Color EndColor
    {
        get { return color2; }
        set { color2 = value; Invalidate(); }
    }
    public int Transparent1
    {
        get { return color1Transparent; }
        set
        {
            color1Transparent = value;
            if (color1Transparent > 255)
            {
                color1Transparent = 255;
                Invalidate();
            }
            else
                Invalidate();
        }
    }

    public int Transparent2
    {
        get { return color2Transparent; }
        set
        {
            color2Transparent = value;
            if (color2Transparent > 255)
            {
                color2Transparent = 255;
                Invalidate();
            }
            else
                Invalidate();
        }
    }

    public int GradientAngle
    {
        get { return angle; }
        set { angle = value; Invalidate(); }
    }
    public PanelZ()
    {
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Color c1 = Color.FromArgb(color1Transparent, color1);
        Color c2 = Color.FromArgb(color2Transparent, color2);
        Brush b = new System.Drawing.Drawing2D.LinearGradientBrush(ClientRectangle, c1, c2, angle);
        e.Graphics.FillRectangle(b, ClientRectangle);
        b.Dispose();
    }
}

}

但是當我將mydll添加到工具箱時,我在這里得到此錯誤圖像

在WPF或Winforms中,工具箱對於要構建的解決方案的組成部分非常聰明。 對於的WinForms,只需添加一個引用System.Windows.FormsSystem.Drawing到您的類庫,然后從繼承Control (或繼承自其他類Control )。

例如,我可以創建一個這樣的自定義控件(請注意,它必須是工具箱的公共控件才能找到它):

using System.Drawing;
using System.Windows.Forms;

namespace ClassLibrary1
{
    public class CustomControl : Control
    {
        public CustomControl()
        {
            this.BackColor = Color.Red;
        }
    }
}

構建項目后,當與應用程序中的表單進行交互時,便可以在工具箱中看到它。

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM