C# List Selection Control ComboBox

    ComboBox is a combination of text box and list box, which is more flexible and commonly used

    1. Drop-down style

    The DropDownStyle property is the most important property and is used to specify the style of the control.

public partial class ComboBox : Form
    {
        public ComboBox()
        {
            InitializeComponent();
            addItems();
            SetComboBoxDropDownStyle();
        }

        private void addItems()
        {
            this.comboBox1.Items.Add("Item1");
            this.comboBox1.Items.Add("Item2");
            this.comboBox1.Items.Add("Item3");
        }
    }

    Common styles are

    Simple users can only enter text, not drop-down selections

        private void SetComboBoxDropDownStyle()
        {
            //Set the drop-down style to a simple text box
            this.comboBox1.DropDownStyle = ComboBoxStyle.Simple;
        }

    DropDownList User can only select using drop down list

private void SetComboBoxDropDownStyle()
        {
            //Set the drop-down style to only list selection
            this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
        }

    DropDown users can enter text or select a list, combining the above two situations

private void SetComboBoxDropDownStyle()
        {
            //Set the drop-down style to a combination of text and list
          this.comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
        }


2. Important attributes

    2.1 DroppedDown Indicates whether to display the drop-down list while the program is running

private void SetComboBoxDropDownStyle()
        {
            //Set the dropdown style
            this.comboBox1.DropDownStyle = ComboBoxStyle.DropDown;//This is to display the specified
            this.comboBox1.DroppedDown = true;
        }

    2.2 DropDownHeight Indicates the height of the drop-down list, trim when the height of the drop-down list is greater than the sum of the heights of the items

public partial class ComboBox : Form
    {
        public ComboBox()
        {
            InitializeComponent();
            addItems();
            SetDroppedDownHeight();
        }

        private void addItems()
        {
            this.comboBox1.Items.Add("Item1");
            this.comboBox1.Items.Add("Item2");
            this.comboBox1.Items.Add("Item3");
        }

        private void SetDroppedDownHeight()
        {
            this.comboBox1.DropDownHeight = 200;//Set the drop-down height
        }
    }

    2.3 DropDownWidth indicates the width of the control, the default width of the control is equal to the width of the drop-down

public partial class ComboBox : Form
    {
        public ComboBox()
        {
            InitializeComponent();
            addItems();
            SetDroppedDownWidth();
        }

        private void addItems()
        {
            this.comboBox1.Items.Add("Item1");
            this.comboBox1.Items.Add("Item2");
            this.comboBox1.Items.Add("Item3");
        }

        private void SetDroppedDownWidth()
        {
            this.comboBox1.DropDownWidth = 200;//Set the drop-down height
        }
    }

    2.4 MaxDropDwnItems Indicates the maximum number of items that can be displayed in the drop-down list. When 2.2 and 2.4 conflict, the 2.4 property shall prevail

public partial class ComboBox : Form
    {
        public ComboBox()
        {
            InitializeComponent();
            addItems();
            SetMaxDropDownItems();
        }

        private void addItems()
        {
            this.comboBox1.Items.Add("Item1");
            this.comboBox1.Items.Add("Item2");
            this.comboBox1.Items.Add("Item3");
        }

        private void SetMaxDropDownItems()
        {
            this.comboBox1.MaxDropDownItems = 2;//Maximum display 2 items
        }
    }


3. Automatic completion (also suitable for TextBox)

    One of the data sources for AutoCompleteCustomSource to match, similar to Items

    AutoCompleteSource is an enumeration value indicating what data source to use for completion

    AutoCompleteMode indicates how the data appears during completion

public partial class ComboBox : Form
    {
        public ComboBox()
        {
            InitializeComponent();
            addItems();
            SetAutoCompleteCustomSource();
        }

        private void addItems()
        {
            this.comboBox1.Items.Add("Item1");
            this.comboBox1.Items.Add("Item2");
            this.comboBox1.Items.Add("Item3");
        }

        private void SetAutoCompleteCustomSource()
        {
            this.comboBox1.AutoCompleteCustomSource.
                AddRange(new string[]{"CustomItem1","CustomItem2",
                    "CustomItem3","CustomItem4","CustomItem5",
                    "CustomItem6"});
            this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems; //Complete according to the drop-down list
            this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;//It's just that the suggestion is not automatically completed
        }

    For the second enumeration value there is FileSystem, complete the file system as the data source


    FileSystemDirectories, using directories in the file system as data sources

    RecentlyUsedList recently visited URL completion

    HistoryList,AllUrl,AllSystemSources

    CustomSource uses the first property above as the data source

    For three, there is also SuggestAppend auto-completion and prompt, Append auto-completion


4. Events

    4.1 DropDown Triggered when the drop-down list is expanded

public partial class ComboBox : Form
    {
        public ComboBox()
        {
            InitializeComponent();
            addItems();
        }

        private void addItems()
        {
            this.comboBox1.Items.Add("Item1");
            this.comboBox1.Items.Add("Item2");
            this.comboBox1.Items.Add("Item3");
        }

        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            MessageBox.Show("DropDown event is triggered");
        }

        private void comboBox1_DropDownClosed(object sender, EventArgs e)
        {
            MessageBox.Show("DropDownClosed event is triggered");
        }
    }

        Add to the InitializeComponent method in Designer.cs

