Products Technologies Demo Docs Blog Support Company

Set the Current Input Position on a Right Mouse Click

This VB.NET sample moves the text cursor to the right-click location in TX Text Control. The MouseDown event detects right-clicks, a helper function converts pixel coordinates to twips using the current DPI settings, and InputPosition places the caret at the calculated point.

Set the Current Input Position on a Right Mouse Click

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

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

Windows FormsWPF.NET

Create a Table of Contents in Windows Forms using C#

This article explains how to create a table of contents in Windows Forms using the ribbon or programmatically. Creating a table of contents is required to organize large documents.


Windows FormsList.NET

Two Ways to Restart Numbered Lists in TX Text Control

In TX Text Control, numbered lists are continued by default and need to be reset when required. There is more than one way if you want to restart numbered lists in a document. In this article, two…


.NETSample

Paste Special: The Easy Way to Implement

TX Text Control version 15.0 introduced a ClipboardFormat parameter on the Paste method, enabling native Paste Special functionality. The GetClipboardFormats method returns all available clipboard…


.NETSampleSections

How to Remove All Section Breaks in a Document?

TX Text Control 15.0 adds per-section page column support alongside existing section breaks. To remove all section breaks programmatically, iterate through SectionCollection using…


.NETPrintingSample

Batch Printing: How to Print Documents in One Print Job

Batch printing multiple documents as a single print job using TX Text Control relies on a .NET PrintDocument with PrintPage and QueryPageSettings events. Each page is rendered individually via the…

Share on this blog post on: