📌  相关文章
📜  C#中的ComboBox

📅  最后修改于: 2021-05-29 16:26:14             🧑  作者: Mango

在Windows窗体中,ComboBox在单个控件中提供了两种不同的功能,这意味着ComboBox既可以用作TextBox也可以用作ListBox。在ComboBox中,一次仅显示一个项目,其余项目显示在下拉菜单中。 ComboBox是C#中的一个类,在System.Windows.Forms命名空间下定义。您可以使用两种不同的方式创建ComboBox:

1.设计时:使用以下步骤创建ComboBox控件是最简单的方法:

  • 第1步:创建一个Windows窗体,如下图所示:
    Visual Studio->文件->新建->项目-> WindowsFormApp
  • 步骤2:从工具箱中拖动ComboBox控件,并将其放在Windows窗体上。您可以根据需要将ComboBox控件放置在Windows窗体上的任何位置。
  • 步骤3:拖放之后,您将转到ComboBox控件的属性,以根据需要设置ComboBox的属性。

    输出:

运行时:比上面的方法有些棘手。在此方法中,可以使用ComboBox类设置创建自己的ComboBox控件。创建动态ComboBox的步骤:

  • 步骤1:使用ComboBox类提供的ComboBox()构造函数创建一个组合框。
    // Creating combobox using ComboBox class
    ComboBox mybox = new ComboBox();
    
  • 步骤2:创建ComboBox之后,设置ComboBox类提供的ComboBox的属性。
    // Set the location of the ComboBox 
    mybox.Location = new Point(327, 77);
    
    // Set the size of the ComboBox
    mybox.Size = new Size(216, 26);
    
    // Add items in the ComboBox
    mybox.Items.Add("C#");
    mybox.Items.Add("Java");
    mybox.Items.Add("Scala");
    mybox.Items.Add("C");
    mybox.Items.Add("C++");
    
  • 步骤3:最后使用Add()方法将此ComboBox控件添加到窗体中。
    // Add this ComboBox to the form
    this.Controls.Add(mybox);
    

    例子:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
      
    namespace WindowsFormsApp18 {
      
    public partial class Form1 : Form {
      
        public Form1()
        {
            InitializeComponent();
        }
      
        private void Form1_Load(object sender, EventArgs e)
        {
            // Creating and setting the properties of label
            Label l = new Label();
            l.Location = new Point(122, 80);
            l.AutoSize = true;
            l.Text = "Select Programming Language";
      
            // Adding this label to the form
            this.Controls.Add(l);
      
            // Creating and setting the properties of comboBox
            ComboBox mybox = new ComboBox();
            mybox.Location = new Point(327, 77);
            mybox.Size = new Size(216, 26);
            mybox.Items.Add("C#");
            mybox.Items.Add("Java");
            mybox.Items.Add("Scala");
            mybox.Items.Add("C");
            mybox.Items.Add("C++");
      
            // Adding this ComboBox to the form
            this.Controls.Add(mybox);
        }
    }
    }
    

    输出:

组合框的重要属性

Property Description
BackColor This property is used to set the background color for the ComboBox control.
DropDownHeight This property is used to set the height in pixels of the drop-down portion of the ComboBox control.
DropDownStyle This property is used to set a value specifying the style of the ComboBox control.
DropDownWidth This property is used to set the width of the of the drop-down portion of a ComboBox control.
Font This property is used to set the font of the text displayed by the ComboBox control.
ForeColor This property is used to set the foreground color of the ComboBox control.
Height This property is used to set the height of the ComboBox control.
Items This property is used to get an object representing the collection of the items contained in this ComboBox control.
MaxDropDownItems This property is used to set the maximum number of items to be shown in the drop-down portion of the ComboBox control.
MaxLength This property is used to set the number of characters a user can type into the ComboBox control.
Name This property is used to set the name of the ComboBox control.
SelectedItem This property is used to set currently selected item in the ComboBox.
Size This property is used to set the height and width of the ComboBox control.
Sorted This property is used to set a value indicating whether the items in the combo box are sorted.
Text This property is used to set the text associated with this ComboBox control.
Visible This property is used to set a value indicating whether the control and all its child controls are displayed.

重要事件

Event Description
Click This event occur when the ComboBox control is clicked.
DragDrop This event occur when a drag-and-drop operation is completed.
DropDown This event occur when the drop-down portion of a ComboBox is shown.
DropDownClosed This event occur when the drop-down portion of the ComboBox is no longer visible.
DropDownStyleChanged This event occur when the DropDownStyle property has changed.
Leave This event occur when the input focus leaves the ComboBox control.
MouseClick This event occur when the ComboBox control is clicked by the mouse.
MouseDoubleClick This event occur when the ComboBox control is double clicked by the mouse.
MouseDown This event occur when the mouse pointer is over the ComboBox control and a mouse button is pressed.
MouseEnter This event occur when the mouse pointer enters the ComboBox control.
MouseHover This event occur when the mouse pointer rests on the ComboBox control.
SelectedIndexChanged This event occur when the SelectedIndex property has changed.