created array of buttons.. now how can I desroy them!!!

kyleolson

Newcomer
Joined
Sep 9, 2008
Messages
2
I have created an array of buttons, and need to event off them. Problem is I need the event to destroy the buttons and then recreate some new ones. I dont know how to destroy all the buttons that I have. Right now I am creating a duplicate form, but dont think that is really a good way to do it. :-( How can I just call a destruction of the buttons? HEEEEELP
---------
Visual Basic:
Public Class frmBarSales


    Private Sub frmBarSales_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lstItems.Items.Clear()
        lstItemsID.Items.Clear()

        'lstItems.Items.Add("test KYLE")
        'lstItemsID.Items.Add("1")
        'lstItems.Items.Add("TEST 2")
        'lstItemsID.Items.Add("2")

        Dim aButton As New System.Windows.Forms.Button

        Dim oSQLConn As SqlConnection = New SqlConnection()
        Dim dr As SqlDataReader
        Dim strButtonName As String

        oSQLConn.ConnectionString = _
            "Data Source=xxxxxx;" & _
            "Initial Catalog=xxxxxx;" & _
            "User ID=xxxxxx;" & _
            "Password=12345"
        Dim strSQL As String
        'Get Items..
        strSQL = "SELECT * FROM categories where sub_id = " & intCategoryID
        oSQLConn.Open()

        Dim Buttons(99) As Button
        Dim cmd As New SqlCommand(strSQL, oSQLConn)
        dr = cmd.ExecuteReader()
        Dim x As Integer = 0
        Dim LV As Integer
        LV = 0
        Do While dr.Read

            strButtonName = dr.Item("description")
            Buttons(LV) = New Button 'Create the button.
            'It has a default width and height.
            Buttons(LV).Width = 166
            Buttons(LV).Height = 166
            Buttons(LV).Left = Buttons(LV).Width * (LV Mod 10) + 200
            Buttons(LV).Top = ((Buttons(LV).Height * (LV \ 10) + 100))
            Buttons(LV).Name = "Btn" & LV
            Buttons(LV).Text = strButtonName
            Buttons(LV).Tag = dr.Item("id")


            AddHandler Buttons(LV).Click, AddressOf ButtonClickEventProcStart
            Me.Controls.Add(Buttons(LV))
            LV = LV + 1
        Loop


        oSQLConn.Close()
    End Sub

    Private Sub btRemoveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btRemoveItem.Click

        Dim intIndexofItem As Integer
        intIndexofItem = lstItems.SelectedIndex
        lstItems.Items.RemoveAt(intIndexofItem)
        lstItemsID.Items.RemoveAt(intIndexofItem)


    End Sub

    Public Sub ButtonClickEventProcStart(ByVal sender As Object, ByVal e As EventArgs)
        Dim Btn As Button = DirectCast(sender, Button)
        intCategoryID = Btn.Tag
        'Me.Close()
        'Dim frm As New frmBarSales
        'frm.Show()
    End Sub

End Class
 
Last edited by a moderator:
When you want to destroy the buttons, simply remove them from the container (Form.Controls.Remove) and dispose of them (Button.Dispose).
 
Back
Top