本文介绍了如何通过单击按钮调用迭代器函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很清楚在使用标签或文本框来说出更改文本或执行小任务时如何在按钮内调用函数.我的问题是尝试调用一个函数来执行我的枚举函数?
I am well aware of how to call a function inside a button when using a label or text box to say change text or to do small tasks of that matter. My problem is trying to call a function to carry out my enumeration function?
Private Iterator Function GetFilesRecursive(ByVal dirInfo As DirectoryInfo, ByVal searchPattern As String) As IEnumerable(Of FileInfo)
searchPattern = ComboBox1.SelectedItem
For Each di As DirectoryInfo In dirInfo.GetDirectories()
For Each fi As FileInfo In GetFilesRecursive(di, searchPattern)
Yield fi
Next fi
Next di
For Each fi As FileInfo In dirInfo.GetFiles(searchPattern)
Yield fi
Next fi
End Function
推荐答案
Public Class FrmTest
''First solution
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Lst As Generic.List(Of System.IO.FileInfo)
Lst = GetFileRecursive("C:\temp", "3W5357*")
For Each f As System.IO.FileInfo In Lst
'' your code
Next
End Sub
Private Function GetFileRecursive(ByVal Path As String, ByVal FileSearchPattern As String) As Generic.List(Of System.IO.FileInfo)
Dim d As New System.IO.DirectoryInfo(Path)
Return GetFileRecursive(d, FileSearchPattern)
End Function
Private Function GetFileRecursive(ByVal Path As System.IO.DirectoryInfo, ByVal FileSearchPattern As String) As Generic.List(Of System.IO.FileInfo)
Dim lstfile As New Generic.List(Of System.IO.FileInfo)
''first step
''Add file
For Each s As String In System.IO.Directory.GetFiles(Path.FullName, FileSearchPattern)
Dim F As New System.IO.FileInfo(s)
lstfile.Add(F)
Next
''Serch for subdirectory
For Each s As String In System.IO.Directory.GetDirectories(Path.FullName)
lstfile.AddRange(GetFileRecursive(s, FileSearchPattern))
Next
Return lstfile
End Function
''Second solution
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim Lst As New MyFile("C:\temp", "3W5357*")
For Each f As System.IO.FileInfo In Lst
'' your code
Next
End Sub
End Class
Public Class MyFile
Implements IEnumerable
Private MyPath As String
Private MyFileSearchPath As String
Public Sub New(ByVal Path As String, ByVal FileSearchPath As String)
MyBase.New()
MyPath = Path
MyFileSearchPath = FileSearchPath
End Sub
Public Function GetEnumerator() As System.Collections.IEnumerator Implements System.Collections.IEnumerable.GetEnumerator
Return New MyFileEnum(MyPath, MyFileSearchPath)
End Function
End Class
Public Class MyFileEnum
Implements IEnumerator
Private Position As Integer = -1
Private MyLst As Generic.List(Of System.IO.FileInfo)
Private Function GetFileRecursive(ByVal Path As String, ByVal FileSearchPattern As String) As Generic.List(Of System.IO.FileInfo)
Dim d As New System.IO.DirectoryInfo(Path)
Return GetFileRecursive(d, FileSearchPattern)
End Function
Private Function GetFileRecursive(ByVal Path As System.IO.DirectoryInfo, ByVal FileSearchPattern As String) As Generic.List(Of System.IO.FileInfo)
Dim lstfile As New Generic.List(Of System.IO.FileInfo)
''first step
''Add file
For Each s As String In System.IO.Directory.GetFiles(Path.FullName, FileSearchPattern)
Dim F As New System.IO.FileInfo(s)
lstfile.Add(F)
Next
''Serch for subdirectory
For Each s As String In System.IO.Directory.GetDirectories(Path.FullName)
lstfile.AddRange(GetFileRecursive(s, FileSearchPattern))
Next
Return lstfile
End Function
Public Sub New(ByVal Path As String, ByVal FileSearchPath As String)
MyBase.New()
MyLst = GetFileRecursive(Path, FileSearchPath)
End Sub
Public ReadOnly Property Current() As Object Implements System.Collections.IEnumerator.Current
Get
Try
Return MyLst(Position)
Catch ex As IndexOutOfRangeException
Throw New InvalidOperationException
End Try
End Get
End Property
Public Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
Position = Position + 1
Return (Position < MyLst.Count)
End Function
Public Sub Reset() Implements System.Collections.IEnumerator.Reset
Position = -1
End Sub
End Class
这篇关于如何通过单击按钮调用迭代器函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!