从vb.net转换为C#自定义控件不会显示在工具箱中

从vb.net转换为C#自定义控件不会显示在工具箱中,c#,vb.net,dll,visual-studio-2015,C#,Vb.net,Dll,Visual Studio 2015,我有一些我已经使用了一段时间的自定义控件。几天前,我做了一个自定义复选框。因为我不太懂C#语法,所以我是在VB.NET中编写的。问题是互联网上的大多数免费自定义控件都是C语言的。我的解决方案是将它们编译成DLL并在我的应用程序中使用 我继续把它转换成C#,并试图将它添加到我的DLL中。它根本不会出现在工具箱中。我想我在代码中遗漏了VB.NET不需要的东西,但我不知道它是什么 下面是VB.NET版本,它创建了一个合适的DLL,为我提供了自定义工具 Public Class ColorCheckBo

我有一些我已经使用了一段时间的自定义控件。几天前,我做了一个自定义复选框。因为我不太懂C#语法,所以我是在VB.NET中编写的。问题是互联网上的大多数免费自定义控件都是C语言的。我的解决方案是将它们编译成DLL并在我的应用程序中使用

我继续把它转换成C#,并试图将它添加到我的DLL中。它根本不会出现在工具箱中。我想我在代码中遗漏了VB.NET不需要的东西,但我不知道它是什么

下面是VB.NET版本,它创建了一个合适的DLL,为我提供了自定义工具

Public Class ColorCheckBox
    Inherits CheckBox
    Public CheckColor As New SolidBrush(Color.Blue)
    Public IndeterminateColor As New SolidBrush(Color.Red)
    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim CheckRegion As New Rectangle(1, 2, 11, 11)

        Dim Points(15) As Point

        Points(0) = New Point(1, 2)
        Points(1) = New Point(4, 2)
        Points(2) = New Point(6, 5)
        Points(3) = New Point(9, 2)
        Points(4) = New Point(12, 2)
        Points(5) = New Point(12, 4)
        Points(6) = New Point(9, 7)
        Points(7) = New Point(12, 10)
        Points(8) = New Point(12, 13)
        Points(9) = New Point(9, 12)
        Points(10) = New Point(6, 9)
        Points(11) = New Point(3, 13)
        Points(12) = New Point(1, 12)
        Points(13) = New Point(1, 10)
        Points(14) = New Point(4, 7)
        Points(15) = New Point(1, 4)

        MyBase.OnPaint(e)

        If CheckState = CheckState.Checked Then
            e.Graphics.FillRectangle(New SolidBrush(Color.White), CheckRegion)
            e.Graphics.FillPolygon(CheckColor, Points)
        ElseIf CheckState = CheckState.Indeterminate Then
            e.Graphics.FillRectangle(New SolidBrush(Color.White), CheckRegion)
            e.Graphics.FillPolygon(IndeterminateColor, Points)

        End If
    End Sub

    Sub New()
        ThreeState = True
    End Sub

    Public ReadOnly Property DBValue As Integer
        Get
            Select Case CheckState
                Case CheckState.Unchecked
                    Return 0
                Case CheckState.Checked
                    Return 1
                Case CheckState.Indeterminate
                    Return 2
                Case Else
                    Return 3
            End Select
        End Get
    End Property
End Class
下面是转换后的代码,它不起作用。它编译得很好,但不会给我像上面那样的自定义控件

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

namespace ColorCheckBoxCS
{
    public class ColorCheckBox : CheckBox
    {
        public SolidBrush CheckColor = new SolidBrush(Color.Blue);
        public SolidBrush IndeterminateColor = new SolidBrush(Color.Red);
        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle CheckRegion = new Rectangle(1, 2, 11, 11);

            Point[] Points = new Point[15];

