none
Get currently selected Content control RRS feed

  • Question

  • Hi,
    I want to design a task pane which modifies the tag and title of the content control (just like property dialog of the developer ribbon). In the case where a rich text content control has other plain text controls, it is hard to programmatically find if it has been selected in word 2010/2013.
    Document structure
    1) Add a table with two columns 
    2) Select both columns and add a richtext content control (this should span both columns)
    3) Finally add a plain text content control inside each column (i.e. inside the richtext)

    I used two methods to figure out the selection of rich text box.
    1) Using ActiveDocument.ContentControlOnEnter event. This does not hit when the rich text control is clicked or selected from the title label.  (but the property dialog of the developer ribbon picks it up)
    2) Application.WindowSelectionChange event and mine for control as follow. This one can’t pick up the richtext as well

    public ContentControl IsSelectionInCC(Selection secl)
            {
                ContentControl cc = null;
                
                try
                {
                    Range r = secl.Range;
                    var lol = secl.Range as ContainerControl;
    
                    Document doc = Globals.ThisAddIn.Application.ActiveDocument;
                    r.Start = r.Document.Content.Start;
                    int count = r.ContentControls.Count;
                    if (count > 0)
                    {
                        foreach (ContentControl item in r.ContentControls)
                        {
                            string name = item.Title;
                            if (secl.InRange(item.Range))
                                cc = item;
                        }
    
                    }
                }
                catch { }
                return cc;
            }

    Please let me know any other useful method or the mistake I have made.

    Thank you

    Pati

    Monday, October 7, 2013 5:34 AM

All replies

  • Hi Pati,

    >> Using ActiveDocument.ContentControlOnEnter event. This does not hit when the rich text control is clicked or selected from the title label.  (but the property dialog of the developer ribbon picks it up)<<

    This event fires only for the content control that you enter and not for parent content controls. For example, if you have a text box content control nested inside a group content control, and you place the cursor inside the text box content control, this event fires only once for the text box content control; it does not fire for the parent group content control.(refer to Document.ContentControlOnEnter Event)

    When we click it, most time we enter in a plain text content controls. However only if you press right key after you click to enter in the rich text control instead of the plain content controls, the event will fire.

    To see clearly, I suggest you testing it in design mode, you can see the right postion of the cursor like figure below:

    >>) Application.WindowSelectionChange event and mine for control as follow. This one can’t pick up the richtext as well<<

    I modified the code for testing, when I selected the range it can pick up rich text succefully. Could you please provide us more code for troubleshooting? Below is my test code:

    private void ThisDocument_Startup(object sender, System.EventArgs e)
            {
    
                this.Application.ActiveDocument.ContentControlOnEnter += ActiveDocument_ContentControlOnEnter;
                this.Application.WindowSelectionChange += Application_WindowSelectionChange;
            }
     void Application_WindowSelectionChange(Selection secl)
            {
    
                IsSelectionInCC(secl);
            }
    public ContentControl IsSelectionInCC(Selection secl)
            {
                ContentControl cc = null;
    
                try
                {
                    Range r = secl.Range;
                    var lol = secl.Range as ContainerControl;
                    //Document doc = Globals.ThisAddIn.Application.ActiveDocument;
                    r.Start = r.Document.Content.Start;
                    int count = r.ContentControls.Count;
                    if (count > 0)
                    {
                        foreach (ContentControl item in r.ContentControls)
                        {
                            string name = item.Title;
                            if (secl.InRange(item.Range))
                                cc = item;
                        }
                    }
                }
                catch { }
                return cc;
            }
    void ActiveDocument_ContentControlOnEnter(ContentControl ContentControl)
            {
                //throw new NotImplementedException();
            }
    
    

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Patip Thursday, October 10, 2013 10:53 PM
    • Unmarked as answer by Patip Tuesday, October 15, 2013 11:52 PM
    Wednesday, October 9, 2013 1:43 PM
    Moderator
  • Hi Fei,

    Thank you for your response.

    The option of going to design mode and selecting the rich text worked. That is if you click on the correct place user can select the rich text. However, it is not that user friendly and inconsistent. 

    Right key works as well for all child elements except the last element(right most).

    With respect to the code, that's exactly what I have done. But if you click on the label of rich text although you see the selecting of all child controls, it does not pick the rich text

    Let me know if you can work out away to pick rich-text just by the title labels, which is much more intuitive way for the user.  

    Regards

    Pati

    Thursday, October 10, 2013 11:00 PM
  • Hi Pati,

    >>Let me know if you can work out away to pick rich-text just by the title labels, which is much more intuitive way for the user.  <<

    Do you mean click like fingure below?

    If I understood correctly, we can get current content control by WindowSelectionChange event. In this event, we will get three content control. However it will not trigger ContentControlOnEnter event.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Wednesday, October 16, 2013 12:25 PM
    Moderator
  • Hi Fei,

    Yes, your figure shows exactly what I meant. 

    However, in the above IsSelectionInCC code called in WindowSelectionChange, the inRange() returns false. 

    if (secl.InRange(item.Range))
               cc = item;

    So did not work for me.

    Regards

    Pati

    Thursday, October 17, 2013 1:17 AM