VB.NET ComboBox 选中的项目不会保持效果超过 2 次

VB.NET ComboBox selected item not remains effect than 2 times

我有一个 VB windows 表单应用程序,它有 02 ComboBox 为重命名文件事件提供新名称输入。第一个组合框为新名称提供前缀包括项目(aa、bb、cc、...可以通过 keydown 按钮单击事件添加更多),另一个组合框提供主要名称包括项目(XX、YY、ZZ、..更多通过 keydown 按钮点击事件)。当我 select 来自第一个组合框的“aa”,来自另一个组合框的“XX”然后触发重命名事件时,新文件名应该是“aa - XX”,如果文件“aa - XX”已经存在将“1”添加到最后一个为“aa - XX 1”等等,如果前缀组合框中没有项目 selected,则新名称只是“XX”并递增。我通过系统打开文件对话框获取旧文件名。我的重命名代码如下:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim var As String, prfix As String
        var = ComboBox1.Text
        prfix = ComboBox2.Text
        If ComboBox2.Text = Nothing Then
            If File.Exists(n & "\" & var & extn) = False Then
                My.Computer.FileSystem.RenameFile(OpenFD.FileName, var & extn)
            Else
                Dim i As Integer = 1
                Dim newfn As String = var & " " & i & extn
                Dim m As String = n & "\" & newfn
                While File.Exists(m)
                    newfn = var & " " & i & extn
                    m = n & "\" & newfn
                    i += 1
                End While
                My.Computer.FileSystem.RenameFile(OpenFD.FileName, newfn)
            End If
        Else
            If File.Exists(n & "\" & prfix & " - " & var & extn) = False Then
                My.Computer.FileSystem.RenameFile(OpenFD.FileName, prfix & " - " & var & extn)
            Else
                Dim j As Integer = 1
                Dim newfn1 As String = prfix & " - " & var & " " & j & extn
                Dim k As String = n & "\" & newfn1
                While File.Exists(k)
                    newfn1 = var & " " & j & extn
                    k = n & "\" & newfn1
                    j += 1
                End While
                My.Computer.FileSystem.RenameFile(OpenFD.FileName, newfn1)
            End If
        End If
        MessageBox.Show("Select a next file")
    End Sub

我的代码运行好了2次。在我 select "aa" 和 "XX" 并保留它重命名后,第一个结果是 "aa - XX",第二个结果是 "aa - XX 1" 但第三个结果是 "XX",第四个是“XX 1”然后递增,而结果应该是“aa - XX 2”和下一个递增。我不明白为什么 combobox1 仍然有效但 combobox2 在没有重新 select 两个组合框中的项目(2 次)之后就没有了。我是 VB 的新手,所以任何建议都将不胜感激。谢谢

我对你的解释有点困惑,但如果我理解正确,这应该有所帮助,

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    CreateFile()
End Sub

Private BasePath As String = "" 'TODO
Private Ext As String = "txt"
Private Sub CreateFile()
    If ComboBox1.SelectedIndex < 0 OrElse
            ComboBox2.SelectedIndex < 0 OrElse
            ComboBox1.SelectedItem.ToString = "" OrElse
            ComboBox2.SelectedItem.ToString = "" Then
        'error message
        Exit Sub
    End If
    Dim fileName As String = String.Format("{0}-{1}.{2}",
                                            ComboBox1.SelectedItem.ToString,
                                            ComboBox2.SelectedItem.ToString,
                                            Ext)

    fileName = IO.Path.Combine(BasePath, fileName)
    Dim ct As Integer = 1
    Do While IO.File.Exists(fileName)
        fileName = String.Format("{0}-{1}{3}.{2}",
                                  ComboBox1.SelectedItem.ToString,
                                  ComboBox2.SelectedItem.ToString,
                                  Ext,
                                  ct)
        fileName = IO.Path.Combine(BasePath, fileName)
        ct += 1
    Loop
    Dim fs As IO.FileStream = IO.File.Create(fileName)
    fs.Close()
    fs.Dispose()
End Sub

在您较低的 Else 块中,您错误地建立了文件名。

您构建了第一个“newfn1”:

Dim newfn1 As String = prfix & " - " & var & " " & j & extn

但是在下面,您使用了:

newfn1 = var & " " & j & extn

注意开头缺少的前缀和破折号部分。

这是完整的更正版本:

Dim j As Integer = 1
Dim newfn1 As String = prfix & " - " & var & " " & j & extn
Dim k As String = Path.Combine(n, newfn1)
While File.Exists(k)
    j = j + 1
    newfn1 = prfix & " - " & var & " " & j & extn
    k = Path.Combine(n, newfn1)
End While
My.Computer.FileSystem.RenameFile(OpenFD.FileName, newfn1)