0

当我在组合框 1 中更改我的选择时,我无法更新组合框 2 中的内容,我错过了什么或做错了什么?

  Imports System.IO
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'get sub directory\ toolpalette group names...
        ComboBox1.DataSource = New DirectoryInfo("C:\VTS\TREADSTONE LT\ATC").GetDirectories()

        Dim filelocation As String
        filelocation = ("C:\VTS\TREADSTONE LT\ATC\" & ComboBox1.Text & "\")

        'gets file\ paltte names...
        For Each BackupFiles As String In My.Computer.FileSystem.GetFiles(filelocation, FileIO.SearchOption.SearchTopLevelOnly, "*.*")
            ComboBox2.Items.Add(IO.Path.GetFileNameWithoutExtension(BackupFiles))
        Next

        'reloads the combobox contents...
        ComboBox1.Refresh()

    End Sub


End Class
4

2 回答 2

0

当 combobox1 所选项目更改时,您需要拦截事件并添加适当的事件处理程序,然后在此事件中,用文件名重新填充第二个组合。

但是,您使用 DirectoryInfo 对象列表填充第一个组合,当您检索此对象时,您的 DirectoryInfo 不是字符串。您应该提取 DirectoryInfo 对象并使用 FullName 属性来查找所需的文件

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged


    ' First clear the Items collection of the second combo
    ComboBox2.Items.Clear()

    ' Now checks if you have really something selected
    Dim comboBox As comboBox = CType(sender, comboBox)
    if comboBox.SelectedItem Is Nothing Then
        Return
    End If


    Dim di = CType(comboBox.SelectedItem, DirectoryInfo)
    Dim filelocation = di.FullName

    For Each BackupFiles As String In My.Computer.FileSystem.GetFiles(filelocation, FileIO.SearchOption.SearchTopLevelOnly, "*.*")
        ComboBox2.Items.Add(IO.Path.GetFileNameWithoutExtension(BackupFiles))
    Next

End Sub 
于 2013-05-24T09:22:54.283 回答
0

您的 ComboBox1 SelectedIndexChanged 事件处理程序应该是这样的..

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

  if ComboBox1.SelectedIndex = -1 then exit sub

  Dim filelocation As String
  filelocation = ("C:\VTS\TREADSTONE LT\ATC\" & ComboBox1.Text & "\")

  ComboBox2.Items.Clear() '---> clearing combobox2 list

  'gets file\ paltte names...
  For Each BackupFiles As String In My.Computer.FileSystem.GetFiles(filelocation, FileIO.SearchOption.SearchTopLevelOnly, "*.*")
        ComboBox2.Items.Add(IO.Path.GetFileNameWithoutExtension(BackupFiles))
  Next
End Sub
于 2013-05-24T09:23:33.863 回答