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

コンボボックス 1 の選択された項目が変更されたら、適切なイベント ハンドラーを追加してイベントをインターセプトする必要があります。次に、このイベントで、2 番目のコンボにファイル名を再入力します。

ただし、最初のコンボに 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 に答える