dynamically add and remove controls?

Jacobier

New member
Joined
Nov 9, 2013
Messages
2
Programming Experience
Beginner
I have search internet for quit a while, but can't find the solution for my coding.
What I want to do is I first add 3 buttons on the form with coding. If I press the first button, all the 3 buttons will be removed and another 3 new buttons will be added:


Here is my code, I don't know where do I make mistake:



Public Class FrMain


Public WithEvents newButton As Windows.Forms.Button
Dim ManualFlag As Integer ' 1.Welcome manual 2. Starting point manual
Private Sub FrMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


'Manual for starting
Me.Show()
Me.Focus()
WelcomManual()
ManualFlag = 1
End Sub




Private Sub FrMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown


LbMouseLocate.Text = "X=" & e.X & " " & "Y=" & e.Y


End Sub


Private Sub WelcomManual() 'Welcome manual



newButton = New Windows.Forms.Button
newButton.Name = "BtStart"
newButton.Text = "Start "
newButton.Top = 100
newButton.Left = 300
newButton.Width = 150
newButton.Height = 50
AddHandler newButton.Click, AddressOf ButtonClicked
Me.Controls.Add(newButton)


newButton = New Windows.Forms.Button
newButton.Name = "BtLoad"
newButton.Text = "Load"
newButton.Top = 160
newButton.Left = 300
newButton.Width = 150
newButton.Height = 50
AddHandler newButton.Click, AddressOf ButtonClicked
Me.Controls.Add(newButton)



newButton = New Windows.Forms.Button
newButton.Name = "BtExit"
newButton.Text = "Exit "
newButton.Top = 220
newButton.Left = 300
newButton.Width = 150
newButton.Height = 50
AddHandler newButton.Click, AddressOf ButtonClicked
Me.Controls.Add(newButton)






End Sub


Private Sub ButtonClicked(ByVal sender As Object, ByVal e As System.EventArgs)


InitializeMain()
StartingPointmanual()
ManualFlag = 2
End Sub




Private Sub InitializeMain()


For Each con In Me.Controls


Me.Controls.Remove(con)


Next


For Each con In newButton.Controls
RemoveHandler newButton.Click, AddressOf ButtonClicked
newButton.Controls.Remove(con)
Next
newButton.Dispose()
End Sub
Private Sub StartingPointmanual()
newButton = New Windows.Forms.Button
newButton.Name = "BtEvent1"
newButton.Text = "Charter 1"
newButton.Top = 100
newButton.Left = 300
newButton.Width = 150
newButton.Height = 50
AddHandler newButton.Click, AddressOf ButtonClicked
Me.Controls.Add(newButton)




newButton = New Windows.Forms.Button
newButton.Name = "BtEvent2"
newButton.Text = "Charter 2 "
newButton.Top = 160
newButton.Left = 300
newButton.Width = 150
newButton.Height = 50
AddHandler newButton.Click, AddressOf ButtonClicked
Me.Controls.Add(newButton)




newButton = New Windows.Forms.Button
newButton.Name = "BtEven3"
newButton.Text = "Charter 3"
newButton.Top = 220
newButton.Left = 300
newButton.Width = 150
newButton.Height = 50
AddHandler newButton.Click, AddressOf ButtonClicked
Me.Controls.Add(newButton)





End Sub






End Class



What I got was: When I pressed the first button all the first set of buttons remained there, and if I pressed the second button, the first set buttons start to turn into second set buttons. What is most wired is after I deleted the code for adding the second set buttons. I saw the same phenomenon. Did I need to release something?
 
I think you are going about this the wrong way... Why do you need 6 buttons anyways? From what you were saying, only 3 will ever be shown together. In addition, all you are really doing is showing/hiding them. Just lay your buttons in the designer and change their .Visible property, no point in creating and destroying buttons at runtime. Better yet, add a panel to your form, put three buttons on it, then select the panel, copy/paste it, and position it right on top of the other one. Buttons 1, 2, and 3 on top, 4, 5, and 6 underneath. Then add this code to the form:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Panel1.Visible = False
    End Sub


I just did what all your code above attempted to do in one line of code.
 
Last edited:
Back
Top