DomainUpDown.DomainUpDownItemCollection.Add 方法

此方法支持 .NET 基础结构,但不适合在代码中直接使用。

将指定的对象添加到集合末尾。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Overrides Function Add ( _
    item As Object _
) As Integer
用法
Dim instance As DomainUpDownItemCollection
Dim item As Object
Dim returnValue As Integer

returnValue = instance.Add(item)
public override int Add (
    Object item
)
public:
virtual int Add (
    Object^ item
) override
public int Add (
    Object item
)
public override function Add (
    item : Object
) : int

参数

  • item
    要添加到集合末尾的 Object

返回值

添加到集合中的 Object 的从零开始的索引值。

备注

也可以使用 Insert 方法将新的 Object 添加到集合中。

若要移除先前添加的 Object,请使用 RemoveRemoveAt 方法。

示例

下面的示例创建并初始化一个 DomainUpDown 控件。该示例允许您设置它的部分属性并创建在 Up-Down 控件中显示的字符串集合。该代码假定已经在窗体上实例化一个 TextBox、一个 CheckBox 和一个 Button。示例还假定已将类级别上的一个成员变量声明为名为 myCounter 的 32 位有符号整数。可在文本框中输入一个字符串,并在单击按钮后将它添加到 Items 集合中。通过单击复选框,可以切换 Sorted 属性,并观察 up-down 控件中项集合的相应变化。

Protected domainUpDown1 As DomainUpDown


Private Sub InitializeMyDomainUpDown()
    ' Create and initialize the DomainUpDown control.
    domainUpDown1 = New DomainUpDown()
    
    ' Add the DomainUpDown control to the form.
    Controls.Add(domainUpDown1)
End Sub 'InitializeMyDomainUpDown


Private Sub button1_Click(sender As Object, e As EventArgs)
    ' Add the text box contents and initial location in the collection
    ' to the DomainUpDown control.
    domainUpDown1.Items.Add((textBox1.Text.Trim() & " - " & myCounter))
    
    ' Increment the counter variable.
    myCounter = myCounter + 1
    
    ' Clear the TextBox.
    textBox1.Text = ""
End Sub 'button1_Click


Private Sub checkBox1_Click(sender As Object, e As EventArgs)
    ' If Sorted is set to true, set it to false; 
    ' otherwise set it to true.
    If domainUpDown1.Sorted Then
        domainUpDown1.Sorted = False
    Else
        domainUpDown1.Sorted = True
    End If
End Sub 'checkBox1_Click


Private Sub domainUpDown1_SelectedItemChanged _
    (sender As Object, e As EventArgs)
    
    ' Display the SelectedIndex and 
    ' SelectedItem property values in a MessageBox.
    MessageBox.Show(("SelectedIndex: " & domainUpDown1.SelectedIndex.ToString() & _
        ControlChars.Cr & "SelectedItem: " & domainUpDown1.SelectedItem.ToString()))
End Sub 'domainUpDown1_SelectedItemChanged
protected DomainUpDown domainUpDown1;

private void InitializeMyDomainUpDown()
 {
    // Create and initialize the DomainUpDown control.
    domainUpDown1 = new DomainUpDown();
    
    // Add the DomainUpDown control to the form.
    Controls.Add(domainUpDown1);
 }
 
 private void button1_Click(Object sender, 
                           EventArgs e)
 {   
    // Add the text box contents and initial location in the collection
    // to the DomainUpDown control.
    domainUpDown1.Items.Add((textBox1.Text.Trim()) + " - " + myCounter);
    
    // Increment the counter variable.
    myCounter = myCounter + 1;
 
    // Clear the TextBox.
    textBox1.Text = "";
 }
 
 private void checkBox1_Click(Object sender, 
                             EventArgs e)
 {
    // If Sorted is set to true, set it to false; 
    // otherwise set it to true.
    if (domainUpDown1.Sorted)
    {
       domainUpDown1.Sorted = false;
    }
    else
    {
       domainUpDown1.Sorted = true;
    }
 }
 
 private void domainUpDown1_SelectedItemChanged(Object sender, 
                                               EventArgs e)
 {
    // Display the SelectedIndex and 
    // SelectedItem property values in a MessageBox.
    MessageBox.Show("SelectedIndex: " + domainUpDown1.SelectedIndex.ToString() 
       + "\n" + "SelectedItem: " + domainUpDown1.SelectedItem.ToString());
 }
protected:
   DomainUpDown^ domainUpDown1;

private:
   void InitializeMyDomainUpDown()
   {
      // Create and initialize the DomainUpDown control.
      domainUpDown1 = gcnew DomainUpDown;
      
      // Add the DomainUpDown control to the form.
      Controls->Add( domainUpDown1 );
   }

   void button1_Click( Object^ sender,
      EventArgs^ e )
   {
      // Add the text box contents and initial location in the collection
      // to the DomainUpDown control.
      domainUpDown1->Items->Add( String::Concat(
         (textBox1->Text->Trim()), " - ", myCounter ) );
      
      // Increment the counter variable.
      myCounter = myCounter + 1;
      
      // Clear the TextBox.
      textBox1->Text = "";
   }

   void checkBox1_Click( Object^ sender,
      EventArgs^ e )
   {
      
      // If Sorted is set to true, set it to false; 
      // otherwise set it to true.
      domainUpDown1->Sorted =  !domainUpDown1->Sorted;
   }

   void domainUpDown1_SelectedItemChanged( Object^ sender,
      EventArgs^ e )
   {
      
      // Display the SelectedIndex and 
      // SelectedItem property values in a MessageBox.
      MessageBox::Show( String::Concat( "SelectedIndex: ", domainUpDown1->SelectedIndex,
         "\nSelectedItem: ", domainUpDown1->SelectedItem ) );
   }

平台

Windows 98、Windows 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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

DomainUpDown.DomainUpDownItemCollection 类
DomainUpDown.DomainUpDownItemCollection 成员
System.Windows.Forms 命名空间
Insert
Remove
RemoveAt