我目前正在尝试为软件(Revit)编写一些宏,但是遇到了一个我不知道如何解决的问题。
所以我有一个带有两个复选框和一个元素列表的Windows窗体,我希望根据复选框的状态来更新元素列表。
这是我的复选框状态”代码:
If StructcheckBox.Checked = True Then
Select Case tmpView.ViewType
Case ViewType.EngineeringPlan
vpList.Add(tmpVP)
End Select
End If
If LegcheckBox.Checked = True Then
Select Case tmpView.ViewType
Case ViewType.Legend
vpList.Add(tmpVP)
End Select
End If
现在,该代码的问题在于,它仅检查复选框的初始状态,而在选中/取消选中复选框时不更新列表。
如何使其在每次复选框状态更改时更新列表VpList?
谢谢!
答案 0 :(得分:1)
此处的关键是添加一个用于复选框检查的子项。您将需要该sub,因为它将根据用户的操作多次调用。
因为您没有提供有关tmpView和vpList的任何见解,所以我会坚持使用您的代码,但是您应注意,根据您要执行的操作,您的代码可以简化或重写,以提高效率。例如,您没有指定希望tmpVP值在列表中是唯一的还是大于一倍,我假设您希望唯一,因此这是子代码(请阅读代码中的注释) ):
Private Sub CheckBoxesStatus(StructcheckBoxChecked As Boolean, LegcheckBoxChecked As Boolean)
If StructcheckBoxChecked Then
If tmpView.ViewType = ViewType.EngineeringPlan Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'StructcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
If LegcheckBoxChecked Then
If tmpView.ViewType = ViewType.Legend Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'LegcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
End Sub
现在您有了子,您可以随时调用它。 “随时随地”表示对用户的各种操作,例如选中或取消选中复选框,以及在其他各种位置(如表单的初始化(加载)),这些都是事件。根据您的问题,您需要从3个不同的事件中调用它。
1。在加载表单以获取初始状态时:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
2。当用户更改StructcheckBox状态时:
Private Sub StructcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles StructcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
3。当用户更改LegcheckBox状态时:
Private Sub LegcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LegcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
这是完整表格的代码:
Public Class Form1
Private Sub CheckBoxesStatus(StructcheckBoxChecked As Boolean, LegcheckBoxChecked As Boolean)
If StructcheckBoxChecked Then
If tmpView.ViewType = ViewType.EngineeringPlan Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'StructcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
If LegcheckBoxChecked Then
If tmpView.ViewType = ViewType.Legend Then
'Add code here to check if the tmpVP element is already in the vpList
'and add it only if there isn't otherwise it will be added each time the
'LegcheckBox is checked by the user...
'An example code is as follows:
If Not vpList.Contains(tmpVP) Then vpList.Add(tmpVP)
End If
End If
End Sub
Private Sub StructcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles StructcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Private Sub LegcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles LegcheckBox.CheckedChanged
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub
End Class
希望这会有所帮助。