            Points[0] = new Point(1, 2);
            Points[1] = new Point(4, 2);
            Points[2] = new Point(6, 5);
            Points[3] = new Point(9, 2);
            Points[4] = new Point(12, 2);
            Points[5] = new Point(12, 4);
            Points[6] = new Point(9, 7);
            Points[7] = new Point(12, 10);
            Points[8] = new Point(12, 13);
            Points[9] = new Point(9, 12);
            Points[10] = new Point(6, 9);
            Points[11] = new Point(3, 13);
            Points[12] = new Point(1, 12);
            Points[13] = new Point(1, 10);
            Points[14] = new Point(4, 7);
            Points[15] = new Point(1, 4);

            base.OnPaint(e);

            if (CheckState == CheckState.Checked)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.White), CheckRegion);
                e.Graphics.FillPolygon(CheckColor, Points);
            }
            else if (CheckState == CheckState.Indeterminate)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.White), CheckRegion);
                e.Graphics.FillPolygon(IndeterminateColor, Points);

            }
        }

        ColorCheckBox()
        {
            ThreeState = true;
        }

        public int DBValue
        {
            get
            {
                switch (CheckState)
                {
                    case CheckState.Unchecked:
                        return 0;
                    case CheckState.Checked:
                        return 1;
                    case CheckState.Indeterminate:
                        return 2;
                    default:
                        return 3;
                }
            }
        }
    }
}
另一方面,如果有人想使用此代码,请继续。它使复选框使用三种状态并绘制一个X。我将它用于两级身份验证系统。您甚至可以在代码中设置X的颜色或在控件中更改它。在VB.NET中工作得很好,如果它得到修复,在C#中也会工作得很好。我计划让它接受一个值并设置CheckState,但我首先解决了这个问题。如果代码被修复,我会用set重新发布正确的代码

我花了很长时间才得到正确的点位置,所以如果你想使用它

对于任何知道问题所在的人,请帮助。我不是一个好的销售员

修订工作守则

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

namespace ColorCheckBoxCS
{
    public class ColorCheckBox : CheckBox
    {
        /// <summary>
        /// The color of the X for Checked
        /// </summary>
        public Color CheckColor = Color.Blue;

        /// <summary>
        /// The color of the X for Indeterminate
        /// </summary>
        public Color IndeterminateColor = Color.Red;

