Retrieve selection start index

Good day,

1) I am registering the event Document.Selection.Target.Changed. Even though i can access the selected text through Document.Selection.Target, i could not find a way to retrieve the start index of the selection (such as the native WinForms TextBox.SelectionStart). There may be a solution by using user32.dll or the Windows Automation API but it would be prefferable if i could avoid that.

2) I noticed that when a selection is made in the Editor, the ActiveSegment does not change. Is there a way to manualy set the ActiveSegment to the one that is being highlighted (possibly through Document.SetActiveSegmentPair) from the aforementioned event's context? The problem is that there is no property to reference the SegmentPair that is being highlighted so i can retrieve its id.

Thank you in advance,

George Evgeneiadis

Parents Reply Children
  • Thank you very much for your immediate reply.
    The solution for 1) works well.
    I think i did not explain myself well about 2). I am aware of the ActiveSegmentChanged event but that does not solve my issue. My problem is that if i highlight something in a segment other than the ActiveSegment, then the ActiveSegment does not change. I need to be able to change the ActiveSegment manually to that which is being highlighted. The Document.SetActiveSegmentPair requires the segment's Id and i can't find a way to retrieve it from the Document.Selection.Target.Changed event's context or from the Document.Selection public members.
  • Actually i have found a solution using reflection but i was wondering if there is a solution through the public API:
    var id = GetPropertyValue(targetSelection, "ContentSelection.From.Node.Parent.Segment.Properties.Id.Id");
    public static object GetPropertyValue(object src, string propName)
    {

    if (propName.Contains("."))
    {
    var temp = propName.Split(new char[] { '.' }, 2);
    return GetPropertyValue(GetPropertyValue(src, temp[0]), temp[1]);
    }
    else
    {
    var prop = src.GetType().GetProperty(propName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
    return prop != null ? prop.GetValue(src, null) : null;
    }
    }