VB.NET中的List用法


Imports System.IO
Imports System.Data.OleDb
Public Class Form_mobilecodeFilter
    Public list As New List(Of String)
    Public mobilecode As New List(Of mobile)
    Public loadlist As New List(Of plist)

    '结构体

    Public Structure mobile
        Public mobilecode As String
        Public Province As String
        Public City As String
        Public Card As String
    End Structure

    Public Structure plist
        Public province As String
        Public mobileNew As List(Of citylist)
    End Structure

    Public Structure citylist
        Public city As String
        Public mobilecontext As List(Of mobileLoadwritetxt)
    End Structure

    Public Structure mobileLoadwritetxt
        Public context As String
        Public mobilecode As String
        Public Province As String
        Public City As String
        Public Card As String
    End Structure

    Private Sub Button_Importmobilecode_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Importmobilecode.Click
        Dim fileName As String
        OpenFileDialog_ImportMobileCode.Filter = "文本文件(*.txt)|*.txt"
        OpenFileDialog_ImportMobileCode.ShowDialog()
        fileName = OpenFileDialog_ImportMobileCode.FileName
        TextBox_Importfilepath.Text = fileName

        Dim path As String
        Dim line As String = String.Empty
        path = TextBox_Importfilepath.Text
        Try
            FileOpen(1, path, OpenMode.Input)
        Catch ex As Exception
            FileClose(1)
            FileOpen(1, path, OpenMode.Input)
        End Try
        line = ""

        Do While Not EOF(1)
            Input(1, line)
            If line.Trim <> "" Then
                list.Add(line)
            End If
        Loop
        FileClose(1)
    End Sub

    Private Sub Button_Checkout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_Checkout.Click

        Dim fileName As String
        Dim result As DialogResult = FolderBrowserDialog_download.ShowDialog()
        fileName = FolderBrowserDialog_download.SelectedPath
        Dim i, j, k As Integer
        Dim cl As citylist
        Dim mo As mobileLoadwritetxt
        For i = 0 To loadlist.Count - 1
            Dim txtpath As String = fileName & "\" & loadlist.Item(i).province
            System.IO.Directory.CreateDirectory(txtpath)
            For j = 0 To loadlist.Item(i).mobileNew.Count - 1
                cl = loadlist.Item(i).mobileNew.Item(j)
                Dim writefile As New StreamWriter(txtpath & "\" & cl.city & ".txt")
                For k = 0 To cl.mobilecontext.Count - 1
                    mo = cl.mobilecontext.Item(k)
                    Dim context As String = mo.context
                    Dim province As String = mo.Province
                    Dim City As String = mo.City
                    Dim mobilecode As String = mo.mobilecode
                    Dim Card As String = mo.Card
                    Dim writecontext As String = ""
                    If CheckBox_ID.Checked Then
                        writecontext = writecontext & k 1 & "   "
                    End If
                    If CheckBox_mobilecode.Checked Then
                        writecontext = writecontext & context & "     "
                    End If
                    If CheckBox_province.Checked Then
                        writecontext = writecontext & province & "    "
                    End If
                    If CheckBox_city.Checked Then
                        writecontext = writecontext & City & "    "
                    End If
                    If CheckBox_haoduan.Checked Then
                        writecontext = writecontext & mobilecode & "    "
                    End If
                    If CheckBox_execute.Checked Then
                        writecontext = writecontext & Card
                    End If
                    writefile.WriteLine(writecontext)
                Next
                writefile.Flush()
                writefile.Close()
            Next
        Next
        TextBox_Importfilepath.Text = ""
        MessageBox.Show("完成")

    End Sub

    Private Sub Button_closeMe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_closeMe.Click
        Me.Close()
    End Sub

    Public Sub loadmobile()
        Dim SQLGroupStr = "select * from mobile"
        Dim CString As String = " Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\ctc-nft.mdb;Persist Security Info=True"
        Dim con As New OleDbConnection(CString)
        Dim com As New OleDbCommand()
        com.Connection = con
        con.Open()
        com.CommandType = CommandType.Text
        com.CommandText = SQLGroupStr
        Dim rs As OleDbDataReader = com.ExecuteReader()
        While (rs.Read)
            Dim m As New mobile
            m.mobilecode = rs.GetString(0)
            m.Province = rs.GetString(1)
            m.City = rs.GetString(2)
            m.Card = rs.GetString(3)
            mobilecode.Add(m)
        End While
        con.Close()
    End Sub

    Private Sub Button_manage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_manage.Click
        loadmobile()
        MessageBox.Show("在处理中可能要等几分钟,请稍等.........", "提示")
        Dim i, j As Integer
        Dim a As Integer = 1
        Dim m As mobile
        For i = 0 To mobilecode.Count - 1
            m = mobilecode.Item(i)
            For j = 0 To list.Count - 1
                If m.mobilecode.ToString.Equals(list.Item(j).ToString.Substring(0, 7)) Then
                    If a = 1 Then
                        a = a 1
                        Dim pl As New plist
                        pl.province = mobilecode.Item(i).Province
                        Dim lm As New citylist
                        lm.city = mobilecode.Item(i).City
                        Dim mo As New mobileLoadwritetxt
                        mo.Card = mobilecode.Item(i).Card
                        mo.City = mobilecode.Item(i).City
                        mo.context = list.Item(j)
                        mo.mobilecode = mobilecode.Item(i).mobilecode
                        mo.Province = mobilecode.Item(i).Province
                        lm.mobilecontext = New List(Of mobileLoadwritetxt)
                        lm.mobilecontext.Add(mo)
                        pl.mobileNew = New List(Of citylist)
                        pl.mobileNew.Add(lm)
                        loadlist.Add(pl)
                    Else
                        Dim sp As String = isProvince(loadlist, mobilecode.Item(i).Province)
                        Dim str() As String = sp.Split(" ")
                        Dim flagprovince As String = str(0)
                        Dim flag As Boolean
                        If flagprovince.ToString.Equals("1") Then
                            flag = True
                        Else
                            flag = False
                        End If
                        If flag = True Then
                            Dim sc As String = iscity(loadlist, mobilecode.Item(i).City)
                            Dim str1() As String = sc.Split(" ")
                            Dim flagcity As Boolean
                            If str1(0).ToString.Equals("1") Then
                                flagcity = True
                            Else
                                flagcity = False
                            End If
                            If flagcity = True Then
                                Dim mob As New mobileLoadwritetxt
                                mob.Card = mobilecode.Item(i).Card
                                mob.City = mobilecode.Item(i).City
                                mob.context = list.Item(j)
                                mob.mobilecode = mobilecode.Item(i).mobilecode
                                mob.Province = mobilecode.Item(i).Province
                                loadlist.Item(str(1)).mobileNew.Item(str1(1)).mobilecontext.Add(mob)
                            Else
                                Dim cl As New citylist
                                cl.city = mobilecode.Item(i).City
                                Dim mi As New mobileLoadwritetxt
                                mi.Card = mobilecode.Item(i).Card
                                mi.City = mobilecode.Item(i).City
                                mi.context = list.Item(j)
                                mi.mobilecode = mobilecode.Item(i).mobilecode
                                mi.Province = mobilecode.Item(i).Province
                                cl.mobilecontext = New List(Of mobileLoadwritetxt)
                                cl.mobilecontext.Add(mi)
                                loadlist.Item(str(1)).mobileNew.Add(cl)
                            End If
                        Else
                            Dim pl As New plist
                            pl.province = mobilecode.Item(i).Province
                            Dim lm As New citylist
                            lm.city = mobilecode.Item(i).City
                            Dim mo As New mobileLoadwritetxt
                            mo.Card = mobilecode.Item(i).Card
                            mo.City = mobilecode.Item(i).City
                            mo.context = list.Item(j)
                            mo.mobilecode = mobilecode.Item(i).mobilecode
                            mo.Province = mobilecode.Item(i).Province
                            lm.mobilecontext = New List(Of mobileLoadwritetxt)
                            lm.mobilecontext.Add(mo)
                            pl.mobileNew = New List(Of citylist)
                            pl.mobileNew.Add(lm)
                            loadlist.Add(pl)
                        End If
                        End If
                End If
            Next
        Next
        MessageBox.Show("请立即导入,当关闭此窗口数据就会销毁", "提示")
    End Sub

    Public Function isProvince(ByVal list As List(Of plist), ByVal str As String)
        Dim i As Integer
        For i = 0 To list.Count - 1
            If str.ToString.Equals(list.Item(i).province.ToString) Then
                Return 1 & " " & i
            End If
        Next
        Return 0 & " " & i - 1
    End Function

    Public Function iscity(ByVal list As List(Of plist), ByVal str As String)
        Dim i, j As Integer
        Dim pl As plist
        pl.mobileNew = New List(Of citylist)
        Dim cl As citylist
        For i = 0 To list.Count - 1
            pl.mobileNew = list.Item(i).mobileNew
            For j = 0 To pl.mobileNew.Count - 1
                cl = pl.mobileNew.Item(j)
                If cl.city.ToString.Equals(str) Then
                    Return 1 & " " & j
                End If
            Next
        Next
        Return 0 & " " & j - 1
    End Function

End Class

     '此段代码现给有一定编程基础的朋友

你可能感兴趣的:(C#)