Export (0) Print
Expand All
2 out of 5 rated this helpful - Rate this topic

ComboBox.DropDownStyle Property

Gets or sets a value specifying the style of the combo box.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public ComboBoxStyle DropDownStyle { get; set; }
/** @property */
public ComboBoxStyle get_DropDownStyle ()

/** @property */
public void set_DropDownStyle (ComboBoxStyle value)

public function get DropDownStyle () : ComboBoxStyle

public function set DropDownStyle (value : ComboBoxStyle)

Not applicable.

Property Value

One of the ComboBoxStyle values. The default is DropDown.
Exception typeCondition

InvalidEnumArgumentException

The assigned value is not one of the ComboBoxStyle values.

The DropDownStyle property controls the interface that is presented to the user. You can enter a value that provides a simple drop-down list box, where the list always displays, a drop-down list box, where the text portion is not editable and you must select an arrow to view the drop-down, or the default drop-down list box, where the text portion is editable and the user must press the arrow key to view the list. To always display a list that the user cannot edit, use a ListBox control.

If you set the DropDownStyle property to DropDownList, you can select only valid values from the list. If you set the DropDownStyle property to DropDown, you can type any value in the editable area of the ComboBox. In this case, the Append value of AutoCompleteMode works the same as the SuggestAppend value of AutoCompleteMode.

The following code example demonstrates how to create an owner-drawn combo box by setting the DrawMode property to OwnerDrawnVariable and handling the DrawItem and MeasureItem events. It also demonstrates setting the DropDownWidth and DropDownStyle properties. To run the example, paste the following code in a form. Call the InitializeComboBox method in the form's constructor or Load event.


internal System.Windows.Forms.ComboBox ComboBox1;
private string[] animals;
  
// This method initializes the owner-drawn combo box.
// The drop-down width is set much wider than the size of the combo box
// to accomodate the large items in the list.  The drop-down style is set to 
// ComboBox.DropDown, which requires the user to click on the arrow to 
// see the list.
private void InitializeComboBox()
{
    this.ComboBox1 = new ComboBox();
    this.ComboBox1.DrawMode = 
        System.Windows.Forms.DrawMode.OwnerDrawVariable;
    this.ComboBox1.Location = new System.Drawing.Point(10, 20);
    this.ComboBox1.Name = "ComboBox1";
    this.ComboBox1.Size = new System.Drawing.Size(100, 120);
    this.ComboBox1.DropDownWidth = 250;
    this.ComboBox1.TabIndex = 0;
    this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown;
    animals = new string[]{"Elephant", "c r o c o d i l e", "lion"};
    ComboBox1.DataSource = animals;
    this.Controls.Add(this.ComboBox1);

    // Hook up the MeasureItem and DrawItem events
    this.ComboBox1.DrawItem += 
        new DrawItemEventHandler(ComboBox1_DrawItem);
    this.ComboBox1.MeasureItem += 
        new MeasureItemEventHandler(ComboBox1_MeasureItem);
}

// If you set the Draw property to DrawMode.OwnerDrawVariable, 
// you must handle the MeasureItem event. This event handler 
// will set the height and width of each item before it is drawn. 
private void ComboBox1_MeasureItem(object sender, 
    System.Windows.Forms.MeasureItemEventArgs e)
{

    switch(e.Index)
    {
        case 0:
            e.ItemHeight = 45;
            break;
        case 1:
            e.ItemHeight = 20;
            break;
        case 2:
            e.ItemHeight = 35;
            break;
    }
    e.ItemWidth = 260;

}

// You must handle the DrawItem event for owner-drawn combo boxes.  
// This event handler changes the color, size and font of an 
// item based on its position in the array.
private void ComboBox1_DrawItem(object sender, 
    System.Windows.Forms.DrawItemEventArgs e)
{

    float size = 0;
    System.Drawing.Font myFont;
    FontFamily family = null;

    System.Drawing.Color animalColor = new System.Drawing.Color();
    switch(e.Index)
    {
        case 0:
            size = 30;
            animalColor = System.Drawing.Color.Gray;
            family = FontFamily.GenericSansSerif;
            break;
        case 1:
            size = 10;
            animalColor = System.Drawing.Color.LawnGreen;
            family = FontFamily.GenericMonospace;
            break;
        case 2:
            size = 15;
            animalColor = System.Drawing.Color.Tan;
            family = FontFamily.GenericSansSerif;
            break;
    }

    // Draw the background of the item.
    e.DrawBackground();

    // Create a square filled with the animals color. Vary the size
    // of the rectangle based on the length of the animals name.
    Rectangle rectangle = new Rectangle(2, e.Bounds.Top+2, 
            e.Bounds.Height, e.Bounds.Height-4);
    e.Graphics.FillRectangle(new SolidBrush(animalColor), rectangle);

    // Draw each string in the array, using a different size, color,
    // and font for each item.
    myFont = new Font(family, size, FontStyle.Bold);
    e.Graphics.DrawString(animals[e.Index], myFont, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X+rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));

    // Draw the focus rectangle if the mouse hovers over an item.
    e.DrawFocusRectangle();
}


