简体   繁体   English

如何将项目添加到VB中的另一个ListBox?

[英]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. 我希望程序从指定文件夹中的文本文件中获取文件名,然后在listBox上列出它们。 But the ListBox is on another GUI. 但是ListBox在另一个GUI上。

When I press the button, it's suppose to open up another GUI, and output the filenames to listBox on the second GUI. 当我按下按钮时,应该打开另一个GUI,然后将文件名输出到第二个GUI上的listBox。

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. 并且在其他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'. 要从第一个表格访问它,请声明一个新的第二个表格“ 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. 然后使用“ Sec.Show()”显示表单。

Also what is the datasource of the listbox in the second form? 另外,第二种形式的列表框的数据源是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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