我继承了一个列表框控件来写一些像这样的属性:
''' <summary>
''' Indicates the BackColor to paint the unselected ListBox items when the ListBox is enabled.
''' </summary>
Public Property ItemUnselected_BackColor As Color
Get
Return _ItemUnselected_BackColor.Color
End Get
Set(value As Color)
_ItemUnselected_BackColor = New SolidBrush(value)
End Set
End Property
问题在于,当我在设计中更改颜色时,我需要移动控件或最小化/最大化VisualStudio IDE以更新控件上的更改以查看新颜色。
执行此操作的方法是什么?
更新:
我认为这样做的方法是添加一个特殊标志或其他东西......
我试过这个,但是在编辑中该属性变得无法接受:
<EditorAttribute(GetType(Color), GetType(UITypeEditor))> _
Public Property ItemUnselected_BackColor As Color
...
答案 0 :(得分:2)
当属性发生变化时,如何使控件无效?
Public Property ItemUnselected_BackColor As Color
Get
Return _ItemUnselected_BackColor.Color
End Get
Set(value As Color)
_ItemUnselected_BackColor = New SolidBrush(value)
Me.Invalidate(True)
End Set
End Property
修改强>
因为强烈建议只在需要时创建画笔,所以应该这样做:
Public Property ItemUnselected_BackColor As Color
Get
Return Me._ItemUnselected_BackColor
End Get
Set(value As Color)
If ((value = Color.Transparent) OrElse (value = Color.Empty)) Then
Throw New ArgumentOutOfRangeException()
End If
If (value <> _ItemUnselected_BackColor) Then
Me._ItemUnselected_BackColor = value
Me.Invalidate()
End If
End Set
End Property
Private _ItemUnselected_BackColor As Color = Color.Black
在你的绘画方法中:
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Using b As New SolidBrush(Me._ItemUnselected_BackColor)
'TODO: Paint
End Using
MyBase.OnPaint(e)
End Sub
答案 1 :(得分:1)
部分问题是该属性被定义为颜色,但您正在尝试使用/保存画笔。正确的方法是
Public Property ItemUnselected_BackColor As Color
Get
Return _ItemUnselected_BackColor
End Get
Set(value As Color)
_ItemUnselected_BackColor = value
Me.Invalidate
End Set
End Property
并且Invalidate
是更新控件的正确方法,因为在给定新颜色设置的情况下,当前显示无效。刷子应该在paint
或OnDrawItem
程序中创建,而不是在属性get / setter中创建。您的代码不会丢弃前一个代码。
不需要这些属性,也不要做你认为他们做的事情。他们会定义一种特殊方式(如对话框)来获取颜色。
我开始研究你的ListView现在有一个AlternativeBack颜色。如果您想进行优化,可以执行以下操作:
Set(ByVal value As Color)
If _AltBackColor <> value Then
_AltBackColor = value
Me.Invalidate() ' only invalidate/repaint if it changed
End If
End Set