Duane

Duane

  • NA
  • 77
  • 0

BackColor Property

Feb 20 2013 11:14 AM
I have created a User Control similar to a Button, but inheriting directly from Control. I have had to establish many of my own properties and and override some of the existing ones. However, there appears to be a problem with the BackColor property. I cannot get this to change when I put the User Control on a form under any circumstances. I have tried overriding the existing BackColor property and creating one of my own but even that doesn't work. I am at a loss. The property shows up in designer, but nothing affects it. I am sure I am missing something obvious, but what? Code for the control is shown below, in case it's any help:

Imports System.Windows.Forms
Imports System.Drawing
Imports System.ComponentModel
Public Class DoubleTextButton
    Inherits Control
    Private text1 As String = "1"
    Private text2 As String = "2"
    Private font1 As Font = New Font("MicrosoftSansSerif", 8)
    Private font2 As Font = New Font("MicrosoftSansSerif", 8)
    Private foreclr1 As Color = Color.Black
    Private foreclr2 As Color = Color.Black
    Private align1 As ContentAlignment = ContentAlignment.MiddleCenter
    Private align2 As ContentAlignment = ContentAlignment.MiddleCenter
    Private locpt1 As New Point(0, 0)
    Private locpt2 As New Point(ClientRectangle.Width / 2, 0)
    Private asize As New Size(ClientRectangle.Width / 2, ClientRectangle.Height)
    Private rect1 As New Rectangle(locpt1, asize)
    Private rect2 As New Rectangle(locpt2, asize)
    'Private bkcolor As Color = Color.White
    Private flatapp As ButtonState = ButtonState.Normal

    <System.ComponentModel.Browsable(False)> _
    <System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)> _
    Public Overrides Property Text As String
        Get
            Return MyBase.Text
        End Get
        Set(value As String)
            MyBase.Text = value
        End Set
    End Property
    <System.ComponentModel.Browsable(False)> _
   <System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)> _
    Public Overrides Property Font As Font
        Get
            Return MyBase.Font
        End Get
        Set(value As Font)
            MyBase.Font = value
        End Set
    End Property
    <System.ComponentModel.Browsable(False)> _
    <System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)> _
    Public Overrides Property ForeColor As Color
        Get
            Return MyBase.ForeColor
        End Get
        Set(value As Color)
            MyBase.ForeColor = value
        End Set
    End Property
    <Category("Text Data"), _
    Description("Describes the font of the left side text.")> _
        Public Property FontOne() As Font
        Get
            Return font1
        End Get
        Set(value As Font)
            font1 = value
            Me.Invalidate()
        End Set
    End Property
    <Category("Text Data"), _
    Description("Describes the font of the right side text.")> _
    Public Property FontTwo() As Font
        Get
            Return font2
        End Get
        Set(value As Font)
            font2 = value
            Me.Invalidate()
        End Set
    End Property
    <Category("Text Data"), _
    Description("The content of the left side text.")> _
    Public Property TextOne() As String
        Get
            Return text1
        End Get
        Set(value As String)
            text1 = value
            Me.Invalidate()
        End Set
    End Property
    <Category("Text Data"), _
    Description("The content of the right side text.")> _
    Public Property TextTwo() As String
        Get
            Return text2
        End Get
        Set(value As String)
            text2 = value
            Me.Invalidate()
        End Set
    End Property
    <Category("Text Data"), _
    Description("Describes the color of the left side text.")> _
    Public Property ForeColorOne() As Color
        Get
            Return foreclr1
        End Get
        Set(value As Color)
            foreclr1 = value
            Me.Invalidate()
        End Set
    End Property
    <Category("Text Data"), _
    Description("Describes the color of the right side text.")> _
    Public Property ForeColorTwo() As Color
        Get
            Return foreclr2
        End Get
        Set(value As Color)
            foreclr2 = value
            Me.Invalidate()
        End Set
    End Property
    <Category("Text Data"), _
    Description("The alignment of the text in relation to the left side of the button.")> _
    Public Property TextAlignOne() As ContentAlignment
        Get
            Return align1
        End Get
        Set(value As ContentAlignment)
            align1 = value
            Me.Invalidate()
        End Set
    End Property
    <Category("Text Data"), _
    Description("The alignment of the text in relation to the right side of the button.")> _
    Public Property TextAlignTwo() As ContentAlignment
        Get
            Return align2
        End Get
        Set(value As ContentAlignment)
            align2 = value
            Me.Invalidate()
        End Set
    End Property
    <Category("Appearance"), _
    Description("Represents the physical appearance of the button.")>
    Public Property FlatAppearance() As ButtonState
        Get
            Return flatapp
        End Get
        Set(value As ButtonState)
            flatapp = value
            Me.Invalidate()
        End Set
    End Property

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        Me.OnPaint(e)
        Dim g As Graphics = Me.CreateGraphics

        Dim locpt1 As New Point(0, 0)
        Dim locpt2 As New Point(ClientRectangle.Width / 2, 0)
        Dim asize As New Size(ClientRectangle.Width / 2, ClientRectangle.Height)
        Dim rect1 As New Rectangle(locpt1, asize)
        Dim rect2 As New Rectangle(locpt2, asize)
        
        Dim format1 As StringFormat = GetStringFormatFromContentAlignment(Me.align1)
        Dim format2 As StringFormat = GetStringFormatFromContentAlignment(Me.align2)

        Dim brush1 As Brush
        Dim brush2 As Brush
        brush1 = New SolidBrush(foreclr1)
        brush2 = New SolidBrush(foreclr2)

        ControlPaint.DrawButton(e.Graphics, Me.ClientRectangle, flatapp)
        e.Graphics.DrawString(Me.text1, font1, brush1, rect1, format1)
        e.Graphics.DrawString(Me.text2, font2, brush2, rect2, format2)

    End Sub

    Private Function GetStringFormatFromContentAlignment(ca As ContentAlignment) As StringFormat
        Dim format As New StringFormat()
        Select Case (ca)
            Case ContentAlignment.TopCenter
                format.Alignment = StringAlignment.Center
                format.LineAlignment = StringAlignment.Near
            Case ContentAlignment.TopLeft
                format.Alignment = StringAlignment.Near
                format.LineAlignment = StringAlignment.Near
            Case ContentAlignment.TopRight
                format.Alignment = StringAlignment.Far
                format.LineAlignment = StringAlignment.Near
            Case ContentAlignment.MiddleCenter
                format.Alignment = StringAlignment.Center
                format.LineAlignment = StringAlignment.Center
            Case ContentAlignment.MiddleLeft
                format.Alignment = StringAlignment.Near
                format.LineAlignment = StringAlignment.Center
            Case ContentAlignment.MiddleRight
                format.Alignment = StringAlignment.Far
                format.LineAlignment = StringAlignment.Center
            Case ContentAlignment.BottomCenter
                format.Alignment = StringAlignment.Center
                format.LineAlignment = StringAlignment.Far
            Case ContentAlignment.BottomLeft
                format.Alignment = StringAlignment.Near
                format.LineAlignment = StringAlignment.Far
            Case ContentAlignment.BottomRight
                format.Alignment = StringAlignment.Far
                format.LineAlignment = StringAlignment.Far
        End Select
        Return format
    End Function

End Class

Answers (7)