Remove Checkboxes?

How do I remove Checkboxes from my form below is the code I use to create them (How many depends on what the user types in the two textboxes, one for cols and one for rows) but how do I remove them Should the code be placed inside a button event to clear the form or would it be better to place it in it's own sub ie Private Sub ClearBoxes() and have it called when the user enters a number in one of the text boxes

Private Sub MakeBoxes()
RSize = CInt(RowSize.Text)
CSize = CInt(ColSize.Text)
MatrixSize = RSize * CSize
ReDim CheckBoxMatrix(MatrixSize - 1)
ReDim CheckBoxSequence(MatrixSize - 1)
i = 0
For RowCount = 1 To RSize
For ColCount = 1 To CSize
CheckBoxMatrix(i) = i.ToString
Dim ChkBox As New CheckBox
AddHandler ChkBox.CheckedChanged, _
AddressOf SequenceCapture
ChkBox.Name = "Checkbox" & i.ToString + 1
ChkBox.Location = _
New Point(20 * ColCount, 20 * RowCount)
ChkBox.Text = ""
ChkBox.Size = New Size(20, 20)
Me.Controls.Add(ChkBox)
i += 1
Next
Next

End Sub


Answer this question

Remove Checkboxes?

  • tomdart

    nicely observed Dave. :-)

    I would have pretty much said the same thing. But would have also asked if you wish to remove all checkboxes on the form if so then we pretty much can do this:

    for each currentControl as Control in Me.Controls

    if currentControl Is CheckBox then

    Me.Controls.Remove(currentControl)

    end if

    next



  • Paul Baudouin

    ah I know why, you should be going through backwards from the collection...

    for counter as Integer = this.groupBox2.Controls.Count - 1 to 0 step -1

       if typeof(Me.Controls(counter)) Is CheckBox then

         Me.Controls.Remove(Me.Controls(counter))

      end if

    next

     



  • beermix

    Thanks - I knew I had seen that before somewhere recently - glad you could remember where it was.

    For those who might be confused the following code works.

    For counter As Integer = Me.Controls.Count - 1 To 0 Step -1

    If TypeOf Me.Controls(counter) Is CheckBox Then

    Me.Controls.Remove(Me.Controls(counter))

    End If

    Next


  • Roozbeh Sharafi

    Strange - when I run the following code it leaves five boxes behind. I wonder what you do that is different.

    Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    For X As Integer = 1 To 10

    Dim Box As New CheckBox

    Me.Controls.Add(Box)

    Box.SetBounds(200, 50 + 30 * X, 20, 20)

    Next

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    For Each currentControl As Control In Me.Controls

    If TypeOf currentControl Is CheckBox Then

    Me.Controls.Remove(currentControl)

    End If

    Next

    End Sub

    End Class


  • Robert Wakeland

    Using the controls collection is not a good way to remove controls as it will usually only remove half of them.

    I think what happens is that once you remove the first one the collection is updated and what was the second one now becomes the first one. But your loop thinks it has already done that one so it goes on to remove the next one, leaving the second one (which is now the first) in the collection. Does that make sense

    Something like the following seems to work ok

    Dim RemovedOne As Boolean

    Do

    RemovedOne = False

    For Each C As Control In Me.Controls

    If TypeOf C Is CheckBox Then

    Me.Controls.Remove(C)

    RemovedOne = True

    Exit For

    End If

    Next

    If Not RemovedOne Then

    Exit Do

    End If

    Loop While RemovedOne


  • PerBylund

    actually I disagree with that dave. it WILL remove them if its been added into the collection.

  • Olmann

    glad we sorted that out Dave! lol :-)

  • Umair Khan

    Thanks Dave,
    That's worked, The code came from the following link, not sure if its worked or not <url>http://visualbasic.about.com/</url>
    It was an answer to a question that someone had asked and I started to play around with it and decided I would like the option to remove the Checkboxes.

    Here is the full code that I am using.

    Public Class Form1
    Dim CheckBoxSequence() As String
    Dim CheckSeq As Integer = 0
    Dim CheckBoxMatrix() As String
    Dim MatrixSize, i, RowCount, ColCount As Integer
    Dim RSize, CSize As Integer
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    MakeBoxes()
    End Sub
    Private Sub SequenceCapture(ByVal sender As Object, ByVal e As System.EventArgs)
    CheckBoxSequence(CheckSeq) = sender.name
    CheckSeq += 1
    End Sub
    Private Sub Button1_Click( _
    ByVal sender As System.Object, _
    ByVal e As System.EventArgs)Handles Button1.Click

    For i As Int16 = 0 To CheckSeq
    Label1.Text = Label1.Text & CheckBoxSequence(i) & vbCrLf
    Next

    End Sub
    Private Sub MakeBoxes()
    RSize = CInt(RowSize.Text)
    CSize = CInt(ColSize.Text)
    MatrixSize = RSize * CSize
    ReDim CheckBoxMatrix(MatrixSize - 1)
    ReDim CheckBoxSequence(MatrixSize - 1)
    i = 0
    For RowCount = 1 To RSize
    For ColCount = 1 To CSize
    CheckBoxMatrix(i) = i.ToString
    Dim ChkBox As New CheckBox
    AddHandler ChkBox.CheckedChanged, _
    AddressOf SequenceCapture
    ChkBox.Name = "Checkbox" & i.ToString + 1
    ChkBox.Location = _
    New Point(20 * ColCount, 20 * RowCount)
    ChkBox.Text = ""
    ChkBox.Size = New Size(20, 20)
    Me.Controls.Add(ChkBox)
    i += 1
    Next
    Next

    End Sub
    Private Sub ColSize_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles ColSize.LostFocus
    MakeBoxes()
    End Sub
    Private Sub RowSize_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles RowSize.LostFocus
    MakeBoxes()
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim RemovedOne As Boolean
    Do
    RemovedOne = False

    For Each C As Control In Me.Controls

    If TypeOf C Is CheckBox Then
    Me.Controls.Remove(C)
    RemovedOne = True
    Exit For
    End If
    Next

    If Not RemovedOne Then
    Exit Do
    End If

    Loop While RemovedOne
    End Sub

    End Class





  • Chris D Jones

    First of all there are a number of issues with the code you have posted for creating the checkboxes.

    1. If you call the procedure without entering anything into the textboxes then the lines that set RSize and CSize will generate exceptions.

    2. The CheckBoxMatrix seems a bit odd. The line that says

    CheckBoxMatrix(i) = i.ToString

    isn't really doing anything useful as it is simply setting the content of the array element to the string representation of the index.

    3. The line

    ChkBox.Name = "Checkbox" & i.ToString + 1

    generates an exception with Option Strict On (which is usually a good idea). Try changing it to

    ChkBox.Name = "Checkbox" & (i+1).ToString

    4. You haven't kept a reference to any of the checkboxes you have created which causes you a problem when you want to get rid of them.

    I would suggest you make CheckBoxMatrix an array of checkboxes, i.e.

    Dim CheckBoxMatrix() as CheckBox

    Then rework your loop as follows:

    For RowCount = 1 To Rsize
    For ColCount = 1 To Csize
    Dim ChkBox As New CheckBox
    AddHandler ChkBox.CheckedChanged, AddressOf SequenceCapture
    ChkBox.Name = "Checkbox" & (i + 1).ToString
    ChkBox.Location = New Point(20 * ColCount, 20 * RowCount)
    ChkBox.Text = ""
    ChkBox.Size = New Size(20, 20)
    CheckBoxMatrix(i) = ChkBox
    Me.Controls.Add(ChkBox)
    i += 1
    Next
    Next

    Then when you want to get rid of them all you have to do is

    Private Sub RemoveBoxes()
    For counter As Integer = 0 To Matrixsize - 1
    CheckBoxMatrix(counter).Dispose()
    Next
    End Sub


  • Remove Checkboxes?