我编写了一个接收自己的类HintProperties
的方法,其中包含一些属性及其值,HintProperties
类的属性名称由查找方法获取该属性存在于控件中,以将控件属性值设置为HintProperties
中设置的相同。
这是自己的类:
''' <summary>
''' Determines a text-hint and it's personalization properties.
''' </summary>
Public Class HintProperties
''' <summary>
''' Gets or sets the text-hint font.
''' </summary>
''' <value>The font.</value>
Public Property Font As Font = Nothing
''' <summary>
''' Gets or sets the text-hint.
''' </summary>
Public Property Text As String = String.Empty
''' <summary>
''' Gets or sets the text-hint color.
''' </summary>
Public Property Forecolor As Color = Color.Empty
''' <summary>
''' Gets or sets the text-hint alignment.
''' Only avaliable for specific controls such as Textbox's.
''' </summary>
Public Property TextAlign As HorizontalAlignment = 0
End Class
这是方法:
''' <summary>
''' Sets the properties of an specific control.
''' </summary>
''' <param name="Control">Indicates the Control.</param>
''' <param name="HintProperties">Indicates the properties to set it's values.</param>
Private Shared Sub SetProperties(ByVal [Control] As Object,
ByVal HintProperties As HintProperties)
' Set the hint-text.
[Control].Text = HintProperties.Text
Dim t As Type = [Control].GetType
For Each Prop As Reflection.PropertyInfo In HintProperties.GetType.GetProperties
Dim tprop As Reflection.PropertyInfo = t.GetProperty(Prop.Name)
If Not tprop Is Nothing Then
Try
If Not Prop.Name = "TextAlign" Then
tprop.SetValue([Control], Prop.GetValue(HintProperties, Nothing), Nothing)
Else
' Here throws an unhandled System.ComponentModel.Win32Exception exception:
tprop.SetValue([Control], Prop.GetValue(HintProperties, Nothing), Nothing)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
Next
End Sub
问题开始时,在控件的HandleCreated
事件处理程序中,我调用SetProperties
方法传递HintProperties
的实例,并将TextAlign
属性设置为{{ 1}}或HorizontalAlignment.Center
当我尝试在文本框中设置HorizontalAlignment.Right
属性时,win32
方法中出现未处理的SetProperties
异常,我可以找到该属性,我无法获得价值,但我无法设置它。
换句话说,我无法在TextAlign
事件处理程序
TextAlign
属性
示例:
handlecreated
如果我处理其他事件,例如 addhandler textbox1.handlecreated, addresof Control_HandleCreated
Private Shared Sub Control_HandleCreated(ByVal sender As Object, ByVal e As EventArgs)
Dim [Control] As Control = DirectCast(sender, Control)
Dim [HintProperties] As HintProperties = ControlHints([Control]) ' A dictionary (of contorl, hintproperties)
SetProperties([Control], [HintProperties])
End Select
End Sub
或Enter
,则没有问题,但我需要在创建控件时设置Leave
属性。
我做错了什么以及如何解决这个问题?