# [VB.NET] Buttons Collection.



## noaco (Sep 17, 2010)

hi,
i have this button collection i add in run time and then remove, but for some reason it is not removed.


```
System.Windows.Forms.Button).Text, String))
        Dim stringa() As String = {"1", CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(0, 5), CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(6), "", CType(CType(sender, System.Windows.Forms.Button).Tag, String) + " €"}
        Dim item As New ListViewItem(stringa)
        ListView1.Items.Add(item)
 
        Try
 
            Dim CNT1 As Integer = 0
            Dim CNT2 As Integer = 0
            bottone1 = New Button(ListView1.Items.Count * 4) {}
            bottone2 = New ComboBox(ListView1.Items.Count) {}
 
            Dim X As Integer = ListView1.Location.X - 110
            Dim Y As Integer = ListView1.Location.Y + 25
 
            While CNT1 < ListView1.Items.Count * 4
 
 
                bottone1(CNT1) = New Button
                bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
                bottone1(CNT1).Size = New System.Drawing.Size(20, 20)
                bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
                bottone1(CNT1).ForeColor = System.Drawing.Color.Black
                bottone1(CNT1).Text = "+"
                bottone1(CNT1).Tag = CNT2
                AddHandler bottone1(CNT1).Click, AddressOf bottoni
 
                Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
                CNT1 += 1
                X = X + 20
 
                bottone1(CNT1) = New Button
                bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
                bottone1(CNT1).Size = New System.Drawing.Size(20, 20)
                bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
                bottone1(CNT1).ForeColor = System.Drawing.Color.Black
                bottone1(CNT1).Text = "-"
                bottone1(CNT1).Tag = CNT2
                AddHandler bottone1(CNT1).Click, AddressOf bottoni
 
                Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
                CNT1 += 1
                X = X + 20
 
                bottone1(CNT1) = New Button
                bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
                bottone1(CNT1).Size = New System.Drawing.Size(20, 20)
                bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
                bottone1(CNT1).ForeColor = System.Drawing.Color.Black
                bottone1(CNT1).Text = "D"
                'AddHandler bottone1(CNT).Click, AddressOf Button_Click
 
                X = X + 20
 
                bottone2(CNT2) = New ComboBox
                bottone2(CNT2).Location = New System.Drawing.Point(X, Y)
                bottone2(CNT2).Size = New System.Drawing.Size(50, 20)
                'bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
                bottone2(CNT2).ForeColor = System.Drawing.Color.Black
                'For index = 0 To bottone2(CNT2).Items.Count - 1
                'If bottone2(CNT2).Items(index).ToString = CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(0, 5) Then
                'bottone2(CNT2).SelectedIndex = index
                'Exit For
 
                'End If
                'Next
                'bottone2(CNT2).SelectedItem = CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(0, 5)
                bottone2(CNT2).DropDownStyle = ComboBoxStyle.DropDownList
                bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(0).Substring(0, 5))
                bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(1).Substring(0, 5))
                bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(2).Substring(0, 5))
                bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(3).Substring(0, 5))
                bottone2(CNT2).SelectedItem = CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(0, 5)
                bottone2(CNT2).Tag = CNT2
                AddHandler bottone2(CNT2).SelectedValueChanged, AddressOf cambia
 
                X = X + 20
 
                Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
                CNT1 += 1
                Me.SplitContainer1.Panel2.Controls.Add(bottone2(CNT2))
                CNT2 += 1
                X = X + 25 + ListView1.Width + 5
 
                bottone1(CNT1) = New Button
                bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
                bottone1(CNT1).Size = New System.Drawing.Size(40, 20)
                bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
                bottone1(CNT1).ForeColor = System.Drawing.Color.Black
                bottone1(CNT1).Text = "Ingr."
                AddHandler bottone1(CNT1).Click, AddressOf aggiungi
 
 
                Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
                CNT1 += 1
                X = ListView1.Location.X - 110
                Y = Y + 20
 
 
 
            End While
 
 
 
 
 
        Catch ex As Exception
 
        End Try
 
 
 
 
 
    End Sub
```
And this to remove the buttons.