this.comboBox1.DropDown+=new System.EventHandler(this.comboBox1_DropDown);
this.comboBox1.DropDownClosed+=new System.EventHandler(this.comboBox1_DropDownClosed);

        When expanding the list

        when closing the list


    4.2 SelectedIndexChanged event, triggered when an item is selected

private void InitializeComponent()
        {
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            //
            // comboBox1
            //
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Location = new System.Drawing.Point(14, 50);
            this.comboBox1.MaxDropDownItems = 5;
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(100, 20);
            this.comboBox1.TabIndex = 0;
            this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            //
            // textBox1
            //
            this.textBox1.Location = new System.Drawing.Point(15, 14);
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(97, 21);
            this.textBox1.TabIndex = 1;
            //
            // ComboBox
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.comboBox1);
            this.Name = "ComboBox";
            this.Text = "ComboBox";
            this.ResumeLayout(false);
            this.PerformLayout();
        }
public partial class ComboBox : Form
    {
        public ComboBox()
        {
            InitializeComponent();
            addItems();
        }

        private void addItems()
        {
            for (int i = 0; i < 100;i++ )
            {
                for (int j = 0; j < i; j++)
                {
                    this.comboBox1.Items.Add("item" + j.ToString());
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            System.Windows.Forms.ComboBox comboBox = (System.Windows.Forms.ComboBox)sender;
            string selectedItem = (string)comboBox.SelectedItem;
            int count = 0;
            int resultIndex = -1;
            resultIndex = comboBox.FindStringExact(selectedItem); //same method as listbox
            while (resultIndex!=-1)
            {
                comboBox.Items.RemoveAt(resultIndex);//Remove
                count += 1;
                resultIndex = comboBox.FindStringExact(selectedItem, resultIndex);
            }
            this.textBox1.Text = selectedItem + "total" + count + "items";
        }
    }
The result after item7 is selected


    4.3 DrawItem event, triggered when drawing an item.

    4.4 The MeasureItem event is triggered when describing the options in the DrawItem control, such as height changes

public partial class ComboBox : Form
    {
        public ComboBox()
        {
            InitializeComponent();
            addItems();
        }
        private string[] colors;
        private void addItems()
        {
            colors = new string[] { "红", "黄", "蓝" };
            for (int i = 0; i < colors.Length;i++ )
            {
                this.comboBox1.Items.Add(colors[i]);
            }
        }

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            float size = 0;
            System.Drawing.Font font;
            FontFamily family = null;
            System.Drawing.Color color = new System.Drawing.Color();
            switch (e.Index) //The first item is being drawn
            {
                case 0:
                    size = 35;
                    color = System.Drawing.Color.Red;
                    family = FontFamily.GenericMonospace;
                    break;
                case 1:
                    size = 25;
                    color = System.Drawing.Color.Yellow;
                    family = FontFamily.GenericMonospace;
                    break;
                case 2:
                    size = 15;
                    color = System.Drawing.Color.Blue;
                    family = FontFamily.GenericMonospace;
                    break;
            }
            e.DrawBackground();//Draw background
            Rectangle rectangle = new Rectangle(0, e.Bounds.Top, e.Bounds.Height, e.Bounds.Height);//Here e.Bounds.Height is provided by the MeasureItem event
            e.Graphics.FillRectangle(new SolidBrush(color), rectangle);//Draw the left rectangle
            //Draw the drop-down box font
            font = new Font(family, size, FontStyle.Regular);//Normal font style
            e.Graphics.DrawString(colors[e.Index], font, System.Drawing.Brushes.Black,
                new RectangleF(e.Bounds.X+rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));//Draw text, where width is provided by MeasureItem
            e.DrawFocusRectangle();//Draw each child*
        }

        private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            switch (e.Index)
            {
                case 0:
                    e.ItemHeight = 40;
                    break;
                case 1:
                    e.ItemHeight = 30;
                    break;
                case 2:
                    e.ItemHeight = 20;
                    break;
            }
            e.ItemWidth=100;
        }
    }
private void InitializeComponent()
        {
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
            //
            // comboBox1
            //
            this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
            this.comboBox1.DropDownHeight = 300;
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.IntegralHeight = false;
            this.comboBox1.Location = new System.Drawing.Point(10, 20);
            this.comboBox1.MaxDropDownItems = 5;//Display up to 5 items
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(140, 22);
            this.comboBox1.TabIndex = 0;
            this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);//Register two events
            this.comboBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.comboBox1_MeasureItem);
            //
            // ComboBox
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.comboBox1);
            this.Name = "ComboBox";
            this.Text = "ComboBox";
            this.ResumeLayout(false);

        }



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325658924&siteId=291194637
Recommended