【问题标题】:combobox not updating? VB.net组合框不更新? VB.net
【发布时间】:2013-05-24 08:53:38
【问题描述】:

当我在组合框 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

【问题讨论】:

  • “我错过了什么” - 组合框 1 选择更改事件处理程序?
  • @MitchWheat 我想我唯一想做的就是在 combobox1 选择更改事件下从“Dim filelocation As String”向下剪切并粘贴所有内容?

标签: vb.net combobox refresh


【解决方案1】:

您的 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

【讨论】:

  • 那行得通,虽然我把它拆开以便更好地理解它非常感谢:)
【解决方案2】:

当 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 

【讨论】:

    猜你喜欢
    • 2011-07-23
    • 2011-11-08
    • 1970-01-01
    • 1970-01-01
    • 2011-05-13
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多