Windows 组合框控件

ComboBox

表示 Windows 组合框控件。ComboBox 显示与一个 ListBox 组合的编辑字段,使用户可以从列表中选择或输入新文本。ComboBox 的默认行为是显示一个编辑字段,该字段附带一个隐藏的下拉列表。DropDownStyle 属性确定要显示的组合框的样式。您可以输入一个值,该值指示允许以下情况:简单的下拉列表(始终显示列表)、下拉列表框(文本部分不可编辑,并且必须选择一个箭头才能查看下拉列表框)或默认下拉列表框(文本部分可编辑,并且用户必须按箭头键才能查看列表)。若要始终显示用户不能编辑的列表,请使用 ListBox 控件。

若要在运行时向列表添加对象,请用 AddRange 方法分配一个对象引用数组。然后,列表显示每个对象的默认字符串值。可以用 Add 方法添加单个对象。

除显示和选择功能外,ComboBox 还提供一些功能使您能够有效地向 ComboBox 添加项以及在列表项内查找文本。BeginUpdate EndUpdate 方法使您能够向 ComboBox 添加大量项而不必在每次向列表添加项时重新绘制控件。FindString FindStringExact 方法使您能够在列表中搜索包含特定搜索字符串的项。

可以使用这些属性管理列表中当前选定的项,使用 Text 属性指定编辑字段中显示的字符串,使用 SelectedIndex 属性获取或设置当前项,以及使用 SelectedItem 属性获取或设置对对象的引用。

AddRange 方法  ComboBox 的项列表添加项的数组。

如果 ComboBox Sorted 属性设置为真,则按字母顺序将项插入到列表中。否则按项在数组中出现的顺序插入项。通常向此方法传递 String 对象的数组,但可以向此方法传递任何类型的对象的数组。当向集合添加一个对象时,该方法调用此对象的 ToString 方法来获得要在列表中显示的字符串。使用此方法向集合添加项时,不需要调用 BeginUpdate EndUpdate 方法来优化性能。

ComboBox.BeginUpdate 方法

当将多项一次一项地添加到 ComboBox 时维持性能。

在调用 EndUpdate 方法前,该方法会阻止控件绘制。

ComboBox 添加项的首选方法是使用 ComboBox.ObjectCollection 类的 AddRange 方法(通过使用 ComboBox Items 属性)。这使您可以一次向列表添加一组项。但是,如果想使用 ComboBox.ObjectCollection 类的 Add 方法一次添加一个项时,则可以使用 BeginUpdate 方法,以防止每次向列表添加项时控件都重新绘制 ComboBox。完成向列表添加项的任务后,调用 EndUpdate 方法来启用 ComboBox 进行重新绘制。当向列表添加大量的项时,使用这种方法添加项可以防止绘制 ComboBox 时闪烁

ComboBox.SelectedIndexChanged 事件

SelectedIndex 属性更改后发生。

事件数据

事件处理程序接收一个 EventArgs 类型的参数。

备注

可以为该事件创建事件处理程序,以确定 ComboBox 中选定的索引是否已更改。这在需要根据 ComboBox 中的当前选定内容显示其他控件中的信息时非常有用。可以使用该事件的事件处理程序来加载其他控件中的信息。

示例

