从TextBox继承的vb .NET自定义控件不会触发Paint事件 [英] vb .NET custom control inheriting from TextBox doesn't fire Paint event

查看:172
本文介绍了从TextBox继承的vb .NET自定义控件不会触发Paint事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个多行文本框,该框始终处于禁用状态,但不应将其自身涂成灰色,但我想保留其设计者选择的颜色。

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.

我以前对始终为黑色的Label(没有多行)有相同的要求,所以我从Label继承来了:

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

这很好。现在我想要相同的东西,但是带有多行标签,所以我选择从TextBox继承:

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

现在,永远不会在CustomControl1-TextBox继承-控件中触发Paint事件。

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

为什么不能获得Paint事件?

Why can't I get the Paint event?

此外,如果我想使Enabled属性不可见且用户无法设置,我会做:

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

但是这样,我都不能设置 real Enabled属性,我的意思是后备字段。

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

推荐答案

我找到了一个解决方案。看起来TextBox甚至禁用了子类的Paint事件。但是您可以强制WM_PAINT位调用SetStyle:

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

这篇关于从TextBox继承的vb .NET自定义控件不会触发Paint事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