• 0

[VB.NET] UserControl Overridden Property


Question

I made a UserControl that acts like a button, except it is skinned. I had to make a property "Text" since UserControls don't have this property by default, but apparently they have it in their code: "Property 'Text' shadows an overridable method in the base class 'UserControl'. To override the base method, this method must be declared 'Overrides'."

I declared it Overrides: "Public Overrides Property Text() As String", and made it browsable: "<Browsable(True)>". The property appears in the PropertyGrid when I am designing the parent form, and I can set it and the changes show up on the control just fine. Everything works perfectly until I build the project. At this point the property is reset back to default and I have to set it again. It just won't retain the value. I looked in the .Designer.vb, and it isn't actually generating the code to set the property. What is going on here? I didn't used to have this problem until I upgraded from the individual Express editions to the Professional edition of VS2008.

6 answers to this question

Recommended Posts

  • 0

You need to add another attribute, DesignerSerializationVisibilityAttribute to the property, with the value of Visible:

&lt;DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)&gt; _
Public Property Text As String
   '...
End Property

This tells the designer serialiser to output the value of the property in the InitializeComponent() method.

  • 0

yeah heres the code file:

Imports System.ComponentModel

&lt;System.ComponentModel.DefaultEvent("Click")&gt; Public Class SkinButton

    Public topColNormal As Color = Color.FromArgb(255, 87, 175, 80)
    Public botColNormal As Color = Color.FromArgb(255, 50, 99, 45)
    Public topColHover As Color
    Public botColHover As Color
    Public topColPress As Color = botColNormal
    Public botColPress As Color = topColNormal
    Public State As Integer = 0 '0 = normal, 1 = hover, 2 = press'
    Private myText As String = Name

    Public Sub New()
        ' This call is required by the Windows Form Designer.'
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call.'
        SetStyle(ControlStyles.SupportsTransparentBackColor, True)

        Dim hr, hg, hb As Integer
        hr = topColNormal.R + 64 : hg = topColNormal.G + 64 : hb = topColNormal.B + 64
        If hr &gt; 255 Then hr = 255 : If hg &gt; 255 Then hg = 255 : If hb &gt; 255 Then hb = 255
        topColHover = Color.FromArgb(255, hr, hg, hb)

        hr = botColNormal.R + 64 : hg = botColNormal.G + 64 : hb = botColNormal.B + 64
        If hr &gt; 255 Then hr = 255 : If hg &gt; 255 Then hg = 255 : If hb &gt; 255 Then hb = 255
        botColHover = Color.FromArgb(255, hr, hg, hb)
    End Sub

    &lt;System.ComponentModel.Category("Appearance")&gt; &lt;Browsable(True)&gt; &lt;DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)&gt; Public Property ButtonText() As String
        Get
            Return myText
        End Get
        Set(ByVal value As String)
            myText = value
            Refresh()
        End Set
    End Property

    &lt;System.ComponentModel.Category("Appearance")&gt; Public Property TopColor() As Color
        Get
            Return topColNormal
        End Get
        Set(ByVal value As Color)
            topColNormal = value
            topColPress = botColNormal
            botColPress = topColNormal

            Dim hr, hg, hb As Integer
            hr = topColNormal.R + 64 : hg = topColNormal.G + 64 : hb = topColNormal.B + 64
            If hr &gt; 255 Then hr = 255
            If hg &gt; 255 Then hg = 255
            If hb &gt; 255 Then hb = 255
            topColHover = Color.FromArgb(255, hr, hg, hb)

            Refresh()
        End Set
    End Property

    &lt;System.ComponentModel.Category("Appearance")&gt; Public Property BottomColor() As Color
        Get
            Return botColNormal
        End Get
        Set(ByVal value As Color)
            botColNormal = value
            botColPress = topColNormal
            topColPress = botColNormal

            Dim hr, hg, hb As Integer
            hr = botColNormal.R + 64 : hg = botColNormal.G + 64 : hb = botColNormal.B + 64
            If hr &gt; 255 Then hr = 255
            If hg &gt; 255 Then hg = 255
            If hb &gt; 255 Then hb = 255
            botColHover = Color.FromArgb(255, hr, hg, hb)
            Refresh()
        End Set
    End Property

    &lt;System.ComponentModel.Category("Appearance")&gt; Public ReadOnly Property TopColorHover() As Color
        Get
            Return topColHover
        End Get
    End Property

    &lt;System.ComponentModel.Category("Appearance")&gt; Public ReadOnly Property TopColorPress() As Color
        Get
            Return topColPress
        End Get
    End Property

    &lt;System.ComponentModel.Category("Appearance")&gt; Public ReadOnly Property BottomColorHover() As Color
        Get
            Return botColHover
        End Get
    End Property

    &lt;System.ComponentModel.Category("Appearance")&gt; Public ReadOnly Property BottomColorPress() As Color
        Get
            Return botColPress
        End Get
    End Property

    Protected Overrides Sub OnMouseEnter(ByVal e As System.EventArgs)
        MyBase.OnMouseEnter(e)
        State = 1
        Refresh()
    End Sub

    Protected Overrides Sub OnMouseLeave(ByVal e As System.EventArgs)
        MyBase.OnMouseEnter(e)
        State = 0
        Refresh()
    End Sub

    Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseUp(e)
        State = 1
        Refresh()
    End Sub

    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseUp(e)
        State = 2
        Refresh()
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim topCol, botCol As Color


        Select Case State
            Case 0
                topCol = topColNormal : botCol = botColNormal
            Case 1
                topCol = topColHover : botCol = botColHover
            Case Else
                topCol = topColPress : botCol = botColPress
        End Select


        Dim gb As New Drawing.Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, Height), topCol, botCol)
        Dim sgb As New Drawing.Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, Height / 2), Color.FromArgb(128, 255, 255, 255), Color.FromArgb(64, 255, 255, 255))
        Dim g As Graphics = e.Graphics
        g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
        FillRoundedRectangle(g, New Rectangle(0, 0, Me.Width - 1, Me.Height - 1), 8, gb)
        FillRoundedRectangle(g, New Rectangle(0, 0, Me.Width - 1, (Me.Height / 2) - 1), 8, sgb)
        DrawRoundedRectangle(g, New Rectangle(0, 0, Me.Width - 1, Me.Height - 1), 8, New Pen(Color.FromArgb(128, 0, 0, 0)))
        DrawRoundedRectangle(g, New Rectangle(1, 1, Me.Width - 3, Me.Height - 3), 8, New Pen(Color.FromArgb(128, 255, 255, 255)))

        Dim sf As New StringFormat()
        sf.Alignment = StringAlignment.Center
        sf.LineAlignment = StringAlignment.Center
        g.DrawString(myText, Font, New SolidBrush(ForeColor), New Rectangle(0, 0, Width, Height), sf)
    End Sub

    Public Shared Sub DrawRoundedRectangle(ByVal g As Graphics, ByVal r As Rectangle, ByVal d As Integer, ByVal p As Pen)

        Dim gp As New System.Drawing.Drawing2D.GraphicsPath()

        gp.AddArc(r.X, r.Y, d, d, 180, 90)
        gp.AddArc(r.X + r.Width - d, r.Y, d, d, 270, 90)
        gp.AddArc(r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90)
        gp.AddArc(r.X, r.Y + r.Height - d, d, d, 90, 90)
        gp.AddLine(r.X, r.Y + r.Height - d, r.X, CInt(r.Y + d / 2))

        g.DrawPath(p, gp)
    End Sub

    Public Shared Sub FillRoundedRectangle(ByVal g As Graphics, ByVal r As Rectangle, ByVal d As Integer, ByVal b As Brush)

        Dim gp As New System.Drawing.Drawing2D.GraphicsPath()

        gp.AddArc(r.X, r.Y, d, d, 180, 90)
        gp.AddArc(r.X + r.Width - d, r.Y, d, d, 270, 90)
        gp.AddArc(r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90)
        gp.AddArc(r.X, r.Y + r.Height - d, d, d, 90, 90)
        gp.AddLine(r.X, r.Y + r.Height - d, r.X, CInt(r.Y + d / 2))

        g.FillPath(b, gp)
    End Sub
End Class

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.