Home > front end >  VB.NET ComboBox selected item not remains effect than 2 times
VB.NET ComboBox selected item not remains effect than 2 times

Time:03-05

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.

CodePudding user response:

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

CodePudding user response:

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)
  • Related