用三个列表框的值写一个 .txt
Write a .txt with values of three listboxes
我必须导出一个 .txt 文件,每行包含三个列表框的值。我尝试使用以下代码来做到这一点:
Using writer = New StreamWriter(SaveFileDialog1.FileName)
For Each o As String In Form10.ListBox1.Items.Cast(Of Object).Zip(Form10.ListBox2.Items.Cast(Of Object).Zip(Form6.ListBox2.Items.Cast(Of Object)), Function(x1, x2, x3) CStr(x1) & "," & CStr(x2) & "," & CStr(x3))
writer.WriteLine(o)
Next
End Using
我收到几个错误:
Error BC36646 Data type(s) of the type parameter(s) in extension method 'Public Function Zip(Of TSecond, TResult)(second As IEnumerable(Of TSecond), resultSelector As Func(Of Object, TSecond, TResult)) As IEnumerable(Of TResult)' defined in 'Enumerable' cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
Error BC36586 Argument not specified for parameter 'resultSelector' of extension method 'Public Function Zip(Of TSecond, TResult)(second As IEnumerable(Of TSecond), resultSelector As Func(Of Object, TSecond, TResult)) As IEnumerable(Of TResult)' defined in 'Enumerable'.
Error BC42020 Variable declaration without an 'As' clause; type of Object assumed.
不幸的是,虽然 Form10.Listbox1 和 Form10.Listbox2 的大小应该相同,但 Form6.Listbox6 较小;所以我需要导出的文件应该是以下格式:
F10.LB1,F10.LB2,F6.LB2 - 用于 Form6.Listbox6 的长度,F10.LB1,F10.LB2 用于其余项目。如果您需要更多解释,请评论或回答。谢谢所有人都会在这里写信给我。最好的问候。
编辑:
Using writer = New StreamWriter("d:\test.txt")
For id As Integer = 0 To ListBox1.Items.Count - 1
Dim to_write As String = ListBox1.Items(id) + "," + ListBox2.Items(id)
If (ListBox3.Items.Count -1 >= id ) Then
to_write += "," + ListBox3.Items(id)
End If
writer.WriteLine(to_write)
Next
End Using
不要忘记计数 -1 ;如果您有 5 个项目:0 到 4 = 5 个项目。
你觉得这样好吗:
Using writer = New StreamWriter("d:\test.txt")
For Each o As String In ListBox1.Items
writer.WriteLine(o)
Next
For Each o As String In ListBox2.Items
writer.WriteLine(o)
Next
For Each o As String In ListBox3.Items
writer.WriteLine(o)
Next
End Using
或者对于所有列表框:
Using writer = New StreamWriter("d:\test.txt")
For Each lbx As ListBox In Me.Controls.OfType(Of ListBox)
For Each o As String In lbx.Items
writer.WriteLine(o)
Next
Next
End Using
我必须导出一个 .txt 文件,每行包含三个列表框的值。我尝试使用以下代码来做到这一点:
Using writer = New StreamWriter(SaveFileDialog1.FileName)
For Each o As String In Form10.ListBox1.Items.Cast(Of Object).Zip(Form10.ListBox2.Items.Cast(Of Object).Zip(Form6.ListBox2.Items.Cast(Of Object)), Function(x1, x2, x3) CStr(x1) & "," & CStr(x2) & "," & CStr(x3))
writer.WriteLine(o)
Next
End Using
我收到几个错误:
Error BC36646 Data type(s) of the type parameter(s) in extension method 'Public Function Zip(Of TSecond, TResult)(second As IEnumerable(Of TSecond), resultSelector As Func(Of Object, TSecond, TResult)) As IEnumerable(Of TResult)' defined in 'Enumerable' cannot be inferred from these arguments. Specifying the data type(s) explicitly might correct this error.
Error BC36586 Argument not specified for parameter 'resultSelector' of extension method 'Public Function Zip(Of TSecond, TResult)(second As IEnumerable(Of TSecond), resultSelector As Func(Of Object, TSecond, TResult)) As IEnumerable(Of TResult)' defined in 'Enumerable'.
Error BC42020 Variable declaration without an 'As' clause; type of Object assumed.
不幸的是,虽然 Form10.Listbox1 和 Form10.Listbox2 的大小应该相同,但 Form6.Listbox6 较小;所以我需要导出的文件应该是以下格式: F10.LB1,F10.LB2,F6.LB2 - 用于 Form6.Listbox6 的长度,F10.LB1,F10.LB2 用于其余项目。如果您需要更多解释,请评论或回答。谢谢所有人都会在这里写信给我。最好的问候。
编辑:
Using writer = New StreamWriter("d:\test.txt")
For id As Integer = 0 To ListBox1.Items.Count - 1
Dim to_write As String = ListBox1.Items(id) + "," + ListBox2.Items(id)
If (ListBox3.Items.Count -1 >= id ) Then
to_write += "," + ListBox3.Items(id)
End If
writer.WriteLine(to_write)
Next
End Using
不要忘记计数 -1 ;如果您有 5 个项目:0 到 4 = 5 个项目。
你觉得这样好吗:
Using writer = New StreamWriter("d:\test.txt")
For Each o As String In ListBox1.Items
writer.WriteLine(o)
Next
For Each o As String In ListBox2.Items
writer.WriteLine(o)
Next
For Each o As String In ListBox3.Items
writer.WriteLine(o)
Next
End Using
或者对于所有列表框:
Using writer = New StreamWriter("d:\test.txt")
For Each lbx As ListBox In Me.Controls.OfType(Of ListBox)
For Each o As String In lbx.Items
writer.WriteLine(o)
Next
Next
End Using