Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to make content of RichTextBox unselectable.

0.00/5 (No votes)
7 Apr 2008 1  
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...

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here