Back to Devexpress

RichEditControl.GetBoundsFromPosition(DocumentPosition) Method

windowsforms-devexpress-dot-xtrarichedit-dot-richeditcontrol-dot-getboundsfromposition-x28-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-documentposition-x29.md

latest7.9 KB
Original Source

RichEditControl.GetBoundsFromPosition(DocumentPosition) Method

Gets bounds of the character at the specified position.

Namespace : DevExpress.XtraRichEdit

Assembly : DevExpress.XtraRichEdit.v25.2.dll

NuGet Package : DevExpress.Win.RichEdit

Declaration

csharp
public Rectangle GetBoundsFromPosition(
    DocumentPosition pos
)
vb
Public Function GetBoundsFromPosition(
    pos As DocumentPosition
) As Rectangle

Parameters

NameTypeDescription
posDocumentPosition

A target position.

|

Returns

TypeDescription
Rectangle

the area occupied by a character, or the System.Drawing.Rectangle.Empty value if the bounds could not be determined.

|

Remarks

You can use the GetBoundsFromPosition method only for the currently visible document part. If the specified position is not displayed, the method returns a System.Drawing.Rectangle.Empty value.

The rectangle’s location and size are expressed in currently specified measurement units. Use the Document.Unit property to change the measurement units.

Example

The following code highlights the line containing the caret. The Document.CaretPosition property determines the caret position, the RichEditControl.GetBoundsFromPosition method calculates the caret screen coordinates.

The RichEditControl instance is passed to the BarItem.ItemClick event handler using the BarItem.Tag property.

View Example

csharp
static void richEditControl_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
    RichEditControl richEdit = sender as RichEditControl;
    DocumentPosition pos = richEdit.Document.CaretPosition;
    if(pos != null) {
        System.Drawing.Rectangle rect = DevExpress.Office.Utils.Units.DocumentsToPixels(
            richEdit.GetBoundsFromPosition(pos), 
            richEdit.DpiX, 
            richEdit.DpiY);

        Section firstSection = richEdit.Document.Sections[0];
        int pageWidth = Convert.ToInt32(firstSection.Page.Width - firstSection.Margins.Left - firstSection.Margins.Right);
        e.Graphics.DrawLine(System.Drawing.Pens.Red, 
            new System.Drawing.Point(0, rect.Bottom), 
            new System.Drawing.Point(pageWidth, rect.Bottom));
    }
}
vb
Private Shared Sub richEditControl_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
    Dim richEdit As RichEditControl = TryCast(sender, RichEditControl)
    Dim pos As DocumentPosition = richEdit.Document.CaretPosition
    If pos IsNot Nothing Then
        Dim rect As System.Drawing.Rectangle = DevExpress.Office.Utils.Units.DocumentsToPixels(
            richEdit.GetBoundsFromPosition(pos),
            richEdit.DpiX,
            richEdit.DpiY)

        Dim firstSection As Section = richEdit.Document.Sections(0)
        Dim pageWidth As Integer = Convert.ToInt32(firstSection.Page.Width - firstSection.Margins.Left - firstSection.Margins.Right)
        e.Graphics.DrawLine(System.Drawing.Pens.Red,
                            New System.Drawing.Point(0, rect.Bottom),
                            New System.Drawing.Point(pageWidth, rect.Bottom))
    End If
End Sub

The following code snippets (auto-collected from DevExpress Examples) contain references to the GetBoundsFromPosition(DocumentPosition) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-richedit-perform-drag-and-drop-from-list-box/CS/Form1.cs#L46

csharp
Rectangle rect = Units.DocumentsToPixels(richEditControl1.GetBoundsFromPosition(pos),
    richEditControl1.DpiX, richEditControl1.DpiY);

winforms-richedit-rich-drag-and-drop-gridview-cell-data/CS/DragDropExample/Form1.cs#L117

csharp
Rectangle rect = Units.DocumentsToPixels(richEditControl1.GetBoundsFromPosition(pos),
    richEditControl1.DpiX, richEditControl1.DpiY);

winforms-richeditcontrol-common-api/CS/RichEditAPISample/CodeExamples/RichEditControlActions.cs#L359

csharp
System.Drawing.Rectangle rect = DevExpress.Office.Utils.Units.DocumentsToPixels(
    richEdit.GetBoundsFromPosition(pos),
    richEdit.DpiX,

winforms-richedit-perform-drag-and-drop-from-list-box/VB/Form1.vb#L37

vb
If pos Is Nothing Then Return
Dim rect As Rectangle = Units.DocumentsToPixels(richEditControl1.GetBoundsFromPosition(pos), richEditControl1.DpiX, richEditControl1.DpiY)
richEditControl1.Document.CaretPosition = pos

winforms-richedit-rich-drag-and-drop-gridview-cell-data/VB/DragDropExample/Form1.vb#L100

vb
If pos Is Nothing Then Return
Dim rect As Rectangle = Units.DocumentsToPixels(richEditControl1.GetBoundsFromPosition(pos), richEditControl1.DpiX, richEditControl1.DpiY)
richEditControl1.Document.CaretPosition = pos

winforms-richeditcontrol-common-api/VB/RichEditAPISample/CodeExamples/RichEditControlActions.vb#L367

vb
Dim rect As System.Drawing.Rectangle = DevExpress.Office.Utils.Units.DocumentsToPixels(
    richEdit.GetBoundsFromPosition(pos),
    richEdit.DpiX,

See Also

GetPositionFromPoint(PointF)

Document.Unit

RichEditControl.Unit

RichEditControl Class

RichEditControl Members

DevExpress.XtraRichEdit Namespace