RichTextBox Label URL?

  • Thread starter Thread starter JZ
  • Start date Start date
J

JZ

Hi,

I remember reading sometime ago that is possible to create a RichtextBox
label.

Has anyone got any source or a URL.

I'm looking for improvements on just setting ReadOnly = true or Enabled =
false.
Something which hides the caret / selection character.

Help!!!!!!!!!!!!!!!!!!!
 
Yeah I'd seen that, I've tried various events but it still shows selection.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
HideCaret(Me.RichTextBox1.Handle)
End Sub
Private Sub RichTextBox1_Enter(ByVal sender As Object, ByVal e As
System.EventArgs) Handles RichTextBox1.Enter
HideCaret(Me.RichTextBox1.Handle)
End Sub
Private Sub RichTextBox1_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDown
HideCaret(Me.RichTextBox1.Handle)
End Sub
Private Sub RichTextBox1_MouseEnter(ByVal sender As Object, ByVal e As
System.EventArgs) Handles RichTextBox1.MouseEnter
HideCaret(Me.RichTextBox1.Handle)
End Sub
Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, ByVal
e As System.EventArgs) Handles RichTextBox1.SelectionChanged
RichTextBox1.SelectionStart = 0
RichTextBox1.SelectionLength = 0
HideCaret(Me.RichTextBox1.Handle)
End Sub
Private Sub RichTextBox1_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseUp
HideCaret(Me.RichTextBox1.Handle)
End Sub

Any further advice?
 
Hi,

Well I think a combination of
'--------
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
' WM_NCLBUTTONDOWN WM_LBUTTONDOWN
If Me.ReadOnly AndAlso (m.Msg = &HA1 OrElse m.Msg = &H201) Then
Console.WriteLine("ignored")
Return 'ignore it
End If
MyBase.WndProc(m)
End Sub 'WndProc
'--------
And
'--------
HideCaret(Me.RichTextBox1.Handle)
'--------

Should do it, but I haven't quite figured it out yet.
Its late in the UK, I'll work on it tomorrow.

Thanks,

--
JZ
Imran Koradia said:
 
Hi,

I have come up with a solution, its not 100% but I think its as good as I'm
going to get it.
If you click and move your mouse over the label from start to finish you get
a little flicker.
But apart from that it works, you can still use URLs too.

Well just in case anyone is reading this in the future here's to code....

Imports System.Windows.Forms
Imports System.ComponentModel

Public Class ReadOnlyRichTextBox
Inherits RichTextBox

Private Declare Auto Function HideCaret Lib "user32.dll" (ByVal hwnd As
IntPtr) As Int32

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

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

<DefaultValue(True)> Public Shadows Property [ReadOnly]() As Boolean
Get
Return True
End Get
Set(ByVal Value As Boolean)
End Set
End Property

<DefaultValue(False)> Public Shadows Property TabStop() As Boolean
Get
Return False
End Get
Set(ByVal Value As Boolean)
End Set
End Property

Public Sub New()
MyBase.ReadOnly = True
MyBase.TabStop = False
Me.SetStyle(ControlStyles.Selectable, False)
HideCaret(Me.Handle)
End Sub

Private Sub ReadOnlyRichTextBox_SelectionChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles MyBase.SelectionChanged
Me.SelectionLength = 0
HideCaret(Me.Handle)
End Sub

Private Sub ReadOnlyRichTextBox_MouseDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
HideCaret(Me.Handle)
End Sub

Private Sub ReadOnlyRichTextBox_MouseUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
HideCaret(Me.Handle)
End Sub

End Class

If anyone improves on that please post in here, even months ahead.
 
In fact if you don't need to use URLs then you can add.

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
' WM_NCLBUTTONDOWN WM_LBUTTONDOWN
If Me.ReadOnly AndAlso (m.Msg = &HA1 OrElse m.Msg = &H201) Then
Console.WriteLine("ignored")
Return 'ignore it
End If

MyBase.WndProc(m)
End Sub 'WndProc

I also forgot to mention that you'd need to change the borderstyle to none
and the background color.
 
Back
Top