Products Technologies Demo Docs Blog Support Company

Sample: Getting the TextField at the Current Mouse Location

Last week, I published two useful code snippets for the TX Text Control TextField handling. Today, I would like to complete this series of code snippets by adding another useful function. The method GetTextFieldFromLocation returns the TextField at a specific location such as the current mouse position. If no TextField exists at this location, it returns null. private TXTextControl.TextField GetTextFieldFromLocation(Point Location) { Graphics g = textControl1.CreateGraphics(); int iDpi =…

Sample: Getting the TextField at the Current Mouse Location

Last week, I published two useful code snippets for the TX Text Control TextField handling. Today, I would like to complete this series of code snippets by adding another useful function. The method GetTextFieldFromLocation returns the TextField at a specific location such as the current mouse position. If no TextField exists at this location, it returns null.

private TXTextControl.TextField GetTextFieldFromLocation(Point Location)
{
    Graphics g = textControl1.CreateGraphics();
    int iDpi = (int)(1440 / g.DpiX);

    Point pTwipsLocation = new Point(Location.X * iDpi, Location.Y * iDpi);

    foreach (TXTextControl.TextField tfField in textControl1.TextFields)
    {
        Rectangle rFieldBounds = new Rectangle(tfField.Bounds.X - textControl1.ScrollLocation.X,
                                               tfField.Bounds.Y - textControl1.ScrollLocation.Y,
                                               tfField.Bounds.Width,
                                               tfField.Bounds.Height);

        if (pTwipsLocation.X >= rFieldBounds.X &&
            pTwipsLocation.X <= rFieldBounds.X + rFieldBounds.Width &&
            pTwipsLocation.Y >= rFieldBounds.Y &&
            pTwipsLocation.Y <= rFieldBounds.Y + rFieldBounds.Height)
        {
            return tfField;
        }
    }

    return null;
}

This method can be perfectly used to show a tool tip when the mouse hovers an inserted TextField:

private TXTextControl.TextField tfCurField = null;

private void textControl1_MouseMove(object sender, MouseEventArgs e)
{
    TXTextControl.TextField tfField = GetTextFieldFromLocation(e.Location);

    if (tfField != null)
    {
        if (tfCurField == null || tfField.Bounds != tfCurField.Bounds)
        {
            toolTip1.Show(tfField.Name, textControl1, e.Location);
        }
    }
    else
        toolTip1.Hide(textControl1);

    tfCurField = tfField;
}

Want more of this? Request such snippets by opening a support case here:

Open TX Text Control Support Case

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.


ASP.NETWindows FormsWPF

Official TX Text Control .NET Sample Applications Are Now Hosted on GitHub

This article gives a quick overview of the new repositories, their structure and our plans for the future.


ASP.NETJavaScriptDocument Editor

Detect Toggle Button Changes Using a MutationObserver

This article shows how to detect changes of toggle buttons in the ribbon of the web editor using a MutationObserver. The state of a toggle button in the ribbon visualizes the state of a certain…


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…


Windows FormsSampleShortcuts

Zoom Tricks: Disabling CTRL + MOUSE WHEEL and More

This article shows how to disable CTRL + MOUSE WHEEL, implement zooming with keyboard and reset the zoom factor to its default value.