如何将数据加载到 ArrayList 中

来源:互联网 发布:Linux的loop 编辑:程序博客网 时间:2024/05/16 00:19

ArrayList是一个集合,动态数组,可以放任何对象Object。动态的增加和减少元素,实现了ICollection和IList接口
灵活的设置数组的大小。

Imports System.Collections

Public Class FrmSelFileSetionClass FrmSelFileSetion

    
Private Class FileSetionClass FileSetion
        
Private myFNameEN As String
        
Private myFNameCN As String

        
Public Sub New()Sub New(ByVal FNameEN As StringByVal FNameCN As String)
            myFNameEN 
= FNameEN
            myFNameCN 
= FNameCN
        
End Sub


        
Public ReadOnly Property FNameEN()Property FNameEN() As String
            
Get
                
Return myFNameEN
            
End Get
        
End Property


        
Public ReadOnly Property FNameCN()Property FNameCN() As String
            
Get
                
Return myFNameCN
            
End Get
        
End Property

    
End Class


    
Dim FList As New ArrayList()

    
Private Sub FrmSelFileSetion_Load()Sub FrmSelFileSetion_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FList.Add(
New FileSetion("ItmeNo""货号"))
        FList.Add(
New FileSetion("ItmeSize""尺寸"))
        FList.Add(
New FileSetion("UnitPrice""价格"))
        FList.Add(
New FileSetion("ProductWeight""产品重量"))
        FList.Add(
New FileSetion("DimensionOutL,DimensionOutW,DimensionOutH""外箱尺寸"))
        FList.Add(
New FileSetion("DimensionOutGW""外箱重量"))
        FList.Add(
New FileSetion("CasePackOut""外箱装箱量"))
        FList.Add(
New FileSetion("ShipQTY,ShipCTN""40尺柜装箱量"))
        ListBox1.DataSource 
= FList
        ListBox1.ValueMember 
= "FNameEN"
        ListBox1.DisplayMember 
= "FNameCN"
    
End Sub


    
Private Sub ListBox1_MouseDown()Sub ListBox1_MouseDown(ByVal sender As ObjectByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown
        
If Me.ListBox1.SelectedIndex <> -1 Then
            
Me.ListBox1.DoDragDrop(Me.ListBox1.Text & "|@|" & Me.ListBox1.SelectedValue.ToString, DragDropEffects.Copy)
        
End If
    
End Sub


    
Private Sub TextBoxS_DragDrop()Sub TextBoxS_DragDrop(ByVal sender As ObjectByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop, TextBox2.DragDrop, TextBox3.DragDrop, TextBox4.DragDrop
        
If e.Data.GetDataPresent(DataFormats.Text) Then
            
Dim SelData() As String
            SelData 
= Split(e.Data.GetData(DataFormats.Text), "|@|")
            
CType(sender, TextBox).Text = SelData(0)
            
CType(sender, TextBox).Tag = SelData(1)
        
End If
    
End Sub


    
Private Sub TextBoxS_DragEnter()Sub TextBoxS_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter, TextBox2.DragEnter, TextBox3.DragEnter, TextBox4.DragEnter
        e.Effect 
= DragDropEffects.Copy
    
End Sub


    
Private Sub Button1_Click()Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
MsgBox("文本:" & TextBox1.Text & vbCrLf & "隐藏内容:" & TextBox1.Tag.ToString, , "你选择的内容"
   
End Sub

End Class

ArrayList的几个常用的方法:

Count属性是目前ArrayList包含的元素的数量,这个属性是只读的。
Capacity属性是目前ArrayList能够包含的最大数量,可以手动的设置这个属性,但是当设置为小于Count值的时候会引发一个异常。
Add方法用于添加一个元素到当前列表的末尾
AddRange方法用于添加一批元素到当前列表的末尾
Remove方法用于删除一个元素,通过元素本身的引用来删除
RemoveAt方法用于删除一个元素,通过索引值来删除
RemoveRange用于删除一批元素,通过指定开始的索引和删除的数量来删除
Insert用于添加一个元素到指定位置,列表后面的元素依次往后移动
InsertRange用于从指定位置开始添加一批元素,列表后面的元素依次往后移动
Clear方法用于清除现有所有的元素
Contains方法用来查找某个对象在不在列表之中   
TrimSize这个方法用于将ArrayList固定到实际元素的大小,当动态数组元素确定不在添加的时候,可以调用这个方法来释放空余的内存。
ToArray这个方法把ArrayList的元素Copy到一个新的数组中。

原创粉丝点击