ControlCollection.AddComboBoxContentControl 方法

定义

重载

AddComboBoxContentControl(String)

在文档的当前选定内容中添加一个新的 ComboBoxContentControl

AddComboBoxContentControl(ContentControl, String)

将新 ComboBoxContentControl 添加到集合中。 新控件基于文档中已存在的本机内容控件。

AddComboBoxContentControl(Range, String)

在文档的指定范围中,添加一个新的 ComboBoxContentControl

AddComboBoxContentControl(String)

在文档的当前选定内容中添加一个新的 ComboBoxContentControl

public:
 Microsoft::Office::Tools::Word::ComboBoxContentControl ^ AddComboBoxContentControl(System::String ^ name);
public Microsoft.Office.Tools.Word.ComboBoxContentControl AddComboBoxContentControl (string name);
abstract member AddComboBoxContentControl : string -> Microsoft.Office.Tools.Word.ComboBoxContentControl
Public Function AddComboBoxContentControl (name As String) As ComboBoxContentControl

参数

name
String

新控件的名称。

返回

ComboBoxContentControl,已添加到该文档。

例外

namenull,或其长度为零。

ControlCollection 中已存在具有相同名称的控件。

示例

下面的代码示例在文档的开头添加一个新的 ComboBoxContentControl 。 该示例还向用户可以在 控件中选择的项列表添加多种颜色的名称。

此版本适用于文档级自定义。 若要使用此代码,请将其粘贴到ThisDocument项目中的 类中,然后从 ThisDocument_Startup 方法调用 AddComboBoxControlAtSelection 方法。

private Microsoft.Office.Tools.Word.ComboBoxContentControl comboBoxControl1;

private void AddComboBoxControlAtSelection()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();
    this.Paragraphs[1].Range.Select();

    comboBoxControl1 = this.Controls.AddComboBoxContentControl("comboBoxControl1");
    comboBoxControl1.DropDownListEntries.Add("Red", "Red", 0);
    comboBoxControl1.DropDownListEntries.Add("Green", "Green", 1);
    comboBoxControl1.DropDownListEntries.Add("Blue", "Blue", 2);
    comboBoxControl1.PlaceholderText = "Choose a color, or enter your own";
}
Dim comboBoxControl1 As Microsoft.Office.Tools.Word.ComboBoxContentControl

Private Sub AddComboBoxControlAtSelection()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    Me.Paragraphs(1).Range.Select()
    comboBoxControl1 = Me.Controls.AddComboBoxContentControl("comboBoxControl1")
    With comboBoxControl1
        .DropDownListEntries.Add("Red", "Red", 0)
        .DropDownListEntries.Add("Green", "Green", 1)
        .DropDownListEntries.Add("Blue", "Blue", 2)
        .PlaceholderText = "Choose a color, or enter your own"
    End With
End Sub

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到ThisAddIn项目中的 类中,然后从 ThisAddIn_Startup 方法调用 AddComboBoxControlAtSelection 方法。

private Microsoft.Office.Tools.Word.ComboBoxContentControl comboBoxControl1;

private void AddComboBoxControlAtSelection()
{
    if (this.Application.ActiveDocument == null)
        return;

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();
    vstoDoc.Paragraphs[1].Range.Select();

    comboBoxControl1 = vstoDoc.Controls.AddComboBoxContentControl(
        "comboBoxControl1");
    comboBoxControl1.DropDownListEntries.Add("Red", "Red", 0);
    comboBoxControl1.DropDownListEntries.Add("Green", "Green", 1);
    comboBoxControl1.DropDownListEntries.Add("Blue", "Blue", 2);
    comboBoxControl1.PlaceholderText = "Choose a color, or enter your own";            
}
Dim comboBoxControl1 As Microsoft.Office.Tools.Word.ComboBoxContentControl

