0

以下代码在加载时执行并从目录中检索文件名

Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  Dim lynxin As New IO.DirectoryInfo(sPath)
  lstPlanned.Items.Clear()
  For Each txtfi In lynxin.GetFiles("*.txt")
    lstPlanned.Items.Add(IO.Path.GetFileNameWithoutExtension(txtfi.Name)) 'filename only
  Next
End Sub

我想要的是它像上面那样拉回信息(没有扩展名),但如果需要打开文件,仍然可以双击列表框中的项目。我知道我可以更改lstPlanned.Items.Add(IO.Path.GetFileNameWithoutExtension(txtfi.Name))lstPlanned.Items.Add(IO.Path.GetFile(txtfi.Name))并且该过程将起作用,但是我不希望显示文件扩展名。我不确定我是否应该查看解析或替换文本。

4

3 回答 3

2
Dim lynxin As New IO.DirectoryInfo(sPath)

lstPlanned.Items.Clear()

For Each txtfi In lynxin.GetFiles("*.txt")
    Dim i As New ListItem
    i.Value = txtfi.Name
    i.Text = IO.Path.GetFileNameWithoutExtension(txtfi.Name)
    lstPlanned.Items.Add(i) 'filename only

Next

编辑:如果它是一个 Windows.Forms 项目然后尝试:

   Dim l = (From p1 In lynxin.GetFiles("d:\", "*.jpg")
             Select New With {.fi = New IO.FileInfo(p1),
                              .Name = .fi.Name.Replace(.fi.Extension, ""),
                              .Data = p1}
                          ).ToList

    ComboBox1.ValueMember = "Data"
ComboBox1.DisplayMember = "Name"
ComboBox1.DataSource = l
于 2013-11-15T11:33:30.960 回答
1

我喜欢 Lisa-Berlin 对使用 ValueMember()、DisplayMember() 和 LINQ 的 WinForms 的回答,但其中有几个错误。这是一个更简洁的示例,它还显示了在进行选择后如何使用 ListBox 的 SelectedValue() 成员:

Public Class Main

    Private spath = "C:\Users\Mike\Documents"

    Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim Files = (From file In System.IO.Directory.GetFiles(spath, "*.txt")
                     Select New With {
                         .FullName = file,
                         .FileNameWithoutExtension = System.IO.Path.GetFileNameWithoutExtension(file)}
                     ).ToList

        lstPlanned.ValueMember = "FullName"
        lstPlanned.DisplayMember = "FileNameWithoutExtension"
        lstPlanned.DataSource = Files
    End Sub

    Private Sub lstPlanned_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles lstPlanned.SelectedIndexChanged
        If lstPlanned.SelectedIndex <> -1 Then
            Label1.Text = lstPlanned.SelectedValue.ToString
        End If
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If lstPlanned.SelectedIndex <> -1 Then
            Process.Start(lstPlanned.SelectedValue.ToString)
        End If
    End Sub

End Class
于 2013-11-15T15:29:34.863 回答
1

这就是汉斯所指的:

Class Element
   Public ItemName As String = ""
   Public ItemData As Object = Nothing

   Public Sub New(n As String, d as object)
       ItemName = n
       ItemData = d
   End Sub

   Public Sub New()
   End Sub

   Public Overrides Function ToString() As String
      Return ItemName
   End Sub
 End Class

要使用它:

   For Each fi as in lynxin.GetFiles("*.txt")
         ' create an element, what you want to display is first arg
         ' second it the data to store
         Dim El AS New Element(Path.GetFileNameWithoutExtension(fi.Name),
               fi.Name)
         lstbox.add(El)
   Next

访问数据的语法如下:

   console.WriteLine("file: {0}   fullname: {1}", _
        lstBox.Items(N).ItemName,  lstBox.Items(N).ItemData) 

lstBox.Items不再指一个简单的字符串,而是一个元素对象。要获取基础数据,请使用 Element 成员进行限定:lstBox.Items(N).ItemName在这种情况下返回不带 Ext 的文件名(N是一个虚拟变量),.ItemData在这种情况下是完整的文件名。

你可以在很多类似的情况下使用这样的小班

于 2013-11-15T13:29:48.710 回答