[Visual Basic, C#, C++] 下面的示例是个完整的应用程序,阐释可如何使用 Add 方法向 ComboBox 添加项,如何使用 FindString 方法在 ComboBox 中查找项,以及如何使用 BeginUpdate EndUpdate 方法以高效的方式向 ComboBox 添加大量项。

[C#] 

using System;

using System.Windows.Forms;

 

namespace Win32Form1Namespace {

    

    

    public class Win32Form1 : System.Windows.Forms.Form {

        private System.Windows.Forms.Button addButton;

        private System.Windows.Forms.TextBox textBox2;

        private System.Windows.Forms.Button addGrandButton;

        private System.Windows.Forms.ComboBox comboBox1;

        private System.Windows.Forms.Button showSelectedButton;

        private System.Windows.Forms.TextBox textBox1;

        private System.Windows.Forms.Button findButton;

        private System.Windows.Forms.Label label1;

        

        public Win32Form1() {

            this.InitializeComponent();

        }

        

        [System.STAThreadAttribute()]

        public static void 
    
    
     
     Main
    
    () {

            System.Windows.Forms.Application.Run(new Win32Form1());

        }

        

        private void InitializeComponent() {

            this.addButton = new System.Windows.Forms.Button();

            this.textBox2 = new System.Windows.Forms.TextBox();

            this.addGrandButton = new System.Windows.Forms.Button();

            this.comboBox1 = new System.Windows.Forms.ComboBox();

            this.showSelectedButton = new System.Windows.Forms.Button();

            this.textBox1 = new System.Windows.Forms.TextBox();

            this.findButton = new System.Windows.Forms.Button();

            this.label1 = new System.Windows.Forms.Label();

            this.addButton.Location = new System.Drawing.Point(248, 32);

            this.addButton.Size = new System.Drawing.Size(40, 24);

            this.addButton.TabIndex = 1;

            this.addButton.Text = "Add";

            this.addButton.Click += new System.EventHandler(this.addButton_Click);

            this.textBox2.Location = new System.Drawing.Point(8, 64);

            this.textBox2.Size = new System.Drawing.Size(232, 20);

            this.textBox2.TabIndex = 6;

            this.textBox2.Text = "";

            this.addGrandButton.Location = new System.Drawing.Point(8, 96);

            this.addGrandButton.Size = new System.Drawing.Size(280, 23);

            this.addGrandButton.TabIndex = 2;

            this.addGrandButton.Text = "Add 1,000 Items";

            this.addGrandButton.Click += new System.EventHandler(this.addGrandButton_Click);

            this.comboBox1.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 

                        | System.Windows.Forms.AnchorStyles.Right);

            this.comboBox1.DropDownWidth = 280;

            this.comboBox1.Items.AddRange(new object[] {"Item 1",

                        "Item 2",

                        "Item 3",

                        "Item 4",

                        "Item 5"});

            this.comboBox1.Location = new System.Drawing.Point(8, 248);

            this.comboBox1.Size = new System.Drawing.Size(280, 21);

            this.comboBox1.TabIndex = 7;

            this.showSelectedButton.Location = new System.Drawing.Point(8, 128);

            this.showSelectedButton.Size = new System.Drawing.Size(280, 24);

            this.showSelectedButton.TabIndex = 4;

            this.showSelectedButton.Text = "What Item is Selected?";

            this.showSelectedButton.Click += new System.EventHandler(this.showSelectedButton_Click);

            this.textBox1.Location = new System.Drawing.Point(8, 32);

            this.textBox1.Size = new System.Drawing.Size(232, 20);

            this.textBox1.TabIndex = 5;

            this.textBox1.Text = "";

            this.findButton.Location = new System.Drawing.Point(248, 64);

            this.findButton.Size = new System.Drawing.Size(40, 24);

            this.findButton.TabIndex = 3;

            this.findButton.Text = "Find";

            this.findButton.Click += new System.EventHandler(this.findButton_Click);

            this.label1.Location = new System.Drawing.Point(8, 224);

            this.label1.Size = new System.Drawing.Size(144, 23);

            this.label1.TabIndex = 0;

            this.label1.Text = "Test ComboBox";

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

            this.ClientSize = new System.Drawing.Size(292, 273);

            this.Controls.AddRange(new System.Windows.Forms.Control[] {this.comboBox1,

                        this.textBox2,

                        this.textBox1,

                        this.showSelectedButton,

                        this.findButton,

                        this.addGrandButton,

                        this.addButton,

                        this.label1});

            this.Text = "ComboBox Sample";

        }

        

        private void addButton_Click(object sender, System.EventArgs e) {

           comboBox1.Items.Add(textBox1.Text);

        }

 

        private void addGrandButton_Click(object sender, System.EventArgs e) {

            comboBox1.BeginUpdate();

            for (int i = 0; i < 1000; i++) {

                comboBox1.Items.Add("Item 1" + i.ToString());

            }

            comboBox1.EndUpdate();

        }

 

        private void findButton_Click(object sender, System.EventArgs e) {

            int index = comboBox1.FindString(textBox2.Text);

            comboBox1.SelectedIndex = index;

        }

 

        private void showSelectedButton_Click(object sender, System.EventArgs e) {

            int selectedIndex = comboBox1.SelectedIndex;

            Object selectedItem = comboBox1.SelectedItem;

 

            MessageBox.Show("Selected Item Text: " + selectedItem.ToString() + "/n" +

                            "Index: " + selectedIndex.ToString());

        }

    }

}

 

以下代码示例阐释了如何初始化 ComboBox 控件,具体方法为设置 MaxDropDownItems DropDownStyle 属性以及使用 AddRange 方法填充 ComboBox。它还说明如何处理 SelectedIndexChanged 事件。要运行该示例,请将以下代码粘贴到一个窗体中(该窗体包含名为 TextBox1 TextBox),然后从该窗体的构造函数或 Load 方法中调用 InitializeComboBox 方法。

SampleID='System.Windows.Forms.ComboBoxFindString' SnippetID='1,2']

--------- Languages displayed= cs, vb ---------

--------- cs ---------