Private Sub AddComboBoxControlAtSelection()
    If Me.Application.ActiveDocument Is Nothing Then
        Return
    End If

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    vstoDoc.Paragraphs(1).Range.InsertParagraphBefore()
    vstoDoc.Paragraphs(1).Range.Select()
    comboBoxControl1 = vstoDoc.Controls.AddComboBoxContentControl("comboBoxControl1")
    With comboBoxControl1
        .DropDownListEntries.Add("Red", "Red", 0)
        .DropDownListEntries.Add("Green", "Green", 1)
        .DropDownListEntries.Add("Blue", "Blue", 2)
        .PlaceholderText = "Choose a color, or enter your own"
    End With
End Sub

注解

使用此方法在运行时在文档中的当前选定内容中添加一个新 ComboBoxContentControl 。 有关详细信息,请参阅 Adding Controls to Office Documents at Run Time

适用于

AddComboBoxContentControl(ContentControl, String)

将新 ComboBoxContentControl 添加到集合中。 新控件基于文档中已存在的本机内容控件。

public:
 Microsoft::Office::Tools::Word::ComboBoxContentControl ^ AddComboBoxContentControl(Microsoft::Office::Interop::Word::ContentControl ^ contentControl, System::String ^ name);
public Microsoft.Office.Tools.Word.ComboBoxContentControl AddComboBoxContentControl (Microsoft.Office.Interop.Word.ContentControl contentControl, string name);
abstract member AddComboBoxContentControl : Microsoft.Office.Interop.Word.ContentControl * string -> Microsoft.Office.Tools.Word.ComboBoxContentControl
Public Function AddComboBoxContentControl (contentControl As ContentControl, name As String) As ComboBoxContentControl

参数

contentControl
ContentControl

一个 ContentControl,它是新控件的基础。

name
String

新控件的名称。

返回

ComboBoxContentControl,已添加到该文档。

例外

contentControlnull.-或 - name is null 或 具有零长度。

ControlCollection 中已存在具有相同名称的控件。

contentControl不是构建基块库 (也就是说, TypecontentControl 属性没有值 Microsoft.Office.Interop.Word。WdContentControlType.wdContentControlComboBox) 。

示例

下面的代码示例为文档中已有的每个本机组合框创建一个新的 ComboBoxContentControl

此版本适用于文档级自定义。 若要使用此代码,请将其粘贴到ThisDocument项目中的 类中,然后从 ThisDocument_Startup 方法调用 CreateComboBoxControlsFromNativeControls 方法。

private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.ComboBoxContentControl> comboBoxControls;

private void CreateComboBoxControlsFromNativeControls()
{
    if (this.ContentControls.Count <= 0)
        return;

    comboBoxControls = new System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.ComboBoxContentControl>();
    int count = 0;

    foreach (Word.ContentControl nativeControl in this.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlComboBox)
        {
            count++;
            Microsoft.Office.Tools.Word.ComboBoxContentControl tempControl =
                this.Controls.AddComboBoxContentControl(nativeControl,
                "VSTOComboBoxContentControl" + count.ToString());
            comboBoxControls.Add(tempControl);
        }
    }
}
Private comboBoxControls As New System.Collections.Generic.List _
        (Of Microsoft.Office.Tools.Word.ComboBoxContentControl)

Private Sub CreateComboBoxControlsFromNativeControls()
    If Me.ContentControls.Count <= 0 Then
        Return
    End If

    Dim count As Integer = 0
    For Each nativeControl As Word.ContentControl In Me.ContentControls
        If nativeControl.Type = Word.WdContentControlType.wdContentControlComboBox Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.ComboBoxContentControl = _
                Me.Controls.AddComboBoxContentControl(nativeControl, _
                "VSTOComboBoxContentControl" + count.ToString())
            comboBoxControls.Add(tempControl)
        End If
    Next nativeControl
End Sub

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到ThisAddIn项目中的 类中,然后从 ThisAddIn_Startup 方法调用 CreateComboBoxControlsFromNativeControls 方法。

private System.Collections.Generic.List
    <Microsoft.Office.Tools.Word.ComboBoxContentControl> comboBoxControls;