        protected override void OnPaint(PaintEventArgs e)
        {
            SolidBrush CheckColorBrush = new SolidBrush(CheckColor);
            SolidBrush IndeterminateColorBrush = new SolidBrush(IndeterminateColor);

            Rectangle CheckRegion = new Rectangle(1, 2, 11, 11);

            Point[] Points = new Point[16];

            Points[0] = new Point(1, 2);
            Points[1] = new Point(4, 2);
            Points[2] = new Point(6, 5);
            Points[3] = new Point(9, 2);
            Points[4] = new Point(12, 2);
            Points[5] = new Point(12, 4);
            Points[6] = new Point(9, 7);
            Points[7] = new Point(12, 10);
            Points[8] = new Point(12, 13);
            Points[9] = new Point(9, 12);
            Points[10] = new Point(6, 9);
            Points[11] = new Point(3, 13);
            Points[12] = new Point(1, 12);
            Points[13] = new Point(1, 10);
            Points[14] = new Point(4, 7);
            Points[15] = new Point(1, 4);

            base.OnPaint(e);

            if (CheckState == CheckState.Checked)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.White), CheckRegion);
                e.Graphics.FillPolygon(CheckColorBrush, Points);
            }
            else if (CheckState == CheckState.Indeterminate)
            {
                e.Graphics.FillRectangle(new SolidBrush(Color.White), CheckRegion);
                e.Graphics.FillPolygon(IndeterminateColorBrush, Points);
            }
        }

        /// <summary>
        /// Gets or Sets the check state. 0 is Unchecked, 1 is Checked, and 2 is Indeterminate
        /// </summary>
        public int DBValue
        {
            get
            {
                switch (CheckState)
                {
                    case CheckState.Unchecked:
                        return 0;
                    case CheckState.Checked:
                        return 1;
                    case CheckState.Indeterminate:
                        return 2;
                    default:
                        return 3;
                }
            }
            set
            {
                switch (value)
                {
                    case 0:
                        CheckState = CheckState.Unchecked;
                        break;
                    case 1:
                        CheckState = CheckState.Checked;
                        break;
                    case 2:
                        CheckState = CheckState.Indeterminate;
                        break;
                    default:
                        break;
                }


            }

        }



    }
}
使用System.Windows.Forms;
使用系统图;
命名空间ColorCheckBoxCS
{
公共类颜色复选框:复选框
{
/// 
///选中的X的颜色
/// 
公共颜色CheckColor=Color.Blue;
/// 
///不确定的X的颜色
/// 
公共颜色不确定颜色=颜色。红色;
受保护的覆盖无效OnPaint(PaintEventArgs e)
{
SolidBrush CheckColorBrush=新的SolidBrush(CheckColor);
SolidBrush IndeterminateColorBrush=新的SolidBrush(IndeterminateColor);
矩形检查区域=新矩形(1,2,11,11);
点[]点=新点[16];
点[0]=新点(1,2);
点[1]=新点(4,2);
点[2]=新点(6,5);
点[3]=新点(9,2);
点[4]=新点(12,2);
点[5]=新点(12,4);
点[6]=新点(9,7);
点[7]=新点(12,10);
点[8]=新点(12,13);
点[9]=新点(9,12);
点[10]=新点(6,9);
点[11]=新点(3,13);
点[12]=新点(1,12);
点[13]=新点(1,10);
点[14]=新点(4,7);
点[15]=新点(1,4);
基础漆(e);
if(CheckState==CheckState.Checked)
{
e、 图形.FillRectangle(新的SolidBrush(颜色.白色),CheckRegion);
e、 图形。填充多边形(选中彩色笔刷,点);
}
else if(CheckState==CheckState.undeterminate)
{
e、 图形.FillRectangle(新的SolidBrush(颜色.白色),CheckRegion);
e、 图形.填充多边形(不确定色刷,点);
}
}
/// 
///获取或设置检查状态。0为未选中状态,1为已选中状态,2为不确定状态
/// 
公共整数DBValue
{
得到
{
开关(检查状态)
{
案例检查状态。未检查:
返回0;
案例检查状态。已检查:
返回1;
案例检查状态。不确定:
返回2;
违约:
返回3;
}
}
设置
{
开关(值)
{
案例0:
CheckState=CheckState.Unchecked;
打破
案例1:
CheckState=CheckState.Checked;
打破
案例2:
CheckState=CheckState。不确定;
打破
违约:
打破
}
}
}
}
}

好的,代码有两个问题:

Point[] Points = new Point[15];
需要

Point[] Points = new Point[16];
不知道为什么,但我15岁就出界了

第二,我必须删除:

ColorCheckBox()
{
    ThreeState = true;
}

同样,我也不知道为什么。这就是成功的原因。一旦我完成了整个设置,我会更新顶部的代码。

你知道你可以在C项目的DLL中使用VB代码,对吗?我要添加到的DLL是在C中的。如果我能帮上忙的话,我正在努力使我的DLL计数降到1。此外,我也有一天学习C++的想法,这将帮助我更好地理解C>-C++如何工作。我也想学习WPF,但这是下一次的评论。修订后的代码不会处理它创建的任何东西。如果使用
new
且对象具有Dispose方法,请使用
using
block@Plutonix我将如何重写此文件以完成处置?Using不适用于笔刷。从SolidBrush创建的笔刷绝对实现Dispose,因此它可以使用
Using
块。请参阅:您将希望将创建它们的位置移动到实际使用它们的位置。无需为每个绘制调用创建2个画笔,然后只使用1个画笔,不处理任何画笔。关于点数组,需要“16”的原因是,在C中指定长度,而VB中指定上限(最后一个索引)。这两个笔刷成员应该是简单的
颜色
,绘制代码使用它们创建笔刷。按原样,控件未被处理