vb.netwinformssharpdevelop

How to update a form based on a checkbox status


I'm currently trying to code some macros for a software (Revit), and I ran into an issue I do not know how to solve.

So I have a windows form with two checkboxes and a list of elements, and I would like that list of elements to be updated depending on what the status of the checkboxes is.

Here's my checkbox status' code:

        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

Now the problem with that code is that it only checks the initial status of the checkboxes, but do not update the list when the checkboxes are checked/unchecked.

How to make it so that the list VpList is updated everytime a checkbox status is changed?

Thanks!


Solution

  • The key here is to add a sub that does the checkboxes checking. You will need that sub because it will called multiple times depending on the user's actions.

    Because you are not providing any insights about tmpView and vpList I will stick with your code, however you should note that depending on what exactly are you trying to do your code can be simplified or rewritten to be a bit more efficient. For example you don't specify if you want the tmpVP value to be unique in the list or to be more than one times, I assume that you want to be unique, so here is the sub's code (please read the comments in the code):

    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
    

    Now that you have the sub you can call it whenever you want. "Whenever you want" means on various user's actions, like checking or un-checking a checkbox and in various other places like form's initialization (loading) these are events. According to your question you need to call it from 3 different events.

    1.When the form is loading in order to get the initial status:

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

    2.When the user changes the StructcheckBox status:

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

    3.When the user changes the LegcheckBox status:

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

    Here is the full form's code:

    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
    

    Hope this helps.