如何在VB中向其他ListBox添加项目?

时间:2013-06-24 05:55:41

标签: vb.net listbox

这是我的代码:

Public Class Form1

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim FindFolder As New FolderBrowserDialog
    FindFolder.ShowDialog()
    TextBox1.Text = FindFolder.SelectedPath
End Sub

Public Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        My.Settings.theSetPath = TextBox1.Text
        My.Settings.isValidPath = True
        My.Settings.Save()
        TextBox1.Text = My.Settings.theSetPath
        Dim folderInfo As New IO.DirectoryInfo(My.Settings.theSetPath)
        Dim txtFilesInFolder() As IO.FileInfo
        Dim cfgFilesInFolder() As IO.FileInfo
        Dim xmlFilesInFolder() As IO.FileInfo
        Dim datFilesInFolder() As IO.FileInfo
        Dim fileInFolder As IO.FileInfo
        txtFilesInFolder = folderInfo.GetFiles("*.txt")
        cfgFilesInFolder = folderInfo.GetFiles("*.cfg")
        xmlFilesInFolder = folderInfo.GetFiles("*.xml")
        datFilesInFolder = folderInfo.GetFiles("*.dat")

        For Each fileInFolder In txtFilesInFolder
            Second.List.Items.Add(fileInFolder.Name)
        Next
        For Each fileInFolder In cfgFilesInFolder
            Second.List.Items.Add(fileInFolder.Name)
        Next
        For Each fileInFolder In xmlFilesInFolder
            Second.List.Items.Add(fileInFolder.Name)
        Next
        For Each fileInFolder In datFilesInFolder
            Second.List.Items.Add(fileInFolder.Name)
        Next
        MsgBox("Testing")
    Catch ex As Exception
        MsgBox("That is not a valid directory.", MsgBoxStyle.Critical, "Error")
        My.Settings.isValidPath = False
        My.Settings.Save()
    End Try

    Second.Show()

End Sub

Public Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    Dim path As String
    path = TextBox1.Text
End Sub

Public Sub TextBox2_TextChanged(sender As Object, e As EventArgs)

End Sub

End Class

我希望程序从指定文件夹中的文本文件中获取文件名,然后将它们列在listBox上。但是ListBox在另一个GUI上。

当我按下按钮时,它会打开另一个GUI,并在第二个GUI上将文件名输出到listBox。

我得到“那不是一个有效的目录。”出于某种原因,即使它是有效的。

并且它在其他ListBox上没有显示任何内容。我不知道我做错了什么。

1 个答案:

答案 0 :(得分:0)

您的问题可能是尝试直接访问第二个表单。要从第一个表单访问它,请声明一个新的第二个表单,“Dim Sec as New Second”。现在,您可以从第一个窗口访问第二个窗体中的所有控件。然后使用'Sec.Show()'来显示表单。

第二种形式的列表框的数据源是什么?