vb.net 对于 ListBox1 中的每个项目做一些事情然后将项目添加到 listbox2 vb

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19719158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:41:05  来源:igfitidea点击:

For Each Item in ListBox1 do something then add item to listbox2 vb

vb.netloopsforeachlistboxitems

提问by GHoStyaiRo

I made an app to convert certain numbers to other format
i.e

我制作了一个应用程序来将某些数字转换为其他格式,

  • 1 = A
  • 2 = B
  • 3 = C
  • 4 = D
  • 5 = E
  • ETC
  • 1 = A
  • 2 = 乙
  • 3 = C
  • 4 = D
  • 5 = E
  • 等等

I have made that function with no problem and I have been using it for quite sometime, but now I would like to do things faster and in a batch.
So it's really difficult for me to copy from a text file to my Textbox1 then press button1 then copy textbox2 to other text file.

我已经毫无问题地制作了该功能,并且我已经使用它有一段时间了,但现在我想更快地批量处理。
所以我真的很难从文本文件复制到我的 Textbox1 然后按 button1 然后将 textbox2 复制到其他文本文件。

So I was thinking in loading the text file into a List Box then do a loop for each item in that list into a second list that I can export to another text file.

所以我想将文本文件加载到列表框中,然后将该列表中的每个项目循环到第二个列表中,我可以将其导出到另一个文本文件中。

The importing and exporting I have it covered but where I'm stuck at is to make the loop.

我已经涵盖了导入和导出,但我遇到的问题是进行循环。

Here's what I have please if you know a better way to do it let me know or tell me how to fix it this way.

这是我所拥有的,如果您知道更好的方法,请告诉我或告诉我如何以这种方式修复它。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Using FD As New OpenFileDialog()
        FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
            ListBox1.Items.Clear()
            ListBox1.Items.AddRange(IO.File.ReadAllLines(FD.FileName))
        End If
    End Using
End Sub


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Do
    Dim Item As String = ""
    For Each i As String In ListBox1.Items
        Item &= i
        TextBox1.Text = Item
        TextBox2.Text = MYFUNCTION(TextBox1.Text)
        ListBox2.Items.Add(TextBox2.Text)
        TextBox1.Text = ""
        TextBox2.Text = ""
    Next
    Loop Until TextBox1.Text = "END"

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    'TextBox2.Text = MeidHexToDec(TextBox1.Text)

    Using FD As New SaveFileDialog()
        FD.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
        If FD.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim FileContent As String = ""
            For Each i As String In ListBox2.Items
                FileContent &= i & vbCrLf
            Next
            IO.File.WriteAllText(FD.FileName, FileContent)
        End If
    End Using
End Sub

So my final aim is to do something like this:

所以我的最终目标是做这样的事情:

TextFile1.txt

文本文件1.txt

  • 1
  • 2
  • 5
  • 5
  • 1
  • 3
  • 2
  • END
  • 1
  • 2
  • 5
  • 5
  • 1
  • 3
  • 2
  • 结尾

then after the conversion output

然后在转换输出之后

TextFile2.txt

文本文件2.txt

  • A
  • B
  • E
  • E
  • A
  • C
  • B
  • 一种
  • 一种
  • C

The text file size will vary sometimes it will have only 10 items sometimes will be 50... Thanks.

文本文件的大小有时会有所不同,有时只有 10 个项目,有时是 50 个……谢谢。

回答by Karl Anderson

You do not need the Doloop at all and you can simplify the rest of your loop logic, like this:

您根本不需要Do循环,您可以简化其余的循环逻辑,如下所示:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    For Each i As String In ListBox1.Items
        ListBox2.Items.Add(MYFUNCTION(i))
    Next
End Sub

You do not need to look out for the ENDmarker, because everything in the file was read into the ListBox1.Itemscollection, thus once you have looped through all of the string values in ListBox1.Items, then you are at the end of the file.

您不需要寻找END标记,因为文件中的ListBox1.Items所有内容都已读入集合中,因此一旦您遍历了 中的所有字符串值ListBox1.Items,那么您就位于文件的末尾。

The MYFUNCTIONlogic returns the transformation from number to letter, thus just add the result of that function into ListBox2.Itemsand you are done.

MYFUNCTION逻辑返回从数字到字母的转换,因此只需将该函数的结果添加到其中即可ListBox2.Items

回答by djb

Public Function Letr(N As Int16) As String
    Letr = Chr(N + 64)
End Function