我想知道下图中的代码。
我有一个带有一些复选框和一个按钮的表格,
screen is here
我试过这段代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If CheckBox1.Checked = True And CheckBox2.Checked = True And CheckBox3.Checked = True And CheckBox4.Checked = True Then
'when the button is clicked will be the process for moving images
'Like
System.IO.File.Copy(Application.StartupPath + "\File\Pic1.jpg", "D:\File\Pic1.jpg")
End If
End Sub
我对这段代码感到厌倦,编码是否更短? 例如,如果checkbox1.checked = true而另一个复选框未选中,则仅移动一个图标
答案 0 :(得分:2)
如果我理解了这个问题,如果选中复选框1到4,则需要复制图片1到4。 试试这个:
Dim SourcePath As string = Application.StartupPath + "\File\"
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
CopyFile(CheckBox1, "Pic1.jpg")
CopyFile(CheckBox2, "Pic2.jpg")
CopyFile(CheckBox3, "Pic3.jpg")
CopyFile(CheckBox4, "Pic4.jpg")
End Sub
Private Sub CopyFile(CB As CheckBox, FileName As String)
If CB.Checked Then
System.IO.File.Copy(SourcePath + FileName, "D:\File\" + FileName)
End If
End Sub