如何根据复选框状态更新表单

How to update a form based on a checkbox status

我目前正在尝试为软件 (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

现在该代码的问题是它只检查复选框的初始状态,但当复选框为 checked/unchecked 时不更新列表。

如何做到每次更改复选框状态时更新列表 VpList?

谢谢!

这里的关键是添加一个子程序来检查复选框。您将需要该 sub,因为它会根据用户的操作多次调用。

因为您没有提供有关 tmpView 和 vpList 的任何见解,所以我会坚持使用您的代码,但是您应该注意,根据您尝试执行的具体操作,您的代码可以简化或重写以提高效率.例如,如果您没有指定 tmpVP 值在列表中是唯一的还是不止一次,我假设您想要唯一,所以这里是 sub 的代码(请阅读代码中的注释):

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

现在你有了sub,你可以随时调用它。 "Whenever you want" 表示各种用户操作,例如选中或取消选中复选框,以及其他各种地方,例如表单的初始化(加载),这些都是事件。根据您的问题,您需要从 3 个不同的事件中调用它。

1.When 正在加载表单以获得初始状态:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub

2.When用户改变了StructcheckBox状态:

Private Sub StructcheckBox_CheckedChanged(sender As Object, e As EventArgs) Handles StructcheckBox.CheckedChanged
    CheckBoxesStatus(StructcheckBox.Checked, LegcheckBox.Checked)
End Sub

3.When 用户更改 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

希望对您有所帮助。