【问题标题】:vb .net rename files based in two listbox within same positionvb .net基于同一位置的两个列表框重命名文件
【发布时间】:2019-06-13 10:37:12
【问题描述】:

我有一个带有文件名(不带扩展名)的列表框。
我需要将这些文件重命名为与 SelectedIndex 中的 listbox2 具有相同值的名称,但我遇到了一些麻烦。

我的重命名代码正在运行(使用一个简单的文件名进行测试),但我无法从 listbox1 获得相同的“位置”并从 listbox2 获得值来重命名文件:

For Each oItem In ListBox1.Items
    My.Computer.FileSystem.RenameFile(TextBox1.Text & oItem & ".png", "SecondTest.txt")
Next oItem

由于 oItem 没有 SelectedIndex 的值,我无法从 ListBox2 的同一 SelectedIndex 中获取值。

有人可以帮帮我吗?

【问题讨论】:

  • For i as Integer = 0 to ListBox1.Items.Count - 1 枚举代替。

标签: vb.net


【解决方案1】:

你不应该在这里使用For Each,因为你需要索引。你可以这样写:

Dim n as Integer = ListBox1.Items.Count
For i = 0 to n - 1
    Dim oItem as String = ListBox1.Items(i), item2 as String = ListBox2.Items(i)
    My.Computer.FileSystem.RenameFile(TextBox1.Text & oItem & ".png", item2)
Next 

RenameFile 中的代码应该根据你的需要编写。 (item2 是第二个列表框中的关联项)

【讨论】:

    猜你喜欢
    • 2022-09-22
    • 2020-04-18
    • 1970-01-01
    • 2015-01-04
    • 2015-12-06
    • 2018-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多