private void CreateComboBoxControlsFromNativeControls()
{
    if (this.Application.ActiveDocument == null)
        return;

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    if (vstoDoc.ContentControls.Count <= 0)
        return;

    comboBoxControls = new System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.ComboBoxContentControl>();
    int count = 0;

    foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlComboBox)
        {
            count++;
            Microsoft.Office.Tools.Word.ComboBoxContentControl tempControl =
                vstoDoc.Controls.AddComboBoxContentControl(nativeControl,
                "VSTOComboBoxContentControl" + count.ToString());
            comboBoxControls.Add(tempControl);
        }
    }
}
Private comboBoxControls As New System.Collections.Generic.List _
        (Of Microsoft.Office.Tools.Word.ComboBoxContentControl)

Private Sub CreateComboBoxControlsFromNativeControls()
    If Me.Application.ActiveDocument Is Nothing Then
        Return
    End If

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If vstoDoc.ContentControls.Count <= 0 Then
        Return
    End If

    Dim count As Integer = 0
    For Each nativeControl As Word.ContentControl In vstoDoc.ContentControls
        If nativeControl.Type = Word.WdContentControlType.wdContentControlComboBox Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.ComboBoxContentControl = _
                vstoDoc.Controls.AddComboBoxContentControl(nativeControl, _
                "VSTOComboBoxContentControl" + count.ToString())
            comboBoxControls.Add(tempControl)
        End If
    Next nativeControl
End Sub

下面的代码示例为用户添加到文档的每个本机组合框创建一个新的 ComboBoxContentControl

此版本适用于文档级自定义。 若要使用此代码,请将其粘贴到 ThisDocument 项目中的 类中。 对于 C#,还必须将 ThisDocument_ComboBoxContentControlAfterAdd 事件处理程序附加到 ContentControlAfterAdd 类的 ThisDocument 事件。

void ThisDocument_ComboBoxContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlComboBox)
    {
        this.Controls.AddComboBoxContentControl(NewContentControl,
            "ComboBoxControl" + NewContentControl.ID);
    }
}
Private Sub ThisDocument_ComboBoxContentControlAfterAdd(ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean) Handles Me.ContentControlAfterAdd

    If NewContentControl.Type = Word.WdContentControlType.wdContentControlComboBox Then
        Me.Controls.AddComboBoxContentControl(NewContentControl, _
            "ComboBoxControl" + NewContentControl.ID)
    End If
End Sub

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到 ThisAddIn 项目中的 类中。 此外,必须将事件处理程序附加到ActiveDocument_ComboBoxContentControlAfterAddContentControlAfterAdd活动文档的 事件。

void ActiveDocument_ComboBoxContentControlAfterAdd(
    Word.ContentControl NewContentControl, bool InUndoRedo)
{
    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlComboBox)
    {
        vstoDoc.Controls.AddComboBoxContentControl(NewContentControl,
            "ComboBoxControl" + NewContentControl.ID);
    }
}
Private Sub ActiveDocument_ComboBoxContentControlAfterAdd( _
    ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean)

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If NewContentControl.Type = Word.WdContentControlType. _
        wdContentControlComboBox Then
        vstoDoc.Controls.AddComboBoxContentControl(NewContentControl, _
            "ComboBoxControl" + NewContentControl.ID)
    End If
End Sub

注解

使用此方法在运行时添加基于文档中本机内容控件的新 ComboBoxContentControl 。 当你在运行时创建 , ComboBoxContentControl 并且你希望在下次打开文档时重新创建同一个控件时,这非常有用。 有关详细信息,请参阅 Adding Controls to Office Documents at Run Time

适用于

AddComboBoxContentControl(Range, String)

在文档的指定范围中,添加一个新的 ComboBoxContentControl

public:
 Microsoft::Office::Tools::Word::ComboBoxContentControl ^ AddComboBoxContentControl(Microsoft::Office::Interop::Word::Range ^ range, System::String ^ name);
