当我在combobox1中更改选择时,我无法更新combobox2中的内容,我错过了什么或做错了什么?
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
答案 0 :(得分: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
答案 1 :(得分: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