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

前端 未结 2 689
醉酒成梦
醉酒成梦 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:15

    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
    

提交回复
热议问题