public Microsoft.Office.Tools.Word.ComboBoxContentControl AddComboBoxContentControl (Microsoft.Office.Interop.Word.Range range, string name);
abstract member AddComboBoxContentControl : Microsoft.Office.Interop.Word.Range * string -> Microsoft.Office.Tools.Word.ComboBoxContentControl
Public Function AddComboBoxContentControl (range As Range, name As String) As ComboBoxContentControl

参数

range
Range

Range,可为新控件提供边界。

name
String

新控件的名称。

返回

ComboBoxContentControl,已添加到该文档。

例外

namenull,或其长度为零。

ControlCollection 中已存在具有相同名称的控件。

示例

下面的代码示例在文档的开头添加一个新的 ComboBoxContentControl 。 该示例还向用户可以在 控件中选择的项列表添加多种颜色的名称。

此版本适用于文档级自定义。 若要使用此代码,请将其粘贴到ThisDocument项目中的 类中,然后从 ThisDocument_Startup 方法调用 AddComboBoxControlAtRange 方法。

private Microsoft.Office.Tools.Word.ComboBoxContentControl comboBoxControl2;

private void AddComboBoxControlAtRange()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();

    comboBoxControl2 = this.Controls.AddComboBoxContentControl(this.Paragraphs[1].Range,
        "comboBoxControl2");
    comboBoxControl2.DropDownListEntries.Add("Red", "Red", 0);
    comboBoxControl2.DropDownListEntries.Add("Green", "Green", 1);
    comboBoxControl2.DropDownListEntries.Add("Blue", "Blue", 2);
    comboBoxControl2.PlaceholderText = "Choose a color, or enter your own";
}
Dim comboBoxControl2 As Microsoft.Office.Tools.Word.ComboBoxContentControl

Private Sub AddComboBoxControlAtRange()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    comboBoxControl2 = Me.Controls.AddComboBoxContentControl(Me.Paragraphs(1).Range, "comboBoxControl2")
    With comboBoxControl2
        .DropDownListEntries.Add("Red", "Red", 0)
        .DropDownListEntries.Add("Green", "Green", 1)
        .DropDownListEntries.Add("Blue", "Blue", 2)
        .PlaceholderText = "Choose a color, or enter your own"
    End With
End Sub

此版本适用于面向 .NET Framework 4 或 .NET Framework 4.5 的应用程序级外接程序。 若要使用此代码,请将其粘贴到ThisAddIn项目中的 类中,然后从 ThisAddIn_Startup 方法调用 AddComboBoxControlAtRange 方法。

private Microsoft.Office.Tools.Word.ComboBoxContentControl comboBoxControl2;

private void AddComboBoxControlAtRange()
{
    if (this.Application.ActiveDocument == null)
        return;

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();

    comboBoxControl2 = vstoDoc.Controls.AddComboBoxContentControl(
        vstoDoc.Paragraphs[1].Range,
        "comboBoxControl2");
    comboBoxControl2.DropDownListEntries.Add("Red", "Red", 0);
    comboBoxControl2.DropDownListEntries.Add("Green", "Green", 1);
    comboBoxControl2.DropDownListEntries.Add("Blue", "Blue", 2);
    comboBoxControl2.PlaceholderText = "Choose a color, or enter your own";            
}
Dim comboBoxControl2 As Microsoft.Office.Tools.Word.ComboBoxContentControl

Private Sub AddComboBoxControlAtRange()
    If Me.Application.ActiveDocument Is Nothing Then
        Return
    End If

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    vstoDoc.Paragraphs(1).Range.InsertParagraphBefore()
    comboBoxControl2 = vstoDoc.Controls.AddComboBoxContentControl( _
        vstoDoc.Paragraphs(1).Range, "comboBoxControl2")
    With comboBoxControl2
        .DropDownListEntries.Add("Red", "Red", 0)
        .DropDownListEntries.Add("Green", "Green", 1)
        .DropDownListEntries.Add("Blue", "Blue", 2)
        .PlaceholderText = "Choose a color, or enter your own"
    End With
End Sub

注解

使用此方法在运行时在文档中的指定范围内添加新 ComboBoxContentControl 的 。 有关详细信息,请参阅 Adding Controls to Office Documents at Run Time

适用于