用户控件,从类对象中选择自定义属性
问题描述:
这让我很疯狂,但我正在研究这个约一个星期,我无法实现。
我有一个类,这个类有很多布尔值。
Ex:public bool1,
public bool2等
现在我想创建一个带有自定义属性的自定义单选按钮。
在这个自定义属性区域中,我将选择其中一个我的类的布尔值来自下拉列表。之后如果布尔值为true,将检查单选按钮。
我在WindowsForm上工作。
我该怎么做?
我尝试了什么:
我需要的屏幕截图:示例 - Postimage.org [ ^ ]
答
当然,您必须在自定义Control中实现自己的Checked / Unchecked布尔属性,在此案例一个RadioButton。但Checked属性必须在自定义UserControl中实现,而不是在不同的类中实现。
然后您可以选择在MyRadiButton控件中使用从UserControl派生的Radibutton,或者您可以使用System.Drawing方法执行此操作绘制一个圆圈并绘制指示文本属性的文本。这对新手来说太难了,但你可以在网上找到一个例子。
这里有一些代码:
Of course you have to implement its own Checked/Unchecked Boolean Property in a custom Control, in this case a RadioButton. But the Checked Property must be implemented inside your custom UserControl not on different class.
You then can choose to use a Radibutton within your MyRadiButton control that derives from UserControl or you would do it using System.Drawing methods to paint a circle and draw text indicating the Text Property. This all is too difficult for a novice but you may find an example on Web.
Here is some code to start with:
Public Class MyRadioButton : Inherits UserControl
Private _Checked As Boolean
Property Checked As Boolean
Get : Return _Checked : End Get
Set(value As Boolean)
_Checked = value
EnableDisable(New PaintEventArgs(Me.CreateGraphics))
End Set
End Property
Sub New()
'//
End Sub
Protected Sub EnableDisable(e As PaintEventArgs)
If Checked Then
'e.graphics.fillellipse(draw/fill a circle)
Else
'e.graphics.drawellipse(draw circle)
End If
'e.graphics.drawstring(draw string of Me.Text Property)
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
EnableDisable(e)
End Sub
End Class