Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want creat a button in my windows form in visual basic 2010 ,i want to create a button for my field works prosses programing ,and i try for it but when i want remove a stipuleted button my code is removing last created buttan .and i can not undrestand how to add adnew button handler and its rising please

What I have tried:

VB
Public Class ButtonArray
    Inherits System.Collections.CollectionBase
    Private ReadOnly HostForm As System.Windows.Forms.Form

    Public Function AddNewButton() As System.Windows.Forms.Button
        ' Create a new instance of the Button class.
        Dim aButton As New System.Windows.Forms.Button()
        ' Add the button to the collection's internal list.
        Me.List.Add(aButton)
        ' Add the button to the controls collection of the form 
        ' referenced by the HostForm field.
        HostForm.Controls.Add(aButton)
        ' Set intial properties for the button object.
        aButton.Top = Count * 20
        aButton.Left = 100
        aButton.Tag = Me.Count
        aButton.Text = "Button " & Me.Count.ToString
        Return aButton
    End Function
    Public Sub New(ByVal host As System.Windows.Forms.Form)
        HostForm = host
        Me.AddNewButton()

    End Sub
    Default Public ReadOnly Property Item(ByVal Index As Integer) As  _
   System.Windows.Forms.Button
        Get
            Return CType(Me.List.Item(Index), System.Windows.Forms.Button)
        End Get

    End Property
    Public Sub Remove()
        ' Check to be sure there is a button to remove.
        If Me.Count > 0 Then
            ' Remove the last button added to the array from the host form 
            ' controls collection. Note the use of the default property in 
            ' accessing the array.
            HostForm.Controls.Remove(Me(Me.Count - 1))
            Me.List.RemoveAt(Me.Count - 1)
        End If
    End Sub
   
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As  _
   System.EventArgs)
        MessageBox.Show("AddHandler aButton.Click, AddressOf ClickHandler " & CType(CType(sender,  _
           System.Windows.Forms.Button).Tag, String))

        'AddHandler aButton.Click, AddressOf ClickHandle

    End Sub
 End Class

Public Class CFSSRV
    Dim MyControlArray As ButtonArray
    Private ReadOnly HostForm As System.Windows.Forms.Form

    Private Sub CFSSRV_QueryAccessibilityHelp(ByVal sender As Object, ByVal e As System.Windows.Forms.QueryAccessibilityHelpEventArgs) Handles Me.QueryAccessibilityHelp
        Me.Location = New Point(0, 135)
        Me.Size = New Size(602, 533)
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        ' Call the AddNewButton method of MyControlArray.
        ' MyControlArray.AddNewButton()
        MyControlArray.AddNewButton.Text = TRUCK_NO.Text

        ' Change the BackColor property of the Button 0. 
        MyControlArray(0).BackColor = System.Drawing.Color.Red
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        ' Visual Basic
        ' Call the Remove method of MyControlArray.
        MyControlArray.Remove()
        MyControlArray(0).BackColor = System.Drawing.Color.Red
    End Sub

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        MyControlArray = New ButtonArray(Me)
        ' Add any initialization after the InitializeComponent() call.
    End Sub
End Class
Posted
Updated 4-Jul-17 3:23am
v2

1 solution

You remove the last button, because you catch it from the list. If you want to delete the created button you should store it as a member for deletion. And than set the value to Null.

Found a nice example for an Eventhandler on Buttons with Google.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900