65.9K
CodeProject is changing. Read more.
Home

How to make content of RichTextBox unselectable.

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.22/5 (5 votes)

Apr 8, 2008

GPL3

1 min read

viewsIcon

29705

downloadIcon

379

you want to display some text in your application and side-by-side you want to prevent the user from copying the content. . One way to implement this use label, but label do not support the formatting of the text and RTF formats.

Download Source Code


Introduction

How to make content of RichTextBox unselectable.

In many situations, if you want to display some text in your application and side-by-side you want to prevent the user from copying the content. One way to implement this use label, but label do not support the formatting of the text and RTF formats. So in that case this simple tutorial can solve the problem completely.

This tutorial explains the way of using a RichTextBox and it also makes the text unselectable.


Using the code

‘The project starts from here.

‘Create a VB.NET solution.
‘You will see a form1 in it.
‘Now Add Richtextbox1 to this form.
‘Name it as richtextbox1.
‘Add a Label, Name it as Label1.

‘Now add few Events to source code.

‘Form load event

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

RichTextBox1.Rtf = File.ReadAllText("c:\m1.rtf") ‘input file in rtf format

RichTextBox1.ReadOnly = True
RichTextBox1.BorderStyle = BorderStyle.None ‘optional
RichTextBox1.WordWrap = True ‘optional

End Sub

‘Add events to Richtextbox1

Private Sub RichTextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.GotFocus

RichTextBox1.SelectionLength = 0
Label1.Focus()

End Sub

Private Sub RichTextBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDoubleClick

RichTextBox1.SelectionLength = 0
Label1.Focus()

End Sub

Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.SelectionChanged

RichTextBox1.SelectionLength = 0
Label1.Focus()

End Sub

Points of Interest

Just play more with .NET controls.

Just visit IPStudents.info for more exciting codes on VB.NET ,PHP and more...

History

First and I guess Last version...