当我遍历 For Each 时,如何 select 基于复选框状态的特定变量
How do I select specific variables based on checkbox state as I iterate through a For Each
我正在开展一个项目,该项目要求我遍历标签页上的控件列表以找到所有复选框。然后根据框的状态(选中或未选中)select 单个变量(文件名),然后执行批量重命名或删除文件系统上的文件(cb.checked = 执行操作)。
我已经设法为控件的迭代创建 "for each"(感谢 google),但我正在努力弄清楚如何选择变量。很明显,它们的名称都不同,复选框也是如此。此外,复选框静态分配给 form/tabpage。这是我目前拥有的。
Public Sub delBut_code(ByRef fname As String)
If (Sanity = 1) Then
For Each cb As Control In Form1.Controls
If TypeOf cb Is CheckBox AndAlso DirectCast(cb,
CheckBox).Checked Then
If My.Computer.FileSystem.FileExists(fname) Then
My.Computer.FileSystem.DeleteFile(fname)
End If
End If
Next
MessageBox.Show("All Actions Completed Successfully")
Else
MessageBox.Show("Please select a File To Delete")
End If
End Sub
这里是一些变量的例子:
Dim castle As String = selPath & "\zm_castle_loadingmovie.txt"
Dim factory As String = selPath &
"\zm_factory_load_factoryloadingmovie.txt"
Dim island As String = selPath & "\zm_island_loadingmovie.txt"
N.B selpath 收集用户输入的文件夹路径,这里可以忽略
非常感谢任何指点。
首先,您可以使用循环做得更好:
Public Sub delBut_code(ByRef fname As String)
If Sanity <> 1 Then
MessageBox.Show("Please select a File To Delete")
Exit Sub
End If
Dim checked = Form1.Controls.OfType(Of CheckBox)().Where(Function(c) c.Checked)
For Each box As CheckBox in checked
Try
'A file not existing is only one reason among many this could fail,
' so it needs to be in a Try/Catch block.
' And once you're using a Try/Catch block anyway,
' the FileExists() check becomes a slow and unnecessary extra trip to the disk.
My.Computer.FileSystem.DeleteFile(fname)
Catch
'Do something here to let the user know it failed for this file
End Try
Next
MessageBox.Show("All Actions Completed")
End Sub
但是现在您需要知道如何在 fname
变量中获得正确的值。问题中没有足够的信息让我们完全回答这个问题,但我们可以给出一些建议。您可以通过多种方式执行此操作:
- 在构建字符串变量时,在复选框中设置
Tag
属性。然后 fname
变成 DirectCast(box.Tag, String)
.
- 从 CheckBox 继承一个自定义控件来代替普通的 Checkbox,后者有一个额外的字符串 属性 作为文件名。在构建字符串变量时设置此 属性。
- 以可以从 CheckBox 变量名称派生字符串变量名称的方式命名您的字符串变量,然后使用
Switch
从每个 box.Name
中选择正确的字符串变量。
- 保留
Dictionary(Of CheckBox, String)
将复选框映射到正确的字符串值。
但是在不了解应用程序的更多上下文的情况下,我犹豫要不要推荐其中的任何一个以最适合您的情况。
我正在开展一个项目,该项目要求我遍历标签页上的控件列表以找到所有复选框。然后根据框的状态(选中或未选中)select 单个变量(文件名),然后执行批量重命名或删除文件系统上的文件(cb.checked = 执行操作)。
我已经设法为控件的迭代创建 "for each"(感谢 google),但我正在努力弄清楚如何选择变量。很明显,它们的名称都不同,复选框也是如此。此外,复选框静态分配给 form/tabpage。这是我目前拥有的。
Public Sub delBut_code(ByRef fname As String)
If (Sanity = 1) Then
For Each cb As Control In Form1.Controls
If TypeOf cb Is CheckBox AndAlso DirectCast(cb,
CheckBox).Checked Then
If My.Computer.FileSystem.FileExists(fname) Then
My.Computer.FileSystem.DeleteFile(fname)
End If
End If
Next
MessageBox.Show("All Actions Completed Successfully")
Else
MessageBox.Show("Please select a File To Delete")
End If
End Sub
这里是一些变量的例子:
Dim castle As String = selPath & "\zm_castle_loadingmovie.txt"
Dim factory As String = selPath &
"\zm_factory_load_factoryloadingmovie.txt"
Dim island As String = selPath & "\zm_island_loadingmovie.txt"
N.B selpath 收集用户输入的文件夹路径,这里可以忽略
非常感谢任何指点。
首先,您可以使用循环做得更好:
Public Sub delBut_code(ByRef fname As String)
If Sanity <> 1 Then
MessageBox.Show("Please select a File To Delete")
Exit Sub
End If
Dim checked = Form1.Controls.OfType(Of CheckBox)().Where(Function(c) c.Checked)
For Each box As CheckBox in checked
Try
'A file not existing is only one reason among many this could fail,
' so it needs to be in a Try/Catch block.
' And once you're using a Try/Catch block anyway,
' the FileExists() check becomes a slow and unnecessary extra trip to the disk.
My.Computer.FileSystem.DeleteFile(fname)
Catch
'Do something here to let the user know it failed for this file
End Try
Next
MessageBox.Show("All Actions Completed")
End Sub
但是现在您需要知道如何在 fname
变量中获得正确的值。问题中没有足够的信息让我们完全回答这个问题,但我们可以给出一些建议。您可以通过多种方式执行此操作:
- 在构建字符串变量时,在复选框中设置
Tag
属性。然后fname
变成DirectCast(box.Tag, String)
. - 从 CheckBox 继承一个自定义控件来代替普通的 Checkbox,后者有一个额外的字符串 属性 作为文件名。在构建字符串变量时设置此 属性。
- 以可以从 CheckBox 变量名称派生字符串变量名称的方式命名您的字符串变量,然后使用
Switch
从每个box.Name
中选择正确的字符串变量。 - 保留
Dictionary(Of CheckBox, String)
将复选框映射到正确的字符串值。
但是在不了解应用程序的更多上下文的情况下,我犹豫要不要推荐其中的任何一个以最适合您的情况。