468,527 Members | 2,120 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,527 developers. It's quick & easy.

Remove Button Collection Panel

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

Expand|Select|Wrap|Line Numbers
  1.  Private Sub Button_Click(ByVal sender As Object, ByVal e As  _
  2.                              System.EventArgs)
  3.  
  4.         ListBox2.Items.Add(CType(CType(sender,  _
  5.           System.Windows.Forms.Button).Text, String))
  6.         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) + " "}
  7.         Dim item As New ListViewItem(stringa)
  8.         ListView1.Items.Add(item)
  9.  
  10.         Try
  11.  
  12.             Dim CNT1 As Integer = 0
  13.             Dim CNT2 As Integer = 0
  14.             bottone1 = New Button(ListView1.Items.Count * 4) {}
  15.             bottone2 = New ComboBox(ListView1.Items.Count) {}
  16.  
  17.             Dim X As Integer = ListView1.Location.X - 110
  18.             Dim Y As Integer = ListView1.Location.Y + 25
  19.  
  20.             While CNT1 < ListView1.Items.Count * 4
  21.  
  22.  
  23.                 bottone1(CNT1) = New Button
  24.                 bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
  25.                 bottone1(CNT1).Size = New System.Drawing.Size(20, 20)
  26.                 bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
  27.                 bottone1(CNT1).ForeColor = System.Drawing.Color.Black
  28.                 bottone1(CNT1).Text = "+"
  29.                 bottone1(CNT1).Tag = CNT2
  30.                 AddHandler bottone1(CNT1).Click, AddressOf bottoni
  31.  
  32.                 Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
  33.                 CNT1 += 1
  34.                 X = X + 20
  35.  
  36.                 bottone1(CNT1) = New Button
  37.                 bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
  38.                 bottone1(CNT1).Size = New System.Drawing.Size(20, 20)
  39.                 bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
  40.                 bottone1(CNT1).ForeColor = System.Drawing.Color.Black
  41.                 bottone1(CNT1).Text = "-"
  42.                 bottone1(CNT1).Tag = CNT2
  43.                 AddHandler bottone1(CNT1).Click, AddressOf bottoni
  44.  
  45.                 Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
  46.                 CNT1 += 1
  47.                 X = X + 20
  48.  
  49.                 bottone1(CNT1) = New Button
  50.                 bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
  51.                 bottone1(CNT1).Size = New System.Drawing.Size(20, 20)
  52.                 bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
  53.                 bottone1(CNT1).ForeColor = System.Drawing.Color.Black
  54.                 bottone1(CNT1).Text = "D"
  55.                 'AddHandler bottone1(CNT).Click, AddressOf Button_Click
  56.  
  57.                 X = X + 20
  58.  
  59.                 bottone2(CNT2) = New ComboBox
  60.                 bottone2(CNT2).Location = New System.Drawing.Point(X, Y)
  61.                 bottone2(CNT2).Size = New System.Drawing.Size(50, 20)
  62.                 'bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
  63.                 bottone2(CNT2).ForeColor = System.Drawing.Color.Black
  64.                 'For index = 0 To bottone2(CNT2).Items.Count - 1
  65.                 'If bottone2(CNT2).Items(index).ToString = CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(0, 5) Then
  66.                 'bottone2(CNT2).SelectedIndex = index
  67.                 'Exit For
  68.  
  69.                 'End If
  70.                 'Next
  71.                 'bottone2(CNT2).SelectedItem = CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(0, 5)
  72.                 bottone2(CNT2).DropDownStyle = ComboBoxStyle.DropDownList
  73.                 bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(0).Substring(0, 5))
  74.                 bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(1).Substring(0, 5))
  75.                 bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(2).Substring(0, 5))
  76.                 bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(3).Substring(0, 5))
  77.                 bottone2(CNT2).SelectedItem = CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(0, 5)
  78.                 bottone2(CNT2).Tag = CNT2
  79.                 AddHandler bottone2(CNT2).SelectedValueChanged, AddressOf cambia
  80.  
  81.                 X = X + 20
  82.  
  83.                 Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
  84.                 CNT1 += 1
  85.                 Me.SplitContainer1.Panel2.Controls.Add(bottone2(CNT2))
  86.                 CNT2 += 1
  87.                 X = X + 25 + ListView1.Width + 5
  88.  
  89.                 bottone1(CNT1) = New Button
  90.                 bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
  91.                 bottone1(CNT1).Size = New System.Drawing.Size(40, 20)
  92.                 bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
  93.                 bottone1(CNT1).ForeColor = System.Drawing.Color.Black
  94.                 bottone1(CNT1).Text = "Ingr."
  95.                 AddHandler bottone1(CNT1).Click, AddressOf aggiungi
  96.  
  97.  
  98.                 Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
  99.                 CNT1 += 1
  100.                 X = ListView1.Location.X - 110
  101.                 Y = Y + 20
  102.  
  103.  
  104.  
  105.             End While
  106.  
  107.  
  108.  
  109.  
  110.  
  111.         Catch ex As Exception
  112.  
  113.         End Try
  114.  
  115.  
  116.  
  117.  
  118.  
  119.     End Sub
  120.  
