对于ListBox1中的每个项目,执行一些操作,然后将项目添加到listbox2 vb [英] For Each Item in ListBox1 do something then add item to listbox2 vb
问题描述
我做了一个应用程序将某些数字转换为其他格式
ie
- 1 = A
- 2 = B
3 = C
4 = D
- 5 = E
- ETC
我已经完成了这个功能,没有任何问题,我一直在使用它,但是现在我希望能够更快地做到这一点。
所以我很难复制从一个文本文件到我的Textbox1,然后按下按钮1,然后将textbox2复制到其他文本文件。所以我在考虑将文本文件加载到列表框中,然后为列表中的每个项目做一个循环到第二个列表,我可以导出到另一个文本文件。
导入和导出我已经覆盖了,但是我坚持的地方是做循环。
<如果你知道更好的方法,请告诉我如何解决这个问题。 $ c> Private Sub Button1_Click(sender As Object,e As EventArgs)Handles Button1.Click
使用FD作为新的OpenFileDialog()
FD.Filter =txt files(* .txt)| * .txt |所有文件(*。*)| *。*
如果FD.ShowDialog = Windows.Forms.DialogResult.OK然后
ListBox1.Items.Clear()
ListBox1.Items.AddRange (IO.File.ReadAllLines(FD.FileName))
End If
End使用
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs)处理Button2.Click
Do
Dim Ite m 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
循环直到TextBox1.Text = END
End Sub
Private Sub Button3_Click(sender As Object,e As EventArgs)Handle Button3.Click
'TextBox2.Text = MeidHexToDec(TextBox1。文本)
使用FD作为新的SaveFileDialog()
FD.Filter =txt文件(* .txt)| * .txt |所有文件(*。*)| *。*
如果FD.ShowDialog = Windows.Forms.DialogResult.OK然后
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使用
End Sub
所以我的最终目标是做这样的事情:
TextFile1.txt p>
- 1
- 2
- 5
- 5
- 1
- 3
- 2
- END
p>
TextFile2.txt
- A
$ li
$ b $ li $ A $ $ b- C
- B
文本文件的大小会有所不同,它将只有10个项目,有时会50 ...
谢谢。
您完全不需要 Do
简化剩下的循环逻辑,如下所示:
Private Sub Button2_Click(sender As Object,e As EventArgs)处理Button2。单击
对于每个我作为字符串在ListBox1.Items
ListBox2.Items.Add(MYFUNCTION(i))
Next
End Sub
您不需要注意 END
标记,因为文件中的所有内容都是读入 ListBox1.Items
集合,因此,一旦循环遍历 ListBox1.Items
中的所有字符串值,那么你在这个文件的结尾。
$ b $ p $ MYCUNCTION逻辑返回从数字到字母的转换,只需将该函数的结果添加到 ListBox2.Items
中,就完成了。
I made an app to convert certain numbers to other format
i.e
- 1 = A
- 2 = B
- 3 = C
- 4 = D
- 5 = E
- ETC
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.
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
- 2
- 5
- 5
- 1
- 3
- 2
- END
then after the conversion output
TextFile2.txt
- A
- B
- E
- E
- A
- C
- B
The text file size will vary sometimes it will have only 10 items sometimes will be 50... Thanks.
You do not need the Do
loop at all and you can simplify the rest of your loop logic, like this:
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 END
marker, because everything in the file was read into the ListBox1.Items
collection, thus once you have looped through all of the string values in ListBox1.Items
, then you are at the end of the file.
The MYFUNCTION
logic returns the transformation from number to letter, thus just add the result of that function into ListBox2.Items
and you are done.
这篇关于对于ListBox1中的每个项目,执行一些操作,然后将项目添加到listbox2 vb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!