--------- Snippet 1 ---------

    // Declare comboBox1 as a ComboBox.

    internal System.Windows.Forms.ComboBox ComboBox1;

    

    // This method initializes the combo box, adding a large string array

    // but limiting the drop-down size to six rows so the combo box doesn't 

    // cover other controls when it expands.

    private void InitializeComboBox()

    {

        this.ComboBox1 = new System.Windows.Forms.ComboBox();

        string[] employees = new string[]{"Hamilton, David", "Hensien, Kari",

                "Hammond, Maria", "Harris, Keith", "Henshaw, Jeff D.", 

                "Hanson, Mark", "Harnpadoungsataya, Sariya", 

                "Harrington, Mark", "Harris, Keith", "Hartwig, Doris", 

                "Harui, Roger", "Hassall, Mark", "Hasselberg, Jonas", 

                "Harnpadoungsataya, Sariya", "Henshaw, Jeff D.", 

                "Henshaw, Jeff D.", "Hensien, Kari", "Harris, Keith", 

                "Henshaw, Jeff D.", "Hensien, Kari", "Hasselberg, Jonas",

                "Harrington, Mark", "Hedlund, Magnus", "Hay, Jeff", 

                "Heidepriem, Brandon D."};

        ComboBox1.Items.AddRange(employees);

        this.ComboBox1.Location = new System.Drawing.Point(136, 32);

        this.ComboBox1.MaxDropDownItems = 5;

        this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

        this.ComboBox1.Name = "ComboBox1";

        this.ComboBox1.Size = new System.Drawing.Size(136, 81);

        this.ComboBox1.TabIndex = 0;

        this.Controls.Add(this.ComboBox1);

        

        // Associate the event-handling method with the 

        // SelectedIndexChanged event.

        this.ComboBox1.SelectedIndexChanged += 

            new System.EventHandler(ComboBox1_SelectedIndexChanged);

    }

--------- Snippet 2 ---------

    // This method is called when the user changes his or her selection.

    // It searches for all occurrences of the selected employee's

    // name in the Items array and adds the employee's name and 

    // the number of occurrences to TextBox1.Text.

    // CAUTION   This code exposes a known bug: If the index passed to the 

    // FindStringExact(searchString, index) method is the last index 

    // of the array, the code throws an exception.

    private void ComboBox1_SelectedIndexChanged(object sender, 

        System.EventArgs e)

    {

        ComboBox comboBox = (ComboBox) sender;

        // Save the selected employee's name, because we will remove

        // the employee's name from the list.

        string selectedEmployee = (string) ComboBox1.SelectedItem;

        int count = 0;

        int resultIndex = -1;

        // Call the FindStringExact method to find the first 

        // occurrence in the list.

        resultIndex = ComboBox1.FindStringExact(selectedEmployee);

        // Remove the name as it is found, and increment the found count. 

        // Then call the FindStringExact method again, passing in the 

        // index of the current found item so the search starts there 

        // instead of at the beginning of the list.

        while (resultIndex!=-1)

        {

            ComboBox1.Items.RemoveAt(resultIndex);

            count += 1;

            resultIndex = ComboBox1.FindStringExact(selectedEmployee, 

                resultIndex);

        }

        // Update the text in Textbox1.

        TextBox1.Text = TextBox1.Text+ "/r/n" + selectedEmployee + ": "

            + count;

    }

ComboBox.FindString 方法

查找 ComboBox 中以指定字符串开始的第一个项。

重载列表

查找组合框中以指定字符串开始的第一个项。

[Visual Basic] Overloads Public Function FindString(String) As Integer

[C#] public int FindString(string);

[C++] public: int FindString(String*);

[JScript] public function FindString(String) : int;

在给定索引后查找以给定字符串开始的第一项。该搜索不区分大小写。

[Visual Basic] Overloads Public Function FindString(String, Integer) As Integer

[C#] public int FindString(string, int);

[C++] public: int FindString(String*, int);

[JScript] public function FindString(String, int) : int;

示例

[Visual Basic, C#, C++] 下面的示例阐释 FindString 方法和 SelectedIndex 属性的用法。该示例是 ComboBox 类概述中可运行代码示例的一部分。

[Visual Basic, C#, C++] 注意   此示例显示如何使用 FindString 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。

[Visual Basic]

Private Sub findButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

    Dim index As Integer

    index = comboBox1.FindString(textBox2.Text)

    comboBox1.SelectedIndex = index

End Sub

 

[C#]

private void findButton_Click(object sender, System.EventArgs e) {

    int index = comboBox1.FindString(textBox2.Text);

    comboBox1.SelectedIndex = index;

}

 

[C++]

private:

    void findButton_Click(Object* /*sender*/, System::EventArgs* /*e*/) {

        int index = comboBox1->FindString(textBox2->Text);

        comboBox1->SelectedIndex = index;

    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值