VB.NET ComboBox所选项目的保留效果不超过2次
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在较低的
Else
块中,您错误地构建了文件名。您使用以下命令构建第一个“newfn1”:
但在下面,您使用了:
注意开头缺少的前缀和破折号部分。
这是完整的更正版本:
In your lower
Else
block, you were incorrectly building up the file name.You build up the first "newfn1" with:
But then below, you used:
Notice the missing prefix and dash parts at the beginning.
Here's the full corrected version:
我对你的解释有点困惑,但如果我理解正确的话,这应该会有所帮助,
I'm a little confused by your explanation but if I understand correctly this should help,