I have a form. Inside a form there is a button1. I want to drag the button1 anywhere inside the form and duplicate it when it drops, but the codes in the button1 is still there.
Language doesn't matter may be C# or VB.NET
I have a form. Inside a form there is a button1. I want to drag the button1 anywhere inside the form and duplicate it when it drops, but the codes in the button1 is still there.
Language doesn't matter may be C# or VB.NET
Try this below, i'v just wrote and it worked perfectly.
Add a timer to your program (Timer1
) and then see the code below, i'v also added notes to it to explain everything :
Public Class Form1
Dim XLoc, YLoc As Integer
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
'Save the current location of 'button1' in its tag before moving it.
Button1.Tag = Button1.Location
'Get the exact location of the cursor on the 'button1'.
XLoc = (Cursor.Position.X - Left - 8) - Button1.Location.X
YLoc = (Cursor.Position.Y - Top - 30) - Button1.Location.Y
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Move the button while holding down the mouse button.
Button1.Location = New Point(Cursor.Position.X - Left - 8 - XLoc, Cursor.Position.Y - Top - 30 - YLoc)
Timer1.Start()
End Sub
Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
'Stop the movement and create a new button with the same location as 'button1'.
Timer1.Stop()
Dim NewBUT As New Button
NewBUT.Parent = Me
NewBUT.Size = New Size(75, 23)
NewBUT.Text = Button1.Text
NewBUT.Location = Button1.Location
'Return 'button1' to its original location.
Button1.Location = Button1.Tag
End Sub
End Class
To make all the created buttons share the same code, you can do this :
1. Create a function and put all the code you want all the buttons to do when anyone of them is pressed :
Private Sub ButtonClicked()
'Paste here the code.
End Sub
2. When duplicating the button in the old code above, you need to add this line :
AddHandler NewBUT.Click, AddressOf ButtonClicked
So now duplicating the button is like this :
Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
'Stop the movement and create a new button with the same location as 'button1'.
Timer1.Stop()
Dim NewBUT As New Button
NewBUT.Parent = Me
NewBUT.Size = New Size(75, 23)
NewBUT.Text = Button1.Text
NewBUT.Location = Button1.Location
AddHandler NewBUT.Click, AddressOf ButtonClicked
'Return 'button1' to its original location.
Button1.Location = Button1.Tag
End Sub
Hope the will help you :)
Before getting into the code, you need to do some steps, and i will show you these steps using pictures so it will be easier on you.
1.Add an item in the 'Settings' tab to store the locations of the duplicated buttons :
2.The code, this is the whole code for the whole form, i tried to explain as much as i can using comments in the code but if you needed anything, feel free to ask:
Public Class Form1
Dim XLoc, YLoc, CreateButtonX As Integer
Dim CreatedButtons As String()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.CreatedButtons <> "" Then
'Split the string (CreatedButtons) in the Settings with the char "|" as a separator and loop through all the pats when each part is a different location for a duplicated button.
CreatedButtons = My.Settings.CreatedButtons.Split("|")
Dim Separator As String = "|"
For Each Separator In CreatedButtons
Dim NewBUT As New Button
NewBUT.Parent = Me
NewBUT.Size = New Size(75, 23)
NewBUT.Text = "Button 1"
Try
XLoc = CreatedButtons(CreateButtonX).Remove(CreatedButtons(CreateButtonX).IndexOf(","))
YLoc = CreatedButtons(CreateButtonX).Substring(CreatedButtons(CreateButtonX).IndexOf(",") + 1)
NewBUT.Location = New Point(XLoc, YLoc)
Catch : End Try
AddHandler NewBUT.Click, AddressOf ButtonClicked
CreateButtonX += 1
Next
End If
Timer1.Interval = 1
End Sub
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
'Save the current location of 'button1' in its tag before moving it.
Button1.Tag = Button1.Location
'Get the exact location of the cursor on the 'button1'.
XLoc = (Cursor.Position.X - Left - 8) - Button1.Location.X
YLoc = (Cursor.Position.Y - Top - 30) - Button1.Location.Y
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Move the button while holding down the mouse button.
Button1.Location = New Point(Cursor.Position.X - Left - 8 - XLoc, Cursor.Position.Y - Top - 30 - YLoc)
Timer1.Start()
End Sub
Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
'Stop the movement and create a new button with the same location as 'button1'.
Timer1.Stop()
'Create the new button.
Dim NewBUT As New Button
NewBUT.Parent = Me
NewBUT.Size = New Size(75, 23)
NewBUT.Text = Button1.Text
NewBUT.Location = Button1.Location
'Store the location of the duplicated button in the shape of a string array in the Settings with the char "|" as a separator.
If My.Settings.CreatedButtons = "" Then
My.Settings.CreatedButtons &= NewBUT.Location.X & "," & NewBUT.Location.Y
Else
My.Settings.CreatedButtons &= "|" & NewBUT.Location.X & "," & NewBUT.Location.Y
End If
My.Settings.Save()
'Add a handler to the duplicated button.
AddHandler NewBUT.Click, AddressOf ButtonClicked
'Return 'button1' to its original location.
Button1.Location = Button1.Tag
End Sub
Private Sub ButtonClicked() Handles Button1.Click
Timer1.Stop() ' THIS IS IMPORTANT!
'Paste your code that you want all the buttons to handel instead of this next line.
MsgBox("Button clicked!")
End Sub
End Class
Hope that helped you, please make sure to vote it up, so i know it did ;)
Here is how to implement you last thing to your project :
Add another timer to your form design called(Timer2
), this time is to handle the movement of the created buttons where the first timer (Timer1
) was to handle the movement of the main first button (the once we are duplicating from).
Now this is how your whole code should look like, I explained as much as i can in the code, and if you didn't understand a thing, please ask... :
Public Class Form1
Dim XLoc, YLoc, CreateButtonX As Integer
Dim CreatedButtons As String()
Dim DragedButtonName As String
Dim ButtonsCount As Integer = 1
Dim NewBUT As Button
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.CreatedButtons <> "" Then
'Split the string (CreatedButtons) in the Settings with the char "|" as a separator and loop through all the pats when each part is a different location for a duplicated button.
CreatedButtons = My.Settings.CreatedButtons.Split("|")
Dim Separator As String = "|"
For Each Separator In CreatedButtons
CreateNewButton()
Try
Dim Pos1 As Integer = CreatedButtons(CreateButtonX).IndexOf(":")
Dim Pos2 As Integer = CreatedButtons(CreateButtonX).IndexOf(",")
XLoc = CreatedButtons(CreateButtonX).Substring(Pos1 + 1, Pos2 - Pos1)
YLoc = CreatedButtons(CreateButtonX).Substring(Pos2 + 1)
NewBUT.Location = New Point(XLoc, YLoc)
Catch : End Try
AddHandler NewBUT.MouseDown, AddressOf CreatedButtons_Click
AddHandler NewBUT.MouseDown, AddressOf CreatedButtons_MouseDown
AddHandler NewBUT.MouseUp, AddressOf CreatedButtons_MouseUp
CreateButtonX += 1
Next
End If
Timer1.Interval = 1
End Sub
Private Sub CreateNewButton()
NewBUT = New Button
NewBUT.Name = "NewBUT" & ButtonsCount + 1
NewBUT.Parent = Me
NewBUT.Size = New Size(150, 23)
NewBUT.Text = "New created button"
ButtonsCount += 1
End Sub
Private Sub Button1_MouseDown(sender As Object, e As MouseEventArgs) Handles Button1.MouseDown
'Save the current location of 'button1' in its tag before moving it.
Button1.Tag = Button1.Location
'Get the exact location of the cursor on the 'button1'.
XLoc = (Cursor.Position.X - Left - 8) - Button1.Location.X
YLoc = (Cursor.Position.Y - Top - 30) - Button1.Location.Y
Timer1.Start()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'Move the button while holding down the mouse button.
Button1.Location = New Point(Cursor.Position.X - Left - 8 - XLoc, Cursor.Position.Y - Top - 30 - YLoc)
Timer1.Start()
End Sub
Private Sub Button1_MouseUp(sender As Object, e As MouseEventArgs) Handles Button1.MouseUp
'Stop the movement and create a new button with the same location as 'button1'.
Timer1.Stop()
'Create the new button.
CreateNewButton()
NewBUT.Location = Button1.Location
'Store the location of the duplicated button in the shape of a string array in the Settings with the char "|" as a separator.
If My.Settings.CreatedButtons = "" Then
My.Settings.CreatedButtons &= NewBUT.Name & ":" & NewBUT.Location.X & "," & NewBUT.Location.Y
Else
My.Settings.CreatedButtons &= "|" & NewBUT.Name & ":" & NewBUT.Location.X & "," & NewBUT.Location.Y
End If
My.Settings.Save()
'Add handlers to the duplicated button.
AddHandler NewBUT.MouseDown, AddressOf CreatedButtons_Click
AddHandler NewBUT.MouseDown, AddressOf CreatedButtons_MouseDown
AddHandler NewBUT.MouseUp, AddressOf CreatedButtons_MouseUp
'Return 'button1' to its original location.
Button1.Location = Button1.Tag
End Sub
Private Sub CreatedButtons_Click()
'Your code here when the user presses a created button.
End Sub
Private Sub CreatedButtons_MouseDown()
'Get the exact location of the cursor on the 'clicked button'.
XLoc = (Cursor.Position.X - Left - 8) - ActiveControl.Location.X
YLoc = (Cursor.Position.Y - Top - 30) - ActiveControl.Location.Y
DragedButtonName = ActiveControl.Name
Timer2.Start()
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
Controls.Item(DragedButtonName).Location = New Point(Cursor.Position.X - Left - 8 - XLoc, Cursor.Position.Y - Top - 30 - YLoc)
Timer2.Start()
End Sub
Private Sub CreatedButtons_MouseUp()
Timer2.Stop()
'Update the new location of the button based on its name
Dim SelectedButtonPosition As Integer = My.Settings.CreatedButtons.IndexOf(ActiveControl.Name)
Dim SplitSettingsPart1 As String = My.Settings.CreatedButtons.Remove(SelectedButtonPosition)
Dim SplitSettingsPart2 As String = My.Settings.CreatedButtons.Substring(SelectedButtonPosition)
Dim SplitSettingsPart3 As String
If SplitSettingsPart2.Contains("|") Then
SplitSettingsPart3 = SplitSettingsPart2.Substring(SplitSettingsPart2.IndexOf("|"))
End If
SplitSettingsPart2 = SplitSettingsPart2.Remove(SplitSettingsPart2.IndexOf(":"))
SplitSettingsPart2 &= ":" & ActiveControl.Location.X & "," & ActiveControl.Location.Y
My.Settings.CreatedButtons = SplitSettingsPart1 & SplitSettingsPart2 & SplitSettingsPart3
My.Settings.Save()
End Sub
End Class