none
Allow content control styling while placeholder text is read-only RRS feed

  • Question

  • Good morning

    Im looking for a way to have a content control with read-only placeholder text and at the same time the user must be able to apply style formatting to the content control's text.

    The LockContents property prevents the place holder text from being edited by the user:

                    Microsoft.Office.Tools.Word.Document CurrDocument = Globals.Factory.GetVstoObject(CurrentApplication.ActiveDocument);
     
                    Microsoft.Office.Tools.Word.RichTextContentControl richTextControl1;
     
                    CurrDocument.Paragraphs[1].Range.InsertParagraphBefore();
                    CurrDocument.Paragraphs[1].Range.Select();
     
                    richTextControl1 = CurrDocument.Controls.AddRichTextContentControl("richTextControl1");
                    richTextControl1.PlaceholderText = "Sample text";
                    richTextControl1.LockContents = true;

    This however also prevents any style formatting on the place holder text.

    If for example a content control is housed in a paragraph and the user selects the whole paragraph he/she is unable to apply any formatting to the paragraph because it contains a Locked content control.

    Is there any work around for this?

    Wednesday, January 4, 2012 8:12 AM

Answers

  • Hi Denys

    When the user formats, he's accustomed to making a selection, then telling Word what formatting he wants to apply (clicks a Ribbon button, right-clicks and chooses the menu command, presses a keyboard combination...)

    So the user would make a selection in the content control, then choose a command. Your code needs to re-purpose the formatting commands you want to support.

    Since you've posted in hte VSTO forum, I'm going to assume this is a VSTO document or add-in.

    In order to repurpose commands, you need to add a Ribbon XML component to your customization. In the Ribbon XML, define a <commands> section and a <command> element for every built-in formatting command being repurposed.

    You'll find any number of samples if you search the forum on terms such as:
    <commands><command>


    Cindy Meister, VSTO/Word MVP
    Monday, January 9, 2012 7:28 AM
    Moderator

All replies

  • Hi Denys

    You'd have to provide the interface for the formatting and your code would need to unlock, apply the formatting, then lock the content control again.

    A note about Placeholder Text: PlaceholderText always displays in the built-in style "Placeholder Text". This is a hidden style, meaning you'll never see the style name in the UI. All content controls share the same style for placeholder text, so changing this style for one will change it for all.

    The only way you can have "locked" fields show different formatting that's adapted to the surrounding text is to populate them using the Range.Text property of the content control.

    You'll notice that, if you assign placeholder text, apply formatting, then change the text that the formatting reverts.


    Cindy Meister, VSTO/Word MVP
    Wednesday, January 4, 2012 8:40 AM
    Moderator
  • Hello Cindy

    Could you please provide a code example of how this can be done?

    I am aware that I need to unlock content controls to allow formatting when a user selects a portion of text which inludes a content control and then lock it again after the formatting has been applied.What I cannot figure out is how to actually do this in code.I would imagine I'd require something like a SelectionFocus and SelectionLeaveFocus events in Word but all there seems to be is WindowSelectionChanged which fires both when you select text and when you unselect it so that doesn't really help me

    Wednesday, January 4, 2012 9:05 AM
  • Hi Denys

    When the user formats, he's accustomed to making a selection, then telling Word what formatting he wants to apply (clicks a Ribbon button, right-clicks and chooses the menu command, presses a keyboard combination...)

    So the user would make a selection in the content control, then choose a command. Your code needs to re-purpose the formatting commands you want to support.

    Since you've posted in hte VSTO forum, I'm going to assume this is a VSTO document or add-in.

    In order to repurpose commands, you need to add a Ribbon XML component to your customization. In the Ribbon XML, define a <commands> section and a <command> element for every built-in formatting command being repurposed.

    You'll find any number of samples if you search the forum on terms such as:
    <commands><command>


    Cindy Meister, VSTO/Word MVP
    Monday, January 9, 2012 7:28 AM
    Moderator