How can I close a user generated button?

Here's the code for the button that creates new user generated buttons:

    Public Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        TC = TC + 1
        Dim TCP As New Integer
        TCP = TC * 100
        Dim NT As New MyButton
        NT.Anchor = AnchorStyles.Top And AnchorStyles.Left
        NT.Enabled = True
        NT.Location = New Point(TCP, 85)
        NT.Size = New Size(100, 30)
        NT.Visible = True
        Controls.Add(NT)
    End Sub

MyButton is a custom button I had created.

You will have to set the name of the button, or loop through the forms controls:

NT.Name="MyButton1"

Or

For Each m As MyButton in Me.Controls
    m.Dispose 'NOTE: m MUST INHERIT CONTROL
Next

Well your situation is a bit complicated since I find it harder to remove the created button so I then decided to add a timer to force the control to remove the UGB so you can do it like I did below in the codes hope it will answer your question.

Public Class Form1
    Dim TC As Integer
    Dim NT As New Button
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim TCP As New Integer

        If Button1.Text = "Create" Then
            TC = TC + 1
            TCP = TC * 100
            NT.Anchor = AnchorStyles.Top And AnchorStyles.Left
            NT.Enabled = True
            NT.Location = New Point(TCP, 85)
            NT.Size = New Size(100, 30)
            NT.Visible = True
            Controls.Add(NT)
            Button1.Text = "Close"
            Timer1.Stop()
        Else
            If Button1.Text = "Close" Then
                Button1.Text = "Create"
                Timer1.Start()
            End If
        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button1.Text = "Create"
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Controls.Remove(NT)
    End Sub
End Class

5adf64a05033ba44af4afe7e61dc620b

So that's the form and I want to close the last pressed button before the close button.

I'm not clear with your question but I think you want to close the selected button off which was the button that you created using the codes you posted above am I right? If so then please see this you will then need a way to trace your buttons because in this case I had a visual buttons on my interface ruther than you in which it will be created an run mode.

Yes I want to close the selected button. I'll take a look at the link until then I'll keep this discussion open. If I find it helpful I'll mark it as solved.

Ok so I've created another button which should close the main button and itself of course. The problem is that I don't know how to code the close feature.

    Dim TC As Integer = -1
    Dim NT As New Tab01
    Dim NTC As New Close
    Dim BtnName As String
    Dim NameCount As Integer = 0
    Dim NameCountString As String

    Public Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        TC = TC + 1
        Dim TCP As New Integer
        Dim TCCP As New Integer
        TCP = TC * 130
        TCCP = TCP + 100

        NT.Anchor = AnchorStyles.Top And AnchorStyles.Left
        NT.Enabled = True
        NT.Location = New Point(TCP, 85)
        NT.Size = New Size(100, 30)
        NT.Visible = True
        NT.Text = "Tab"

        NTC.Anchor = AnchorStyles.Top And AnchorStyles.Left
        NTC.Enabled = True
        NTC.Location = New Point(TCCP, 85)
        NTC.Size = New Size(30, 30)
        NTC.Visible = True
        NTC.Text = "x"

        NameCount = NameCount + 1
        NameCountString = NameCount.ToString
        BtnName = "Close" + NameCountString
        NTC.Name = BtnName

        Controls.Add(NT)
        Controls.Add(NTC)
    End Sub

If your custom buttons inherit from Windows.Forms.Button then use the Enabled property. It's a boolean. Set it to False to disable the button. If you also want it to disappear set the visible property to False.

Thanks but how can I apply the code to the generated button. That's my main problem.

Start with a class level button declared but not used. When you create your buttons and add them to the controls collection. Add a handler for the click event. In the click event set the class level button to equal the button that was clicked(ClickedButton = DirectCast(sender, Button)). Now when you want to disable the button ClickedButton will have all the properties of the last button that was clicked. The real beauty is that the assignment is by reference so any changes to ClickedButton are reflected in the button on your form. I've never tried it but you might even be able to remove it completely from the controls collection

Edited 3 Years Ago by tinstaafl

This question has already been answered. Start a new discussion instead.