2013-07-05 60 views
0

在我的用戶控件中,我想阻止用戶選擇文本,我的意思是我想取消(忽略)所有的鼠標事件,我已經重寫了重要的事件,但似乎什麼都沒有,因爲我仍然可以選擇控件上的文本。忽略選擇繼承的RichTextbox上的文本?

PS:不是我選擇禁用控制以防止選擇,我想禁用選擇。

Public Class RichTextLabel : Inherits RichTextBox 

    Private Declare Function HideCaret Lib "user32" (ByVal hwnd As IntPtr) As Integer 

    Public Sub New() 
     Me.TabStop = False 
     Me.Cursor = Cursors.Default 
     Me.Size = New Point(200, 20) 
     Me.ReadOnly = True 
     Me.BorderStyle = BorderStyle.None 
     Me.ScrollBars = RichTextBoxScrollBars.None 
    End Sub 

    Public Sub Add_Colored_Text(ByVal text As String, _ 
           ByVal color As Color, _ 
           Optional ByVal font As Font = Nothing) 

     Dim index As Int32 = Me.TextLength 
     Me.AppendText(text) 
     Me.SelectionStart = index 
     Me.SelectionLength = Me.TextLength - index 
     Me.SelectionColor = color 
     If font IsNot Nothing Then Me.SelectionFont = font 
     Me.SelectionLength = 0 

    End Sub 

#Region " Overrided Events " 

    Protected Overrides Sub OnClick(ByVal e As EventArgs) 
     HideCaret(Me.Handle) 
     Return 
    End Sub 

    Protected Overrides Sub OnSelectionChanged(ByVal e As EventArgs) 
     HideCaret(Me.Handle) 
     Return 
    End Sub 

    Protected Overrides Sub OnMouseClick(ByVal e As MouseEventArgs) 
     HideCaret(Me.Handle) 
     ' MyBase.OnClick(e) 
     Return 
    End Sub 

    Protected Overrides Sub OnMouseDoubleClick(ByVal e As MouseEventArgs) 
     HideCaret(Me.Handle) 
     Return 
    End Sub 

    Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs) 
     HideCaret(Me.Handle) 
     Return 
    End Sub 

    Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs) 
     HideCaret(Me.Handle) 
     Return 
    End Sub 

#End Region 

#Region " Handled Events " 

    Private Sub On_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Me.MouseHover 
     HideCaret(Me.Handle) 
    End Sub 

    Private Sub On_TextChanged(sender As Object, e As EventArgs) Handles Me.TextChanged 
     HideCaret(Me.Handle) 
    End Sub 

#End Region 

End Class 
+0

爲什麼你不能禁用該控件?僅僅是因爲它將文本變成灰色嗎? – tehDorf

回答

1

一個黑客一點,但至少這是一個乾淨的:

  • 的元素加入,可以有重點
  • HideSelection = True(默認設置爲True類,只是要注意不要改變它)
  • 重寫OnEnter事件將焦點傳遞給子控件

這樣,RichTextBox從不會保持焦點,因此即使它具有選定的文本,它也不會向用戶顯示。


Public Class RichTextLabel : Inherits RichTextBox 

    Private controlToTakeFocus As New Label() With { 
     .Width = 0, 
     .Height = 0, 
     .Text = String.Empty} 

    Public Sub New() 
     ' Default value is True, but is required for this solution 
     'Me.HideSelection = True 
    End Sub 

    Protected Overrides Sub OnMouseDown(e As MouseEventArgs) 
     Me.Parent.Controls.Add(controlToTakeFocus) 
     controlToTakeFocus.Focus() 
    End Sub 

End Class 

編輯:控制controlToTakeFocus需要能夠採取集中,它不能,直到它在Form。我將覆蓋的事件更改爲OnMouseDown,並添加一行以將控件添加到RichTextLabel的父級,然後嘗試將焦點放在上面。這可能是一個更好的地方,但這只是爲了讓它工作。

+0

謝謝,但沒有爲我工作,複製你的代碼並粘貼它,也試圖覆蓋鼠標事件,然後調用「controlToTakeFocus.focus」,但沒有,我仍然可以選擇控件上的文本,我已經在Winforms中用FW4.0進行了測試。 – ElektroStudios

+0

@ElektroHacker我已更新我的代碼 – tehDorf