删除列表框中的文件扩展名,但将其保留在标记中

时间:2013-11-15 11:25:01

标签: vb.net winforms

以下代码在加载时执行,并从目录

中检索文件名
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)),但该过程将有效,但我不希望显示文件扩展名。我不确定是否应该查看解析或替换文本。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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不再引用简单字符串,而是引用元素对象。要获取基础数据,请使用元素成员限定:lstBox.Items(N).ItemName在这种情况下返回不带Ext的文件名(N是虚拟变量),.ItemData在这种情况下将是完整的文件名。

对于许多类似情况,你可以使用类似的小类

答案 2 :(得分: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