Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable text selection with mouse in a richtextbox 1

Status
Not open for further replies.

assimang

Programmer
Mar 11, 2008
96
Hello everybody.
Is it possible to disable text selection with mouse in a richtextbox? And how can I do this? Any help will be much appreciated.

Thanks in advanced
 
There was another one on this and a few other options for a rich text box just recently. If you do a search you should find it.

-I hate Microsoft!
-Forever and always forward.
 
i tried this in mouseDown and mouseUp events too
Private Sub RTB_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RTB.MouseClick
RTB.SelectionStart = Len(RTB.Text)
RTB.SelectionLength = 0
RTB.HideSelection = True
RTB.SelectionBackColor = RTB.BackColor
End Sub

And my problem is that I can see the blue selection background color. Can I stop it? And how? I also have tried RTB.SelectionBackColor = Color.Transparent but with no effect. Any help will be much appreciated.

Thanks in advanced
 
I think that yuou will find that RTB.HideSelection indicates whether or not the selection highlight should show AFTER the RichTextBox has lost focus.

[vampire][bat]
 
thread796-1452648

-I hate Microsoft!
-Forever and always forward.
 
Sorry that was a little different. In the SelectionChanged event you want to do:

Code:
RTB.Select(RTB.SelectionStart, 0)

-I hate Microsoft!
-Forever and always forward.
 
E&f when the user types in richtextbox (RTB) does it have the focus to true? Right? If yes, why if I use RTB.HideSelection=True doesn't work? The what exactly happening is that, while i keep mouseDown pressed i can see a blue selection backcolor like you can see if you double click in the text property in the property windows of vb.net and when I mousekeyUp everything is allright, which means I don't see this blue selection backcolor.
Thanks both of you.
 
The what exactly want to do is to disable the highlighting. Just not highlighting when the user tries to select a part of a text with mouse. Is it possible?
 
Finally i found the solution alone again

Private Sub RTB_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles RichTextBox1.MouseDown
Me.Focus()
RTB.SelectionStart = RTB.TextLength
RTB.SelectionLength = 0
End Sub

and the same code to the mouse up event. This prevents any kind of textselection with mouse and hides the highlighting, when the user tries to click or double click or drag with hold right click.
Although thanks anyone.
 
Two things come to mind.

1) This prevents re-positioning the cursor with the mouse (perhaps to correct a typing error)

2) Text can still be highlighted with the keyboard

2a) If you use the same technique to prevent highlighting with the keyboard, you will also prevent cursor movement


Hope this helps.

[vampire][bat]
 
Thanks earthandfire,
I have already prevent highlighting with the keyboard, but no with this technique. But with this:

Private Sub RTB_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RTB.KeyDown
Dim i As Integer = 0
Dim BackSpc As Boolean = False

If e.KeyCode = Keys.Back Then
If i > 0 Then
BackSpc = True
i = i - 1
End If
ElseIf e.KeyCode = Keys.Delete Or e.KeyCode = Keys.Left Or e.KeyCode = Keys.Right Or e.KeyCode = Keys.Up _
Or e.KeyCode = Keys.Down Or e.KeyCode = Keys.PageUp Or e.KeyCode = Keys.PageDown Or e.KeyCode = Keys.Home _
Or e.KeyCode = Keys.End Or e.KeyCode = Keys.Escape Or e.KeyCode = Keys.NumLock Or e.KeyCode = Keys.Insert _
Or e.KeyCode = Keys.Enter Or e.Control Then
e.Handled = True
e.SuppressKeyPress = True
ElseIf (e.KeyCode = Keys.V AndAlso e.Control) Then
e.Handled = True
e.SuppressKeyPress = True
End If
End Sub

As a conclusion, the user can type, can paste data nowhere, prevents correcting a typing error for example a word, but he can correct an error character, the cursor is always in the textlength as I want which means that users can't re-position the cursor with keyboard and he can't see the highlighting selection either through keyboard either through mouse as I want too. I hope it is right.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top