VB.NET ComboBox所选项目的保留效果不超过2次

发布于 2025-01-11 18:16:40 字数 2232 浏览 0 评论 0原文

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

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次。当我选择“aa”和“XX”并保留其重命名后,第一个结果是“aa - XX”,第二个结果是“aa - XX 1”,但第三个结果是“XX”,第四个结果是“XX 1” ”,然后递增,依此类推,而结果应该是“aa - XX 2”并进行下一个递增。我不明白为什么组合框1仍然有效,但组合框2在没有重新选择两个组合框中的项目(2次)后什么也没有。我对 VB 很陌生,所以任何建议都值得感激。谢谢。

I have a VB windows form application that has 02 ComboBox that provide newname input for a renaming file event. The first combobox provide prefix for new name comprise items (aa, bb, cc,... can add more through keydown button click event), the other combobox provide main name comprise items (XX, YY, ZZ,.. can also add more through keydown button click event). When I select "aa" from the first combobox, "XX" from the other then fire the rename event, the new file name should be "aa - XX", if file "aa - XX" has already existed then add "1" to the last as "aa - XX 1" and so on and if no item selected in prefix combobox the newname just be "XX" and increment. I get the old file name through a system openfiledialog. My code for rename as follows:

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

My code run well 2 times. After I select "aa" and "XX" and leave it to rename, first result is "aa - XX", the second result is "aa - XX 1" but the third result is "XX", the forth is "XX 1" and then incrementing so on while the result should be "aa - XX 2" and next increment. I don't understand why combobox1 still effective but combobox2 as Nothing after no re-selecting the item in both comboboxes (2 times). I'm very new with VB so any advice should be much appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

财迷小姐 2025-01-18 18:16:40

在较低的 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)

In your lower Else block, you were incorrectly building up the file name.

You build up the first "newfn1" with:

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

But then below, you used:

newfn1 = var & " " & j & extn

Notice the missing prefix and dash parts at the beginning.

Here's the full corrected version:

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)
凉宸 2025-01-18 18:16:40

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

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

I'm a little confused by your explanation but if I understand correctly this should help,

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文