代码之家  ›  专栏  ›  技术社区  ›  vulkanino

从文本框继承的VB.NET自定义控件不会激发绘制事件

  •  3
  • vulkanino  · 技术社区  · 14 年前

    我需要一个总是被禁用的多行文本框,但它不应该用灰色来绘制自己,但我想让它的设计者选择颜色。

    我以前对一个总是黑的标签(没有多行)有同样的要求,所以我继承了标签,比如:

    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
    

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

    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-文本框继承-控件中激发绘制事件。

    为什么我不能参加绘画活动?

    另外,如果我想使启用的属性不可见并且用户无法设置,我会:

    <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”启用的属性,我的意思是支持字段。

    2 回复  |  直到 13 年前
        1
  •  4
  •   vulkanino    14 年前

    我找到了解决办法。它看起来像是一个文本框,即使对于子类也禁用了paint事件。但您可以强制wm_paint位调用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
    

    它按预期工作得很好:)

        2
  •  0
  •   moji_vb    13 年前

    这是你的答案:

    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