I just read a thread in our Support Forum that a customer was asking how to set the current input position, when the end-user clicks the right mouse button. I would like to take this opportunity to publish the simple solution here.

We just need two things: the MouseDown event to check, if the end-user clicks the right mouse button and the InputPosition class to set the caret to that specific position. The returned coordinates from the MouseDown event can be passed to the InputPosition constructor.

Imports System.Drawing

Dim DPI As Point

Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
 DPI.X = e.Graphics.DpiX
 DPI.Y = e.Graphics.DpiY
End Sub

Private Function GetLocation(ByVal pt As Point) As Point
 Dim m_pt As New System.Drawing.Point
 Dim dpiX As Integer = DPI.X
 Dim dpiY As Integer = DPI.Y
 Dim offsetY As Integer = TextControl1.ScrollLocation.Y
 Dim offsetX As Integer = TextControl1.ScrollLocation.X

 m_pt = New Point(pt.X, pt.Y)
 m_pt.X = (m_pt.X * CInt((1440 / dpiX))) + offsetX
 m_pt.Y = (m_pt.Y * CInt((1440 / dpiY))) + offsetY

 Return m_pt
End Function

Private Sub TextControl1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextControl1.MouseDown
 If e.Button = MouseButtons.Right Then
  TextControl1.InputPosition = New TXTextControl.InputPosition(GetLocation(e.Location))
 End If
End Sub