views:

11

answers:

1

I need a multiline TextBox which is always disabled, but it shouldn't paint itself in gray, but I want to keep its designer choosen color.

I previously had the same requirement with an always-black Label (no multiline) and so I inherited from Label like:

Imports System.ComponentModel

   Public Class LabelDisabled
        Inherits Label

        Sub New()
            InitializeComponent()
            Enabled = False
        End Sub

        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            ' always draw it black
            e.Graphics.DrawString(Me.Text, Me.Font, Brushes.Black, 0, 0)
        End Sub

    End Class

That works fine. Now I want the same thing but with a multiline label, so I chose to inherit from TextBox:

Imports System.ComponentModel

Public Class CustomControl1
    Inherits TextBox

    Sub New()

        InitializeComponent()
        'Paint never fires anyway
        'Enabled = False
    End Sub


    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim brush As New SolidBrush(Me.ForeColor)
        e.Graphics.DrawString(Me.Text, Me.Font, brush, 0, 0)
    End Sub

End Class

Now the Paint event is never fired in the CustomControl1 - TextBox inherited - control.

Why can't I get the Paint event?

Also, if I want to make the Enabled property invisible and not-settable by the user, I do:

<Browsable(False),
DefaultValue(False)>
Public Overloads Property Enabled As Boolean
    Get
        Return False
    End Get
    Set(ByVal value As Boolean)
    End Set
End Property

But this way, neither I can set the "real" Enabled property, I mean the backing field.

+1  A: 

I've found a solution. It looks like a TextBox disables the Paint event even for subclasses. But you can force the WM_PAINT bit calling SetStyle:

Public Class DisabledTextBox
    Inherits TextBox

    Public Sub New()
        InitializeComponent()

        Enabled = False
        SetStyle(ControlStyles.Selectable, False)
        SetStyle(ControlStyles.UserPaint, True)

    End Sub

    Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim brush As New SolidBrush(Me.ForeColor)
        e.Graphics.DrawString(Me.Text, Me.Font, brush, 0, 0)
    End Sub

End Class

It works perfectly as expected :)

vulkanino
+1 for posting the solution to your own problem.
Heinzi
I still have some minor issues: my textbox is now disabled, non-selectable and pretty colored, but the wordwrapping is just bad. DrawString just truncates a word if it can't fit in the current line, and then continues to the next line (but the word's been truncated anyway). I'd like to use the wordwrapping feature of a TextBox with the colouring feature of my DisabledTextBox.Ideas?
vulkanino
Looks like I am answering myself today :)Replasce the DrawString with:e.Graphics.DrawString(Me.Text, Me.Font, brush, Me.ClientRectangle)Having a rectangle instead of a Point, gives a good wordwrapping.
vulkanino