```
Public Sub bottoni(ByVal sender As Button, ByVal e As System.EventArgs)
 
        If sender.Text = "+" Then
            ListView1.Items(Integer.Parse(sender.Tag)).SubItems(0).Text += 1
 
 
        ElseIf sender.Text = "-" Then
            ListView1.Items(Integer.Parse(sender.Tag)).SubItems(0).Text -= 1
 
 
 
            If ListView1.Items(Integer.Parse(sender.Tag)).SubItems(0).Text = 0 Then
 
                ListView1.Items(Integer.Parse(sender.Tag)).Remove()
 
                For index = bottone1.Length - 2 to 0 Step -1
 
                    'RemoveHandler bottone1(index).Click, AddressOf bottoni
                    'SplitContainer1.Panel2.Controls.Remove
                    SplitContainer1.Panel2.Controls.Remove(bottone1(index))
 
 
 
 
 
 
 
 
 
 
                Next
 
                'bottone1(0).Dispose()
 
                For index = 0 To bottone2.Length - 2
 
                    'MsgBox(bottone2.Length)
                    SplitContainer1.Panel2.Controls.Remove(bottone2(index))
 
 
 
 
 
 
 
 
 
 
                Next
 
                'MsgBox(Me.SplitContainer1.Panel2.Controls.Count)
                'Dim stringa() As String
                'Dim z = 0
                Dim stringa1 As String
                For Each Control As Object In Me.SplitContainer1.Panel2.Controls
 
                    'stringa(z) = Control.ToString
 
                    stringa1 += vbNewLine + Control.ToString
 
 
 
 
 
 
                Next
 
                'MsgBox(stringa1)
 
                'MsgBox("")
                'MsgBox(SplitContainer1.Panel2.Controls.Count)
 
 
                Array.Clear(bottone1, 0, bottone1.Count)
                Array.Resize(bottone1, 0)
                Array.Clear(bottone2, 0, bottone2.Count)
                Array.Resize(bottone2, 0)
 
                Try
 
                    Dim CNT1 As Integer = 0
                    Dim CNT2 As Integer = 0
                    bottone1 = New Button(ListView1.Items.Count * 4) {}
                    bottone2 = New ComboBox(ListView1.Items.Count) {}
 
                    Dim X As Integer = ListView1.Location.X - 110
                    Dim Y As Integer = ListView1.Location.Y + 25
 
                    While CNT1 < ListView1.Items.Count * 4
 
 
                        bottone1(CNT1) = New Button
                        bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
                        bottone1(CNT1).Size = New System.Drawing.Size(20, 20)
                        bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
                        bottone1(CNT1).ForeColor = System.Drawing.Color.Black
                        bottone1(CNT1).Text = "+"
                        bottone1(CNT1).Tag = CNT2
                        AddHandler bottone1(CNT1).Click, AddressOf bottoni
 
                        Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
                        CNT1 += 1
                        X = X + 20
 
                        bottone1(CNT1) = New Button
                        bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
                        bottone1(CNT1).Size = New System.Drawing.Size(20, 20)
                        bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
                        bottone1(CNT1).ForeColor = System.Drawing.Color.Black
                        bottone1(CNT1).Text = "-"
                        bottone1(CNT1).Tag = CNT2
                        AddHandler bottone1(CNT1).Click, AddressOf bottoni
 
                        Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
                        CNT1 += 1
                        X = X + 20
 
                        bottone1(CNT1) = New Button
                        bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
                        bottone1(CNT1).Size = New System.Drawing.Size(20, 20)
                        bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
                        bottone1(CNT1).ForeColor = System.Drawing.Color.Black
                        bottone1(CNT1).Text = "D"
                        AddHandler bottone1(CNT1).Click, AddressOf bottoni
 
                        X = X + 20
 
                        bottone2(CNT2) = New ComboBox
                        bottone2(CNT2).Location = New System.Drawing.Point(X, Y)
                        bottone2(CNT2).Size = New System.Drawing.Size(50, 20)
                        'bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
                        bottone2(CNT2).ForeColor = System.Drawing.Color.Black
                        'For index = 0 To bottone2(CNT2).Items.Count - 1
                        'If bottone2(CNT2).Items(index).ToString = CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(0, 5) Then
                        'bottone2(CNT2).SelectedIndex = index
                        'Exit For
 
                        'End If
                        'Next
                        'bottone2(CNT2).SelectedItem = CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(0, 5)
                        bottone2(CNT2).DropDownStyle = ComboBoxStyle.DropDownList
                        bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(0).Substring(0, 5))
                        bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(1).Substring(0, 5))
                        bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(2).Substring(0, 5))
                        bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(3).Substring(0, 5))
                        bottone2(CNT2).SelectedItem = CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(0, 5)
                        bottone2(CNT2).Tag = CNT2
                        AddHandler bottone2(CNT2).SelectedValueChanged, AddressOf cambia
 
                        X = X + 20
 
                        Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
                        CNT1 += 1
                        Me.SplitContainer1.Panel2.Controls.Add(bottone2(CNT2))
                        CNT2 += 1
                        X = X + 25 + ListView1.Width + 5
 
                        bottone1(CNT1) = New Button
                        bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
                        bottone1(CNT1).Size = New System.Drawing.Size(40, 20)
                        bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
                        bottone1(CNT1).ForeColor = System.Drawing.Color.Black
                        bottone1(CNT1).Text = "Ingr."
                        AddHandler bottone1(CNT1).Click, AddressOf aggiungi
 
 
                        Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
                        CNT1 += 1
                        X = ListView1.Location.X - 110
                        Y = Y + 20
 
 
 
                    End While
 
 
 
 
 
                Catch ex As Exception
 
                End Try
 
 
 
 
            End If
 
 
        ElseIf sender.Text = "C" Then
            ListView1.Items(Integer.Parse(sender.Tag)).Remove()
            Controls.Remove(bottone1(0))
 
 
        End If
 
 
 
 
 
 
 
 
 
    End Sub
```
Any help would be appreciated eh okk.
Thanks.
Ciao.
Andrea.


----------

