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?
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?