我有一些自定义控件,我已经使用了一段时间。几天前我做了一个自定义复选框。由于我不太了解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;
}
}
}
}
}
答案 0 :(得分:0)
好的,代码有两个问题:
Point[] Points = new Point[15];
需要
Point[] Points = new Point[16];
不知道为什么,但我在15岁时已经超出了界限。
其次,我不得不删除:
ColorCheckBox()
{
ThreeState = true;
}
再次,不知道为什么。这才是有效的。我完成整套工作后,我会在顶部更新代码。