2010-08-25 77 views
2

我需要一個始終禁用的多行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屬性,我的意思是後臺領域。

回答

4

我找到了解決方案。它看起來像一個TextBox即使對於子類也禁用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 

它完全如預期:)

+0

+1張貼解決你自己的問題。 – Heinzi 2010-08-25 09:38:45

+0

我還有一些小問題:我的文本框現在禁用,不可選擇和漂亮的顏色,但wordwrapping只是不好。如果DrawString不能適合當前行,則它將截斷一個單詞,然後繼續到下一行(但該單詞已被截斷)。 我想用我的DisabledTextBox的着色功能來使用TextBox的wordwrapping功能。 想法? – vulkanino 2010-08-25 15:05:27

+1

看起來像我今天:) 回答自己Replasce的拉繩有: e.Graphics.DrawString(Me.Text,Me.Font,刷,Me.ClientRectangle) 有一個矩形,而不是一個點,給出了一個好的文字包裝。 – vulkanino 2010-08-25 15:15:27

0

這裏是你的答案:

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 
+0

添加一些評論,你的回答給OP自我答案增加了什麼? – Yaroslav 2012-10-27 16:04:05