How to Add, Select and Remove an Item in Combo Box in C#, VB.NET

A combo box is a commonly-used GUI widget. It is a combination of a drop-down list or list box and a single-line textbox, allowing the user either to type a value directly into the control or choose from the list of existing options. In this article, we'll introduce how to programmatically manage the item of combo box in Word file using Spire.Doc.

Here is a combo box in the sample Word document, which contains three items including A, B and C. In the following section, we'll add, select and remove an item in the combo box using code.

How to Add, Select and Remove an Item in Combo Box in C#, VB.NET

Code Snippet:

Step 1: Initialize a new instance of Document class and load the sample Word file.

Document document = new Document();
document.LoadFromFile( "test.docx");

Step 2: Get the combo box from the file.

foreach (Section section in document.Sections)
{
    foreach (Body body in section.ChildObjects)
    {
        foreach (DocumentObject bodyObj in body.ChildObjects)
        {
            if (bodyObj is StructureDocumentTag)
            { 
                if ((bodyObj as StructureDocumentTag).SDTProperties.SDTType == SdtType.ComboBox)
                {
                    SdtComboBox combo = (bodyObj as StructureDocumentTag).SDTProperties.ControlProperties as SdtComboBox;
                    
               }
            }
        }
    }

}

Step 3: Create a new item and set two parameters for it: display text and value. Call ListItems.Add() method to add the new item into combo box.

SdtListItem item = new SdtListItem("D","d");
combo.ListItems.Add(item);

Step 4: Call ListItems.RemoveAt() method to remove an item by its index.

combo.ListItems.RemoveAt(0);

Step 5: Call ListItems.SelectedValue() to choose an item from combo box.

combo.ListItems.SelectedValue = sdtItem;

Step 6: Save and launch the file.

document.SaveToFile("result.docx", FileFormat.Docx2013);
System.Diagnostics.Process.Start("result.docx");

Output:

How to Add, Select and Remove an Item in Combo Box in C#, VB.NET

Entire Code:

[C#]
using Spire.Doc;
using Spire.Doc.Documents;
namespace IteminCombo
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile("test.docx");
            foreach (Section section in document.Sections)
            {
                foreach (Body body in section.ChildObjects)
                {
                    foreach (DocumentObject bodyObj in body.ChildObjects)
                    {
                        if (bodyObj is StructureDocumentTag)
                        {
                            if ((bodyObj as StructureDocumentTag).SDTProperties.SDTType == SdtType.ComboBox)
                            {
                                SdtComboBox combo = (bodyObj as StructureDocumentTag).SDTProperties.ControlProperties as SdtComboBox;
                                SdtListItem item = new SdtListItem("D", "d");
                                combo.ListItems.Add(item);
                                foreach (SdtListItem sdtItem in combo.ListItems)
                                {
                                    if (string.CompareOrdinal(sdtItem.Value, "d") == 0)
                                    {
                                        combo.ListItems.SelectedValue = sdtItem;
                                    }
                                }
                                combo.ListItems.RemoveAt(1);
                            }
                        }
                    }
                }

            }
            document.SaveToFile("result.docx", FileFormat.Docx2013);
            System.Diagnostics.Process.Start("result.docx");
        }
    }
}
[VB.NET]
Dim document As New Document()
document.LoadFromFile("test.docx")
For Each section As Section In document.Sections
	For Each body As Body In section.ChildObjects
		For Each bodyObj As DocumentObject In body.ChildObjects
			If TypeOf bodyObj Is StructureDocumentTag Then
				If TryCast(bodyObj, StructureDocumentTag).SDTProperties.SDTType = SdtType.ComboBox Then
					Dim combo As SdtComboBox = TryCast(TryCast(bodyObj, StructureDocumentTag).SDTProperties.ControlProperties, SdtComboBox)
					Dim item As New SdtListItem("D", "d")
					combo.ListItems.Add(item)
					For Each sdtItem As SdtListItem In combo.ListItems
						If String.CompareOrdinal(sdtItem.Value, "d") = 0 Then
							combo.ListItems.SelectedValue = sdtItem
						End If
					Next
					combo.ListItems.RemoveAt(1)
				End If
			End If
		Next

	Next
Next
document.SaveToFile("result.docx", FileFormat.Docx2013)
System.Diagnostics.Process.Start("result.docx")