Problems adding controls at runtime

Raddy

Member
Joined
Nov 17, 2006
Messages
5
Programming Experience
1-3
I'm writing a program that will be a form with a button on it. When I click on the button, I want the old button to disappear and be replaced with a new one in it's place. So far, only the button will disappear. I've tried copying a the code straight out of the book, but still having problems.

VB.NET:
 Dim btnTouch As New Button()

    Private Sub btnStop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStop.Click
        btnStop.Visible = False
        Button1.Visible = False
        btnTouch = New Button()
        btnTouch.Location = New Point(152, 104)
        btnTouch.Size = New Size(180, 23)
        btnTouch.Visible = True
        btnTouch.text = "New Button"
    End Sub
I've also tried replacing the variable statement at the top with:
VB.NET:
 Dim WithEvents btnTouch as Button()
I had seen this in other posts, still no results. Suggestions?
 
You never actually added the new button to the form. Try the following, note the "Me.Controls.Add" part.

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] btnTouch [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Button()
btnTouch.Location = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Point(152, 104)
btnTouch.Size = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Size(180, 23)
btnTouch.Text = [/SIZE][SIZE=2][COLOR=#800000]"New Button"
[/COLOR][/SIZE][SIZE=2]btnTouch.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].Controls.Add(btnTouch)
[/SIZE]
 
Back
Top