ComboBox.ObjectCollection.AddRange 方法

来源:百度文库 编辑:神马文学网 时间:2024/03/28 22:28:24
向 ComboBox 的项列表添加项的数组。
命名空间:  System.Windows.Forms
程序集:  System.Windows.Forms(在 System.Windows.Forms.dll 中)
语法
C#
C++
F#
JScript
复制
'声明Public Sub AddRange ( _items As Object() _)
'用法Dim instance As ComboBox..::.ObjectCollectionDim items As Object()instance.AddRange(items)
复制
public void AddRange(Object[] items)
参数
items
类型:System.Object()
要向列表添加的对象的数组。
异常
异常 条件
ArgumentNullException
items 参数中的一项为 Nothing。
备注
如果 ComboBox 的 Sorted 属性设置为真,则按字母顺序将项插入到列表中。否则按项在数组中出现的顺序插入项。通常向此方法传递 String 对象的数组,但可以向此方法传递任何类型的对象的数组。当向集合添加一个对象时,该方法调用此对象的 ToString 方法来获得要在列表中显示的字符串。使用此方法向集合添加项时,不需要调用 BeginUpdate 和 EndUpdate 方法来优化性能。
示例
下面的代码示例演示如何通过设置文本属性并使用 AddRange 方法填充 ComboBox 来初始化 ComboBox 控件。它还演示了如何处理 DropDown事件。若要运行该示例,请将下面的代码粘贴到一个窗体中,然后在该窗体的构造函数或 Load 方法中调用 InitializeComboBox 方法。
C#
C++
F#
JScript
复制
' Declare ComboBox1.Friend WithEvents ComboBox1 As System.Windows.Forms.ComboBox' Initialize ComboBox1.Private Sub InitializeComboBox()Me.ComboBox1 = New ComboBoxMe.ComboBox1.Location = New System.Drawing.Point(128, 48)Me.ComboBox1.Name = "ComboBox1"Me.ComboBox1.Size = New System.Drawing.Size(100, 21)Me.ComboBox1.TabIndex = 0Me.ComboBox1.Text = "Typical"Dim installs() As String = New String() _{"Typical", "Compact", "Custom"}ComboBox1.Items.AddRange(installs)Me.Controls.Add(Me.ComboBox1)End Sub' Handles the ComboBox1 DropDown event. If the user expands the ' drop-down box, a message box will appear, recommending the' typical installation.Private Sub ComboBox1_DropDown _(ByVal sender As Object, ByVal e As System.EventArgs) _Handles ComboBox1.DropDownMessageBox.Show("Typical installation is strongly recommended.", _"Install information", MessageBoxButtons.OK, MessageBoxIcon.Information)End Sub