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