vb .NET custom control inheriting from TextBox doesn't fire Paint event

前端 未结 2 690
醉酒成梦
醉酒成梦 2021-01-21 00:47

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 re

2条回答
  •  无人及你
    2021-01-21 01:16

    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 :)

提交回复
热议问题