vb.net 循环遍历列表框项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32300432/
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 19:26:04  来源:igfitidea点击:

Loop through listbox items

vb.netvisual-studio-2008

提问by Dustin Bobbitt

I am making a program to automate entering data into a website im building. I can only enter one line of data at a time. So I want to have a listbox with say 8 items, I want to take listbox item 1 enter it into an html feild with the value datafield, click send, then navigate back to my site "mysite.com", enter listbox item 2 into datafield, click send and loop until all listbox items have been entered.

我正在制作一个程序来自动将数据输入到我正在构建的网站中。我一次只能输入一行数据。所以我想要一个包含 8 个项目的列表框,我想将列表框项目 1 输入到带有值数据字段的 html 字段中,单击发送,然后导航回我的网站“mysite.com”,将列表框项目 2 输入到数据字段,单击发送并循环,直到输入所有列表框项目。

If I set a textbox, this works to enter a single line a data from textbox 1, but how could I loop it to go go through all my listbox items.

如果我设置了一个文本框,这可以从文本框 1 中输入一行数据,但是我如何循环它以遍历我的所有列表框项目。

   'Paste url  from textbox to datafield
        WebBrowser1.Document.GetElementById("datafield").SetAttribute("value", textbox1.text)

        'click search button
        Dim allelements As HtmlElementCollection = WebBrowser1.Document.All

        For Each webpageelement As HtmlElement In allelements

            If webpageelement.GetAttribute("type") = "submit" Then

                webpageelement.InvokeMember("click")

            End If

Also is there any good way for when I navigate back to "mysite.com" to wait until the sites loaded to enter the next line of data. If there anyway you could, please take the code I posted and show me a way to paste data from the listbox to the datafield. Like enter listbox item 1 to datafield, click submit, navigate back to homepage then enter listbox item 2 in datafield click submit. and do until its at the end of the listbox

当我导航回“mysite.com”时,还有什么好方法可以等到网站加载后输入下一行数据。如果可以的话,请使用我发布的代码并向我展示一种将数据从列表框粘贴到数据字段的方法。就像在数据字段中输入列表框项目 1,单击提交,导航回主页然后在数据字段中输入列表框项目 2 单击提交。直到它在列表框的末尾

回答by Drarig29

Use this :

用这个 :

For Each item In ListBox1.Items
    'Do stuff
    MsgBox(item.ToString)
Next

Or if you need the index of each item, use this :

或者,如果您需要每个项目的索引,请使用:

For i = 0 To ListBox1.Items.Count - 1
    'Do stuff
    MsgBox(ListBox1.Items(i).ToString)
Next