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

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

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:

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



回答2:

here is your answer:

Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)     MyBase.OnPaint(e)     e.Graphics.FillRectangle(Brushes.LightGray, Me.DisplayRectangle)     Dim sf As New StringFormat     sf.FormatFlags = StringFormatFlags.NoWrap     sf.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Show 'if Mnemonic property is set to true     sf.HotkeyPrefix = Drawing.Text.HotkeyPrefix.Hide 'or none if Mnemonic property is set to false     sf.LineAlignment = StringAlignment.Center 'horizontal alignment     sf.Alignment = StringAlignment.Center ' vertical ...     Dim rect As Rectangle = Me.DisplayRectangle ' this is your text bounds for setting your text alignement using StringFormat(sf)     e.Graphics.DrawString("Something", Me.Font, Brushes.DarkOliveGreen, rect, sf) End Sub 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!