7
Answers

BackColor Property

Photo of Duane

Duane

12y
1.2k
1
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)

0
Photo of Duane
NA 77 0 12y
I did it! Amazingly simple actually. I overrode the BackColor property and created a new property called FullBackColor. Then in the OnPaint event, I created a brush based on the FullBackColor property and also a FillRectangle using that brush and the ClientRectangle of the button. Works like a champ, however...

Now the Border does not show and is something I can't seem to fix. I have set up new properties for border and the border color, but the border does not show. Grips show but nothing in between them. I am going to solve this somehow.

Also, I had to remove the ControlPaint.ButtonDraw method. That is what was messing with the BackColor I think. That may also be causing my current problem though. (it figures, if the left one don't get you...) 

Any advice is still appreciated and thank you all for your efforts so far.
0
Photo of Duane
NA 77 0 12y
I'll give it another shot (Button instead of Control). However the last time I inherited from something so specific, I had serious problems getting the control to work. On the other hand that was some time back, programming-wise and I may be more capable now. At least this time I understand what you're talking about. LOL

I'll let everyone know how it comes out.
0
Photo of Vulpes
NA 98.3k 1.5m 12y
I've looked up those attributes and they are internal to the framework dlls (Friend access in VB) and not therefore accessible to your program.

Worse still, I've also discovered that the UseVisualStyleBackColor property is defined in the ButtonBase class (rather than inherited from Control) and uses private members of that class in its implementation so there's no way you're going to be able to reproduce all this in your user control.

Is there some reason why you can't inherit from Button rather than directly from Control?

It's starting to look as though you're not going to be able to get this working properly otherwise.

0
Photo of Duane
NA 77 0 12y
When I inserted the code snippet from Vulpes, I encountered an error telling me that "SRDescription" and "SRCategory" are not accessible in this context because they are Friend. I am not sure why this is happening. All I did was put this property into my UserControl's class. I left my inheritance at Control. ???
0
Photo of Vulpes
NA 98.3k 1.5m 12y
According to Redgate Reflector, this is how ButtonBase overrides the BackColor propery it inherits from Control:

<SRDescription("ControlBackColorDescr"), SRCategory("CatAppearance")> _
Public Overrides Property BackColor As Color
    Get
        Return MyBase.BackColor
    End Get
    Set(ByVal value As Color)
        If MyBase.DesignMode Then
            If (value <> Color.Empty) Then
                Dim descriptor As PropertyDescriptor = TypeDescriptor.GetProperties(Me).Item("UseVisualStyleBackColor")
                If (Not descriptor Is Nothing) Then
                    descriptor.SetValue(Me, False)
                End If
            End If
        Else
            Me.UseVisualStyleBackColor = False
        End If
        MyBase.BackColor = value
    End Set
End Property

I'd try including this code in your user control but, instead of inheriting from ButtonBase, inherit from Control as you were originally doing.

0
Photo of Duane
NA 77 0 12y
I have tried ButtonBase and it seemed to create other problems(such as interfering with the FlatAppearance property) and still did not seem to fix the BackColor problem. This isn't making a whole lot of sense frankly. I understand inheritance fairly well now and what I've done should have corrected the problem.
0
Photo of Vulpes
NA 98.3k 1.5m 12y
I'd see if it makes any difference if you inherit from ButtonBase rather than directly from Control.

This class simply inherits most of Control's properties (including ForeColor and Text) but overrides some (such as BackColor and Text).