VB.Net列表框问题

时间:2014-08-15 20:30:42

标签: vb.net

我正在尝试编写一个执行以下操作的程序:

1)打开URL列表(完成)

2)添加到列表框(已完成)

3)从文本框中选择前3个项目

4)Webbrowser1,Webbrowser 2和Webbrowser 3导航到从列表框中选择的URL

5)删除部分网址,并将剩余文本添加到Listbox2,一次添加一个项目。

6)删除Listbox1上的选定项目并重新开始处理。

到目前为止,这是我的代码:

    Imports System.IO

Public Class Form1

Dim em As String = "@facebook.com"

Dim url As String = "https://facebook.com/profile.php?id="


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ListBox1.Text = ""

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        BackgroundWorker1.RunWorkerAsync()

    End Sub

    Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        Me.ListBox1.SetSelected(1, True)
        Me.ListBox1.SetSelected(2, True)
        Me.ListBox1.SetSelected(3, True)

        WebBrowser1.Navigate(url + ListBox1.SelectedItems(1).ToString())
        WebBrowser2.Navigate(url + ListBox1.SelectedItems(2).ToString())
        WebBrowser3.Navigate(url + ListBox1.SelectedItems(3).ToString())



    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        ListBox2.Items.Add(WebBrowser1.Url.ToString + em)

    End Sub

    Private Sub WebBrowser2_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser2.DocumentCompleted
        ListBox2.Items.Add(WebBrowser2.Url.ToString + em)
    End Sub

    Private Sub WebBrowser3_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser3.DocumentCompleted
        ListBox2.Items.Add(WebBrowser3.Url.ToString + em)
        System.Threading.Thread.Sleep("1000")
        ListBox1.SelectedItems.Remove(1)
        ListBox1.SelectedItems.Remove(2)
        ListBox1.SelectedItems.Remove(3)
        System.Threading.Thread.Sleep("1000")
        BackgroundWorker1.RunWorkerAsync()

    End Sub
End Class

这是我能想到的最简单的方法(我没有循环经验)。任何帮助都表示赞赏,因为我在几年内没有使用过VB.Net。

1 个答案:

答案 0 :(得分:0)

BackgroundWorkers无法修改任何" physical"对象。就像你在表格中看到的那些东西一样。 BackgroundWorkers只能修改抽象对象,例如变量。