简体   繁体   中英

How to add items to the other ListBox in VB?

Here is my code:

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

I want the program to get filenames from text files in the specified folder, and then list them on a listBox. But the ListBox is on another GUI.

When I press the button, it's suppose to open up another GUI, and output the filenames to listBox on the second GUI.

I get "That is not a valid directory." For some reason, even though it is valid.

And it doesn't show anything on the other ListBox. I don't know what I'm doing wrong.

Your problem is, probably, in trying to access the second form directly. To access it from the first form declare a new second form, 'Dim Sec as New Second'. Now you can access all the controls in the second form, from the first. Then use 'Sec.Show()' to show the form.

Also what is the datasource of the listbox in the second form?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM