无法通过反射设置TextAlign属性的值

时间:2014-04-22 01:58:04

标签: .net vb.net reflection properties controls

我编写了一个接收自己的类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属性。

我做错了什么以及如何解决这个问题?

0 个答案:

没有答案