Click here to Skip to main content
15,881,803 members
Articles / Desktop Programming / Win32
Article

How to make content of RichTextBox unselectable.

,
Rate me:
Please Sign up or sign in to vote.
3.22/5 (5 votes)
7 Apr 2008GPL31 min read 29.2K   379   9   5
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.<o:p>

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.<o:p>

‘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.<o:p>

‘Now add few Events to source code.<o:p>

‘Form load event<o:p>

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<o:p>

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

RichTextBox1.ReadOnly = True<o:p>
RichTextBox1.BorderStyle = BorderStyle.None ‘optional
RichTextBox1.WordWrap = True ‘optional <o:p>

End Sub<o:p>

<o:p>

‘Add events to Richtextbox1

Private Sub RichTextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.GotFocus<o:p>

RichTextBox1.SelectionLength = 0<o:p>
Label1.Focus()<o:p>

End Sub<o:p>

<o:p> Private Sub RichTextBox1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDoubleClick<o:p>

RichTextBox1.SelectionLength = 0
Label1.Focus()<o:p>

<o:p> End Sub<o:p>

<o:p>

Private Sub RichTextBox1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBox1.SelectionChanged<o:p>

RichTextBox1.SelectionLength = 0
Label1.Focus()<o:p>

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, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
India India
IPStudents.Info - Its all about creativity and ideas.

Written By
Software Developer Speedovation
India India
CssEditor.In - Wait less code more.

Comments and Discussions

 
Suggestionmy Solution Pin
Kyle Knapp24-Sep-13 3:15
Kyle Knapp24-Sep-13 3:15 
GeneralVB.NET Pin
abowzer adam hassan17-Jun-08 1:33
abowzer adam hassan17-Jun-08 1:33 
GeneralRe: VB.NET Pin
ksor hien4-Jun-09 0:43
ksor hien4-Jun-09 0:43 
Generalhi Pin
ESTAN7-Apr-08 23:05
ESTAN7-Apr-08 23:05 
GeneralRe: hi Pin
chanderp8-Apr-08 2:44
chanderp8-Apr-08 2:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.