And this to remove the buttons.

Expand|Select|Wrap|Line Numbers
  1. Public Sub bottoni(ByVal sender As Button, ByVal e As System.EventArgs)
  2.  
  3.         If sender.Text = "+" Then
  4.             ListView1.Items(Integer.Parse(sender.Tag)).SubItems(0).Text += 1
  5.  
  6.  
  7.         ElseIf sender.Text = "-" Then
  8.             ListView1.Items(Integer.Parse(sender.Tag)).SubItems(0).Text -= 1
  9.  
  10.  
  11.  
  12.             If ListView1.Items(Integer.Parse(sender.Tag)).SubItems(0).Text = 0 Then
  13.  
  14.                 ListView1.Items(Integer.Parse(sender.Tag)).Remove()
  15.  
  16.                 For index = bottone1.Length - 2 to 0 Step -1
  17.  
  18.                     'RemoveHandler bottone1(index).Click, AddressOf bottoni
  19.                     'SplitContainer1.Panel2.Controls.Remove
  20.                     SplitContainer1.Panel2.Controls.Remove(bottone1(index))
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.                 Next
  32.  
  33.                 'bottone1(0).Dispose()
  34.  
  35.                 For index = 0 To bottone2.Length - 2
  36.  
  37.                     'MsgBox(bottone2.Length)
  38.                     SplitContainer1.Panel2.Controls.Remove(bottone2(index))
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.                 Next
  50.  
  51.                 'MsgBox(Me.SplitContainer1.Panel2.Controls.Count)
  52.                 'Dim stringa() As String
  53.                 'Dim z = 0
  54.                 Dim stringa1 As String
  55.                 For Each Control As Object In Me.SplitContainer1.Panel2.Controls
  56.  
  57.                     'stringa(z) = Control.ToString
  58.  
  59.                     stringa1 += vbNewLine + Control.ToString
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.                 Next
  67.  
  68.                 'MsgBox(stringa1)
  69.  
  70.                 'MsgBox("")
  71.                 'MsgBox(SplitContainer1.Panel2.Controls.Count)
  72.  
  73.  
  74.                 Array.Clear(bottone1, 0, bottone1.Count)
  75.                 Array.Resize(bottone1, 0)
  76.                 Array.Clear(bottone2, 0, bottone2.Count)
  77.                 Array.Resize(bottone2, 0)
  78.  
  79.                 Try
  80.  
  81.                     Dim CNT1 As Integer = 0
  82.                     Dim CNT2 As Integer = 0
  83.                     bottone1 = New Button(ListView1.Items.Count * 4) {}
  84.                     bottone2 = New ComboBox(ListView1.Items.Count) {}
  85.  
  86.                     Dim X As Integer = ListView1.Location.X - 110
  87.                     Dim Y As Integer = ListView1.Location.Y + 25
  88.  
  89.                     While CNT1 < ListView1.Items.Count * 4
  90.  
  91.  
  92.                         bottone1(CNT1) = New Button
  93.                         bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
  94.                         bottone1(CNT1).Size = New System.Drawing.Size(20, 20)
  95.                         bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
  96.                         bottone1(CNT1).ForeColor = System.Drawing.Color.Black
  97.                         bottone1(CNT1).Text = "+"
  98.                         bottone1(CNT1).Tag = CNT2
  99.                         AddHandler bottone1(CNT1).Click, AddressOf bottoni
  100.  
  101.                         Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
  102.                         CNT1 += 1
  103.                         X = X + 20
  104.  
  105.                         bottone1(CNT1) = New Button
  106.                         bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
  107.                         bottone1(CNT1).Size = New System.Drawing.Size(20, 20)
  108.                         bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
  109.                         bottone1(CNT1).ForeColor = System.Drawing.Color.Black
  110.                         bottone1(CNT1).Text = "-"
  111.                         bottone1(CNT1).Tag = CNT2
  112.                         AddHandler bottone1(CNT1).Click, AddressOf bottoni
  113.  
  114.                         Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
  115.                         CNT1 += 1
  116.                         X = X + 20
  117.  
  118.                         bottone1(CNT1) = New Button
  119.                         bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
  120.                         bottone1(CNT1).Size = New System.Drawing.Size(20, 20)
  121.                         bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
  122.                         bottone1(CNT1).ForeColor = System.Drawing.Color.Black
  123.                         bottone1(CNT1).Text = "D"
  124.                         AddHandler bottone1(CNT1).Click, AddressOf bottoni
  125.  
  126.                         X = X + 20
  127.  
  128.                         bottone2(CNT2) = New ComboBox
  129.                         bottone2(CNT2).Location = New System.Drawing.Point(X, Y)
  130.                         bottone2(CNT2).Size = New System.Drawing.Size(50, 20)
  131.                         'bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
  132.                         bottone2(CNT2).ForeColor = System.Drawing.Color.Black
  133.                         'For index = 0 To bottone2(CNT2).Items.Count - 1
  134.                         'If bottone2(CNT2).Items(index).ToString = CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(0, 5) Then
  135.                         'bottone2(CNT2).SelectedIndex = index
  136.                         'Exit For
  137.  
  138.                         'End If
  139.                         'Next
  140.                         'bottone2(CNT2).SelectedItem = CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(0, 5)
  141.                         bottone2(CNT2).DropDownStyle = ComboBoxStyle.DropDownList
  142.                         bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(0).Substring(0, 5))
  143.                         bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(1).Substring(0, 5))
  144.                         bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(2).Substring(0, 5))
  145.                         bottone2(CNT2).Items.Add(FormAggiungi.ComboBox1.Items(3).Substring(0, 5))
  146.                         bottone2(CNT2).SelectedItem = CType(CType(sender, System.Windows.Forms.Button).Text, String).Substring(0, 5)
  147.                         bottone2(CNT2).Tag = CNT2
  148.                         AddHandler bottone2(CNT2).SelectedValueChanged, AddressOf cambia
  149.  
  150.                         X = X + 20
  151.  
  152.                         Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
  153.                         CNT1 += 1
  154.                         Me.SplitContainer1.Panel2.Controls.Add(bottone2(CNT2))
  155.                         CNT2 += 1
  156.                         X = X + 25 + ListView1.Width + 5
  157.  
  158.                         bottone1(CNT1) = New Button
  159.                         bottone1(CNT1).Location = New System.Drawing.Point(X, Y)
  160.                         bottone1(CNT1).Size = New System.Drawing.Size(40, 20)
  161.                         bottone1(CNT1).TextAlign = ContentAlignment.MiddleCenter
  162.                         bottone1(CNT1).ForeColor = System.Drawing.Color.Black
  163.                         bottone1(CNT1).Text = "Ingr."
  164.                         AddHandler bottone1(CNT1).Click, AddressOf aggiungi
  165.  
  166.  
  167.                         Me.SplitContainer1.Panel2.Controls.Add(bottone1(CNT1))
  168.                         CNT1 += 1
  169.                         X = ListView1.Location.X - 110
  170.                         Y = Y + 20
  171.  
  172.  
  173.  
  174.                     End While
  175.  
  176.  
  177.  
  178.  
  179.  
  180.                 Catch ex As Exception
  181.  
  182.                 End Try
  183.  
  184.  
  185.  
  186.  
  187.             End If
  188.  
  189.  
  190.         ElseIf sender.Text = "C" Then
  191.             ListView1.Items(Integer.Parse(sender.Tag)).Remove()
  192.             Controls.Remove(bottone1(0))
  193.  
  194.  
  195.         End If
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.     End Sub
Any help would be appreciated eh okk.
Thanks.
Ciao.
Andrea.
Sep 17 '10 #1
0 1067

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by JohnSouth | last post: by
2 posts views Thread by Ramamoorthy Ramasamy | last post: by
9 posts views Thread by David | last post: by
2 posts views Thread by Patrick | last post: by
3 posts views Thread by aryan24 | last post: by
4 posts views Thread by =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.