Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove Dynamic Controls

Status
Not open for further replies.

Trainingjason

Instructor
Dec 6, 2001
127
GB
Hi

I have successfully created dynamic buttons with events, how do I now remove a button I have created

any help appreciated

jason
 
me.controls.remove() ??

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Christiaan

I have this code to create the a series of buttons, I have named a button addnewbutton. I want to create a button to delete a specified created control.



Inherits System.Windows.Forms.Form
Private intCounter As Int16
Private intPos As Int16
Private Sub Button_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click, Button2.Click
Dim buttonClicked As Button
buttonClicked = CType(sender, Button)
' Tell the world what button was clicked
MessageBox.Show("You clicked " & buttonClicked.Text)
End Sub


Private Sub addNewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addNewButton.Click
intCounter += 1
intPos += 30
Dim newButton As Button

' Create the new control
newButton = New Button

' Set it up on the form
newButton.Location = New System.Drawing.Point(184, 16 + intPos)
newButton.Size = New System.Drawing.Size(75, 23)
newButton.Text = "Button" & intCounter
newButton.Tag = intCounter

' Add it to the form's controls collection
Me.Controls.Add(newButton)

' Hook up the event handler
AddHandler newButton.Click, AddressOf Me.Button_Click

End Sub

Thanks

Jason
 
I have done controlled arrays and here is my code to delete a row:

(pmintRowCounter is my public counter of rows)
Code:
    Private Sub CmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim intIndex, intx As Integer

        intIndex = sender.tag       '** the control array index is stored in the Tag property

        '** Move each of the Rows up and delete last one
        If (intIndex + 1) <= pintRowCounter Then
            For intx = (intIndex + 1) To pintRowCounter
                poptDefault(intx - 1).Checked = poptDefault(intx).Checked
                ptxtBatch(intx - 1).Text = ptxtBatch(intx).Text
                pCboUpLength(intx - 1).Text = pCboUpLength(intx).Text
                ptxtRollMax(intx - 1).Text = ptxtRollMax(intx).Text
            Next
        End If

        poptDefault(pintRowCounter).Dispose()
        ptxtBatch(pintRowCounter).Dispose()
        pCboUpLength(pintRowCounter).Dispose()
        ptxtRollMax(pintRowCounter).Dispose()
        pcmdRemove(pintRowCounter).Dispose()
        pintRowCounter -= 1

    End Sub
 
this is what I came up with

Code:
Private intCounter As Int16
    Private intPos As Int16
    Private Buttonclicked As Button

    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        buttonClicked = CType(sender, Button)
        ' Tell the world what button was clicked
        MessageBox.Show("You clicked " & Buttonclicked.Name)
    End Sub


    Private Sub addNewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addnewbutton.Click
        intCounter += 1
        intPos += 30
        Dim newButton As Button

        ' Create the new control
        newButton = New Button

        ' Set it up on the form
        newButton.Location = New System.Drawing.Point(184, 16 + intPos)
        newButton.Size = New System.Drawing.Size(75, 23)
        newButton.Text = "Button" & intCounter
        newButton.Tag = intCounter
        newButton.Name = "Button" & intCounter

        ' Add it to the form's controls collection
        Me.Controls.Add(newButton)

        ' Hook up the event handler
        AddHandler newButton.Click, AddressOf Me.Button_Click

    End Sub

    Private Sub Deletenewbutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Deletenewbutton.Click
        Me.Deletenewbutton.Focus()
        Me.Controls.Remove(Buttonclicked)
    End Sub

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Chrissie,

Does that move the rows up?

On mine, if they deleted row 3, I have to move rows 4 to n up so it was the last row being removed (or appeared that way)

Thanks!!
Becca
 
Nope, but that wasn't the question.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Hi

I have rewritten the code, so I can delete the last button, but I can only get your method working if I click the button first to be deleted, and then press the delete button. I have also moved buttonclicked to private.

Private intCounter As Int16
Private intPos As Int16
Private buttonClicked As Button
Private Sub Button_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click, Button2.Click

buttonClicked = CType(sender, Button)
' Tell the world what button was clicked
MessageBox.Show("You clicked " & buttonClicked.Text)
End Sub


Private Sub addNewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addNewButton.Click
intCounter += 1
intPos += 30
Dim newButton As Button

' Create the new control
newButton = New Button

' Set it up on the form
newButton.Location = New System.Drawing.Point(184, 16 + intPos)
newButton.Size = New System.Drawing.Size(75, 23)
newButton.Text = "Button" & intCounter
newButton.Tag = intCounter

' Add it to the form's controls collection
Me.Controls.Add(newButton)

' Hook up the event handler
AddHandler newButton.Click, AddressOf Me.Button_Click

End Sub

Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
Dim ctl As Control
For Each ctl In Me.Controls
If TypeOf ctl Is Button And ctl.Tag = intCounter Then
ctl.Hide()
intCounter -= 1
End If
Next
End Sub


Private Sub btnDeleteNewButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteNewButton.Click
Me.btnDeleteNewButton.Focus()
Me.Controls.Remove(Buttonclicked)

End Sub
 
Christaan

I did not read your response properly, you have moved button click up. Basically I want an inputbox to say what button do you want to remove. Then remove it.

Regards



Jason
 
since they all have names now that shouldn't be a problem.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Christiann

Perhaps it is late but I tried the following and it does not work

Dim ctl As Control

Dim strRemove As String
Dim bytNum As Byte
bytNum = InputBox("Enter a number to remove")
For Each ctl In Me.Controls
If TypeOf ctl Is Button And ctl.Tag = bytNum Then
strRemove = ctl + bytNum
Me.Controls.Remove(strRemove)

End If
Next
End Sub


Regards



Jason
 
Thanks chaps got a partial solution in the end using the tag, could not get the code to work without clicking the button.


Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
Dim bytNum As Byte
bytNum = InputBox("Enter number", , 2)
Dim ctl As Control
Try
For Each ctl In Me.Controls
If TypeOf ctl Is Button And ctl.Tag = bytNum Then
ctl.Dispose()
'intCounter -= 1
End If
Next
Catch
Exit Sub
End Try


End Sub
 
One last point to click and remove the button itself

Private Sub Button_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click, Button2.Click

buttonClicked = CType(sender, Button)
' Tell the world what button was clicked
'MessageBox.Show("You clicked " & buttonClicked.Text)
buttonClicked.Dispose()
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top