Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I still have the famous code that I post below, it seems to work perfectly, but I don't know why the position where the cursor is displayed (SelectionMargin) is not what you would expect, that is, it is shifted by about ten pixels towards the right axis x and y always up by 10 pixels, and for no apparent reason, after a bit of data entry it works perfectly as expected. Suggestions? Modified with complete derived class code.

What I have tried:

VB.NET
  1  Option Explicit On
  2  Option Strict Off
  3  
  4  Public Class Super_RichTextBox_PanAn : Inherits System.Windows.Forms.RichTextBox
  5  
  6      Private My_Cursor As Cursor
  7  
  8  #Region "----------------> Eventi Super_RichTextBox_PanAn"
  9       Public Sub New()
 10  
 11          MyBase.New()
 12          Call Me.InitializeComponent()
 13  
 14      End Sub
 15  
 16      Private Sub InitializeComponent()
 17  
 18          With Me
 19  
 20              .AllowDrop = True
 21              .ShowSelectionMargin = True
 22  
 23              .AutoWordSelection = True
 24              .BorderStyle = BorderStyle.FixedSingle
 25              .Dock = DockStyle.Fill
 26              .AcceptsTab = True
 27              .HideSelection = False
 28              .EnableAutoDragDrop = True
 29              .ShortcutsEnabled = True
 30              .AutoWordSelection = True
 31  
 32              .SelectionIndent = 100
 33  
 34          End With
 35  
 36      End Sub
 37  
 38      Private Sub Super_RichTextBox_PanAn_HandleCreated(sender As Object, e As System.EventArgs) Handles MyBase.HandleCreated
 39  
 40          My_Cursor = New Cursor(My.Resources.Cursor_Icon_Dx.Handle)
 41  
 42      End Sub
 43  
 44      Private Sub Super_RichTextBox_PanAn_ContentsResized(sender As Object, e As System.Windows.Forms.ContentsResizedEventArgs) Handles MyBase.ContentsResized
 45  
 46      End Sub
 47  
 48      Private Sub Super_RichTextBox_PanAn_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
 49  
 50          Old_Local_Mouse_Position = Me.PointToClient(System.Windows.Forms.Cursor.Position)
 51          Cursor_Shown = False
 52  
 53      End Sub
 54  
 55  #End Region '<------------ Eventi Super_RichTextBox_PanAn
 56  
 57      Protected Overrides Sub OnMouseMove(e As System.Windows.Forms.MouseEventArgs)
 58  
 59          With Me
 60  
 61              If .SelectionIndent <= 0 Then
 62                  If Not .ClientRectangle.Contains(e.Location) Then
 63                      .Capture = False
 64                  ElseIf Not .Capture Then
 65                      .Capture = True
 66                  End If
 67              End If
 68  
 69              Dim X As Integer = PointToClient(Control.MousePosition).X
 70  
 71              If X <= Me.SelectionIndent Then
 72  
 73                  .Capture = True
 74  
 75                  If Cursor = My_Cursor Then 'If Cursor = Cursors.Arrow Then
 76                      GoTo Salto
 77                  End If
 78  
 79                  Cursor = My_Cursor
 80                  'Cursor = Cursors.Arrow
 81  
 82              Else
 83  
 84                  .Capture = False
 85  
 86                  If Cursor = Cursors.IBeam Then
 87                      GoTo Salto
 88                  End If
 89  
 90                  Cursor = Cursors.IBeam
 91  
 92              End If
 93  
 94  Salto:
 95  
 96  
 97              If Not Me.ClientRectangle.Contains(e.Location) Then
 98                  Me.Capture = False
 99                  If Cursor <> Cursors.Default Then
100                      Cursor = Cursors.Default
101                  End If
102              End If
103  
104              Dim LocalMousePosition As Point = Me.PointToClient(System.Windows.Forms.Cursor.Position)
105  
106              Dim CaretLoc As Point = Nothing
107  
108              If LocalMousePosition <> Old_Local_Mouse_Position Then
109  
110                  If Cursor_Shown = False Then
111  
112                      Cursor_Shown = True
113  
114                  End If
115  
116              End If
117  
118          End With
119  
120          MyBase.OnMouseMove(e)
121  
122      End Sub
123  
124  #Region "------------------> HideCaret - ShowCaret"
125       Private Property Old_Local_Mouse_Position As Point
126      Private _Cursor_Shown As Boolean = True
127  
128      Public Property Cursor_Shown As Boolean
129          Get
130              Return _Cursor_Shown
131          End Get
132          Set(ByVal Value As Boolean)
133              If Value = _Cursor_Shown Then
134                  Return
135              End If
136              If Value Then
137                  System.Windows.Forms.Cursor.Show()
138              Else
139                  System.Windows.Forms.Cursor.Hide()
140              End If
141              _Cursor_Shown = Value
142          End Set
143      End Property
144  
145  #End Region '<-------------- HideCaret - ShowCaret
146  
147  End Class
Posted
Updated 12-Nov-23 23:18pm
v6
Comments
[no name] 11-Nov-23 12:53pm    
You are "capturing" the mouse; that changes it's behaviour / reference system. When you fiddle like this, you make "incremental" changes so you can "observe" changes in behaviour; instead of trying to figure out how to "undo" something.
Ralf Meier 11-Nov-23 19:11pm    
Please explain me what the sense of these code is - for you.
The behaviour you try to achieve (for me) is standard-functionality of a RichTextbox ... (without this code-snippet)
10071962 12-Nov-23 0:24am    
The problem is that the richtextbox at least in .Net 4 does not display the text selection cursor (rightward arrow). Must be a Bug since there is nothing in the derived class that can interfere, just the setting SelectionIndent = 10. Gerry Schmitz What should I undo? Furthermore, after inserting a few lines of text, the mouse behaves as expected.
10071962 12-Nov-23 0:44am    
For completeness, if I add this code, the position is perfect, but then obviously it "busts" when it starts working correctly again.

Dim New_Point = Me.PointToScreen(New Point(e.X, e.Y))
New_Point.X = New_Point.X - 10
New_Point.Y = New_Point.Y + 10
Cursor.Position = New_Point
Ralf Meier 12-Nov-23 5:13am    
Please post your complete code - use the "improve question" widget inside the question.
While testing your actual code-snippet I found that the Cursor isn't set to standard when leaving the modified control. So you are doing something wrong - to make a suggestion I msut understand your issue ...
Another thing : if you want a reaction from some of us you should use the "reply"-widget at the comment you are refering ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900