Question Delete button

dentfree

New member
Joined
Feb 28, 2012
Messages
3
Programming Experience
Beginner
I create many buttons in code like :
Code:
    Public Sub GenereTextBox(ByVal rep As String)
        Dim txt As New TextBox
        Dim fnt As Font

        fnt = Me.Font
        txt.Location = New Drawing.Point(26 + NbreCacher * (68), txtOrdonnee)
        txt.Font = New Font(fnt.Name, 40, FontStyle.Regular)
        txt.Size = New Size(130, 37)
        txt.Multiline = True
        txt.Name = "textb" + Str((NbreEssai - 1) * 4)
        Me.Controls.Add(txt)
        txt.Text = rep
        txtOrdonnee = txtOrdonnee + 40
        txt.ReadOnly = True
    End Sub
And I want delete some buttons crete when I click on my button, but i don't how i can do, because these buttons are create in code??
 

ProtekNickz

Well-known member
Joined
Oct 22, 2009
Messages
117
Location
UK
Programming Experience
1-3
I'm Not sure if you can actully delete a button but you can make it go invisible like so

Code:
bntNamedButton.Visible = False ' Or True to which desired effect your after
Hope that helps :)


Code:
did a bit of sniffing and found this

Control c = this.Controls.Find(yourButtonControl.Name, true)[0];
this.Controls.Remove(c)

Which i C++ but easily changed :)
 

Gunzedreng

Member
Joined
Feb 20, 2012
Messages
9
Location
Denmark
Programming Experience
1-3
I'll try your code in vb.net the next time I need to do this ProtekNickz ;) Much less work :)

I use normally use something like this, hope it helps :)



Public Sub RemoveTextBox(ByVal TextboxName As String)

For Each txtbox As TextBox In Controls
If txtbox.Name = TextboxName Then
Controls.Remove(txtbox)
Exit For
End If
Next

End Sub
Info: 'TextboxName = "textb" + Str((NbreEssai - 1) * 4)


Public Sub RemoveButton(ByVal ButtonName As String)

For Each btn As Button In Controls
If btn.Name = ButtonName Then
Controls.Remove(btn)
Exit For
End If
Next

End Sub
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,295
Location
Sydney, Australia
Programming Experience
10+
As you're giving the Buttons names, you can get them back by that name from the Controls collection you added them to. To destroy them, Remove them from the collection and call their Dispose method.
 
Top Bottom