[英]Converted from vb.net to C# custom control wont show up in toolbox
我已經使用了一段時間的一些自定義控件。 幾天前,我做了一個自定義復選框。 由於我不太了解C#語法,因此我在VB.NET中做到了。 問題是Internet上大多數免費的自定義控件都在C#中。 我的解決方案是將它們編譯成DLL,並在我的應用程序中使用它。
我繼續將其轉換為C#,然后嘗試將其添加到DLL中。 它根本不會顯示在工具箱中。 我認為我在VB.NET不需要的代碼中丟失了一些內容,但我不知道它是什么。
這是制作適當的DLL的VB.NET版本,它為我提供了自定義工具。
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;
}
}
}
}
}
好的,代碼有兩個問題:
Point[] Points = new Point[15];
需要是
Point[] Points = new Point[16];
不知道為什么,但是我超出了15。
其次,我必須刪除:
ColorCheckBox()
{
ThreeState = true;
}
同樣,不知道為什么。 這就是有效的方法。 完成所有設置后,我將在頂部更新代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.