System.Windows.Forms.ComboBox comboBox1;

private String animals[];

// This method initializes the owner-drawn combo box.
// The drop-down width is set much wider than the size of the combo box
// to accomodate the large items in the list.  The drop-down style is set to 
// ComboBox.DropDown, which requires the user to click on the arrow to 
// see the list.
private void InitializeComboBox()
{
    this.comboBox1 = new ComboBox();
    this.comboBox1.set_DrawMode(
        System.Windows.Forms.DrawMode.OwnerDrawVariable);
    this.comboBox1.set_Location(new System.Drawing.Point(10, 20));
    this.comboBox1.set_Name("comboBox1");
    this.comboBox1.set_Size(new System.Drawing.Size(100, 120));
    this.comboBox1.set_DropDownWidth(250);
    this.comboBox1.set_TabIndex(0);
    this.comboBox1.set_DropDownStyle(ComboBoxStyle.DropDown);
    animals = new String[] { "Elephant", "c r o c o d i l e", "lion" };
    comboBox1.set_DataSource(animals);
    this.get_Controls().Add(this.comboBox1);
    // Hook up the MeasureItem and DrawItem events
    this.comboBox1.add_DrawItem(
        new DrawItemEventHandler(comboBox1_DrawItem));
    this.comboBox1.add_MeasureItem(
        new MeasureItemEventHandler(comboBox1_MeasureItem));
} //InitializeComboBox

// If you set the Draw property to DrawMode.OwnerDrawVariable, 
// you must handle the MeasureItem event. This event handler 
// will set the height and width of each item before it is drawn. 
private void comboBox1_MeasureItem(Object sender, 
    System.Windows.Forms.MeasureItemEventArgs e)
{
    switch (e.get_Index()) {
        case 0:
            e.set_ItemHeight(45);
            break;

        case 1:
            e.set_ItemHeight(20);
            break;

        case 2:
            e.set_ItemHeight(35);
            break;
    }
    e.set_ItemWidth(260);
} //comboBox1_MeasureItem

// You must handle the DrawItem event for owner-drawn combo boxes.  
// This event handler changes the color, size and font of an 
// item based on its position in the array.
private void comboBox1_DrawItem(Object sender, 
    System.Windows.Forms.DrawItemEventArgs e)
{
    float size = 0;
    System.Drawing.Font myFont;
    FontFamily family = null;

    System.Drawing.Color animalColor = new System.Drawing.Color();
    switch (e.get_Index()) {
        case 0:
            size = 30;
            animalColor = System.Drawing.Color.get_Gray();
            family = FontFamily.get_GenericSansSerif();
            break;

        case 1:
            size = 10;
            animalColor = System.Drawing.Color.get_LawnGreen();
            family = FontFamily.get_GenericMonospace();
            break;

        case 2:
            size = 15;
            animalColor = System.Drawing.Color.get_Tan();
            family = FontFamily.get_GenericSansSerif();
            break;
    }
    // Draw the background of the item.
    e.DrawBackground();
    // Create a square filled with the animals color. Vary the size
    // of the rectangle based on the length of the animals name.
    Rectangle rectangle = new Rectangle(2, e.get_Bounds().get_Top() + 2, 
        e.get_Bounds().get_Height(),
        e.get_Bounds().get_Height() - 4);
    e.get_Graphics().FillRectangle(new SolidBrush(animalColor), rectangle);
    // Draw each string in the array, using a different size, color,
    // and font for each item.
    myFont = new Font(family, size, FontStyle.Bold);
    e.get_Graphics().DrawString((String)animals.get_Item(e.get_Index()), 
        myFont, System.Drawing.Brushes.get_Black(), 
        new RectangleF(e.get_Bounds().get_X() + rectangle.get_Width(),
        e.get_Bounds().get_Y(), e.get_Bounds().get_Width(),
        e.get_Bounds().get_Height()));
    // Draw the focus rectangle if the mouse hovers over an item.
    e.DrawFocusRectangle();
} //comboBox1_DrawItem

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.