简体   繁体   中英

Converted from vb.net to C# custom control wont show up in toolbox

I have some custom controls that I've been using for a while. A few days ago I made a custom checkbox. Since I don't really know C# syntax, I made it in VB.NET. The problem is that most of the free custom controls on the internet are in C#. My solution was to compile them into a DLL and use that in my applications.

I went ahead and converted this one into C# and tried to add it to my DLL. It won't show up in the toolbox at all. I think I'm missing something in the code that VB.NET doesn't require, but I can't figure out what it is.

Here is the VB.NET version that makes a proper DLL that gives me the custom tool.

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

Here is the converted code that isn't working. It compiles fine, but won't give me a custom control like the above.

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;
                }
            }
        }
    }
}

On a side note, if anyone wants to use this code, go ahead. It makes the checkbox use three states and paints an X. I'm using it for a two level authentication system. You can even set the color of the X in code or change it in the control. Works great in VB.NET and if it gets fixed, it'll work great in C#. I plan to make it take a value and set the CheckState as well, but I hit this issue first. I'll repost the correct code with set should it get fixed.

Took me forever to get the point placement correct, so use it if you want to.

To any one who knows what the issue is, please help. I am not a good sales man.

Revised Working Code;

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;
                }


            }

        }



    }
}

Ok, there were two issues with the code:

Point[] Points = new Point[15];

Needed to be

Point[] Points = new Point[16];

No idea why, but I was getting an out of bounds with 15.

Second, I had to remove:

ColorCheckBox()
{
    ThreeState = true;
}

Again, no idea why. That's just what worked. I'll update the code at the top once I get the whole set thing done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM