Getting Started with Windows Forms ComboBoxBase

18 Nov 201813 minutes to read

ComboBoxBase is a base control that hosts a separate ListControl (typically a ListBox) as its drop-down portion. This section explains how to get started with the WinForms ComboBoxBase control and its basic features.

Assembly Deployment

Refer to the Control Dependencies section for the list of assemblies and NuGet packages required to use the ComboBoxBase control.

Refer to NuGet Packages to learn how to install NuGet packages in a Windows Forms application.

To install the required NuGet package using the NuGet Package Manager Console, run the following command:

Install-Package Syncfusion.Shared.Base

Adding ComboBoxBase via Designer

  1. Add the Syncfusion.Shared.Base assembly reference to the project.

  2. Drag the ComboBoxBase control from the Toolbox onto the Windows Form designer.

    Drag and Drop ComboBoxBase control from the Toolbox

    ComboBoxBase in form

  3. To provide data for the drop-down list, drag a ListBox control (or another ListControl-derived control) onto the form.

    Add a ListBox control

  4. Select the ComboBoxBase control, and in the Properties window, set the ListControl property to the ListBox control that you added.

    Assign the ListBox as the ListControl for ComboBoxBase in the Properties window

Adding ComboBoxBase via Code

To add the ComboBoxBase control programmatically, follow these steps:

  1. Add the Syncfusion.Shared.Base assembly reference to the project and include the required namespace in the code.

    using Syncfusion.Windows.Forms.Tools;
    Imports Syncfusion.Windows.Forms.Tools
  2. Create instances of the ComboBoxBase and ListBox controls.

    private Syncfusion.Windows.Forms.Tools.ComboBoxBase comboBoxBase1;
        private System.Windows.Forms.ListBox listBox1;
       
        this.comboBoxBase1 = new Syncfusion.Windows.Forms.Tools.ComboBoxBase();
        this.listBox1 = new ListBox();
    Private comboBoxBase1 As Syncfusion.Windows.Forms.Tools.ComboBoxBase
        Private listBox1 As System.Windows.Forms.ListBox
       
        Me.comboBoxBase1 = New Syncfusion.Windows.Forms.Tools.ComboBoxBase()
        Me.listBox1 = New ListBox()
  3. Assign the ListBox control to the ListControl property of the ComboBoxBase control and set the required size.

    this.comboBoxBase1.ListControl = this.listBox1;
        this.comboBoxBase1.Size = new Size(120, 20);
    Me.comboBoxBase1.ListControl = Me.listBox1
        Me.comboBoxBase1.Size = New Size(120, 20)
  4. Specify the data source for the ListBox control.

    // Sets the data source.
        ArrayList USStates = new ArrayList();
        USStates.Add(new USState("Washington", "WA"));
        USStates.Add(new USState("West Virginia", "WV"));
        USStates.Add(new USState("Wisconsin", "WI"));
        USStates.Add(new USState("Wyoming", "WY"));
        this.listBox1.DataSource = USStates;
    ' Sets the data source.
        Dim USStates As ArrayList = New ArrayList()
        USStates.Add(New USState("Washington", "WA"))
        USStates.Add(New USState("West Virginia", "WV"))
        USStates.Add(New USState("Wisconsin", "WI"))
        USStates.Add(New USState("Wyoming", "WY"))
        Me.listBox1.DataSource = USStates
  5. Add the ComboBoxBase and ListBox controls to the form.

    this.Controls.Add(this.listBox1);
        this.Controls.Add(this.comboBoxBase1);
    Me.Controls.Add(Me.listBox1)
        Me.Controls.Add(Me.comboBoxBase1)

    ComboBoxBase and ListBox displayed on the form

Complete Code Example

The following example combines all the preceding steps into a single runnable application.

using Syncfusion.Windows.Forms.Tools;
   using System.Collections;
   using System.Drawing;
   
   public partial class Form1 : Form
   {
       private Syncfusion.Windows.Forms.Tools.ComboBoxBase comboBoxBase1;
       private System.Windows.Forms.ListBox listBox1;
   
       public Form1()
       {
           InitializeComponent();
   
           this.comboBoxBase1 = new Syncfusion.Windows.Forms.Tools.ComboBoxBase();
           this.listBox1 = new ListBox();
   
           this.comboBoxBase1.Location = new Point(20, 20);
           this.comboBoxBase1.Size = new Size(120, 20);
           this.listBox1.DisplayMember = "LongName";
           this.listBox1.ValueMember = "Abbreviation";
   
           // Sets the data source.
           ArrayList USStates = new ArrayList();
           USStates.Add(new USState("Washington", "WA"));
           USStates.Add(new USState("West Virginia", "WV"));
           USStates.Add(new USState("Wisconsin", "WI"));
           USStates.Add(new USState("Wyoming", "WY"));
           this.listBox1.DataSource = USStates;
   
           this.comboBoxBase1.ListControl = this.listBox1;
   
           this.Controls.Add(this.listBox1);
           this.Controls.Add(this.comboBoxBase1);
       }
   }
   
   // USState class
   public class USState
   {
       public string LongName { get; set; }
       public string Abbreviation { get; set; }
   
       public USState(string longName, string abbreviation)
       {
           this.LongName = longName;
           this.Abbreviation = abbreviation;
       }
   }
Imports Syncfusion.Windows.Forms.Tools
   Imports System.Collections
   Imports System.Drawing
   
   Public Partial Class Form1
       Inherits System.Windows.Forms.Form
   
       Private comboBoxBase1 As Syncfusion.Windows.Forms.Tools.ComboBoxBase
       Private listBox1 As System.Windows.Forms.ListBox
   
       Public Sub New()
           InitializeComponent()
   
           Me.comboBoxBase1 = New Syncfusion.Windows.Forms.Tools.ComboBoxBase()
           Me.listBox1 = New ListBox()
   
           Me.comboBoxBase1.Location = New Point(20, 20)
           Me.comboBoxBase1.Size = New Size(120, 20)
           Me.listBox1.DisplayMember = "LongName"
           Me.listBox1.ValueMember = "Abbreviation"
   
           ' Sets the data source.
           Dim USStates As ArrayList = New ArrayList()
           USStates.Add(New USState("Washington", "WA"))
           USStates.Add(New USState("West Virginia", "WV"))
           USStates.Add(New USState("Wisconsin", "WI"))
           USStates.Add(New USState("Wyoming", "WY"))
           Me.listBox1.DataSource = USStates
   
           Me.comboBoxBase1.ListControl = Me.listBox1
   
           Me.Controls.Add(Me.listBox1)
           Me.Controls.Add(Me.comboBoxBase1)
       End Sub
   End Class
   
   ' USState class
   Public Class USState
       Public Property LongName As String
       Public Property Abbreviation As String
   
       Public Sub New(ByVal longName As String, ByVal abbreviation As String)
           Me.LongName = longName
           Me.Abbreviation = abbreviation
       End Sub
   End Class

For more information, refer to the Creating ListControl-Derived Controls topic.