【问题标题】:Converted from vb.net to C# custom control wont show up in toolbox从 vb.net 转换为 C# 自定义控件不会显示在工具箱中
【发布时间】:2016-03-11 00:25:17
【问题描述】:

我有一些自定义控件已经使用了一段时间。几天前,我做了一个自定义复选框。由于我不太了解 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;
                }


            }

        }



    }
}

【问题讨论】:

  • 你知道你可以在 C# 项目的 DLL 中使用 VB 代码,对吧?
  • 我添加到的有问题的 DLL 在 C# 中。如果可以的话,我正在尝试将我的 DLL 计数减至 1。此外,我想到有一天学习 C++,这将帮助我更好地理解 C#->C++ 的工作原理。我也想学WPF,不过那是下次评论了。
  • 修改后的代码不会处理它创建的任何东西。如果您使用 new 并且对象具有 Dispose 方法,请使用 using
  • @Plutonix 我将如何重写它以完成处置?使用不适用于画笔。
  • 从 SolidBrush 创建的画笔绝对实现了 Dispose,因此它可以使用 using 块。请参阅:stackoverflow.com/a/35920074/1070452 您需要将创建它们的位置移动到实际使用它们的位置。无需为每个绘画调用创建 2 个画笔,然后只使用 1 个并且不处理任何画笔。 Another example

标签: c# vb.net dll visual-studio-2015


【解决方案1】:

好的,代码有两个问题:

Point[] Points = new Point[15];

需要

Point[] Points = new Point[16];

不知道为什么,但我的 15 分越界了。

其次,我必须删除:

ColorCheckBox()
{
    ThreeState = true;
}

再次,不知道为什么。这就是有效的。完成整个设置后,我将更新顶部的代码。

【讨论】:

  • 关于点的数组,你需要'16'的原因是在C#中你指定长度,不像VB你指定上限(最后一个索引)。
  • 这 2 个画笔成员应该很简单 Colors 并且绘制代码从它们创建画笔。照原样,如果用户想要动态添加/删除它们,控件不会处理它们。
  • @Plutonix 更新了上面的最后一个代码块。我去头并做了刷子/颜色交换。我想这就是你所说的。
  • @DaveDoknjas 从设计的角度来看,这实际上更有意义。实现api cpp编程还有很长的路要走。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-11
  • 2012-02-02
  • 1970-01-01
相关资源
最近更新 更多