我需要一個始終禁用的多行TextBox,但它不應該以灰色自己繪製,但我想讓其設計者選擇顏色。vb從TextBox繼承的.NET自定義控件不會觸發Paint事件
我以前有同樣的要求,與始終黑色標籤(不帶多),所以我從標籤一樣繼承:
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
現在Paint事件在CustomControl1是從來沒有發射 - 文本框繼承 - 控制。
爲什麼我無法獲取Paint事件?
另外,如果我想使Enabled屬性不可見的,用戶沒有可設置的,我做的:
<Browsable(False),
DefaultValue(False)>
Public Overloads Property Enabled As Boolean
Get
Return False
End Get
Set(ByVal value As Boolean)
End Set
End Property
可是這樣一來,無論是我可以設置「真正的」 Enabled屬性,我的意思是後臺領域。
+1張貼解決你自己的問題。 – Heinzi 2010-08-25 09:38:45
我還有一些小問題:我的文本框現在禁用,不可選擇和漂亮的顏色,但wordwrapping只是不好。如果DrawString不能適合當前行,則它將截斷一個單詞,然後繼續到下一行(但該單詞已被截斷)。 我想用我的DisabledTextBox的着色功能來使用TextBox的wordwrapping功能。 想法? – vulkanino 2010-08-25 15:05:27
看起來像我今天:) 回答自己Replasce的拉繩有: e.Graphics.DrawString(Me.Text,Me.Font,刷,Me.ClientRectangle) 有一個矩形,而不是一個點,給出了一個好的文字包裝。 – vulkanino 2010-08-25 15:15:27