拖动并复制表单中的按钮

drag and duplicate a button in a form

我有一个表格。窗体内部有一个 button1。 我想将 button1 拖到表单内的任何位置并在它掉落时复制它,但 button1 中的代码仍然存在。

语言无关紧要可能是 C# 或 VB.NET

试试下面这个,我刚刚写的,效果很好。

在您的程序中添加一个计时器 (Timer1),然后查看下面的代码,我还向其中添加了注释以解释所有内容:

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

要使所有创建的按钮共享相同的代码,您可以这样做:

1. 创建一个函数并放入您希望所有按钮在按下任何按钮时执行的所有代码:

Private Sub ButtonClicked()
    'Paste here the code.
End Sub

2.复制上面旧代码中的按钮时,需要添加这一行:

AddHandler NewBUT.Click, AddressOf ButtonClicked

所以现在复制按钮是这样的:

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

希望对您有所帮助:)

在进入代码之前,你需要做一些步骤,我会用图片向你展示这些步骤,这样你会更容易。

1.Add 'Settings' 选项卡中的一个项目,用于存储重复按钮的位置:

2.The 代码,这是整个表单的全部代码,我尽量在代码中使用注释来解释,但如果您需要有什么事,尽管问:

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

希望对您有所帮助,请务必投票,所以我知道它做到了 ;)

以下是如何在项目中实施最后一件事:

第一个:

在您的表单设计中添加另一个计时器(Timer2),这次是处理创建的按钮的移动,而第一个计时器 (Timer1) 是处理按钮的移动主要的第一个按钮(我们从中复制的一次)。

现在你的整个代码应该是这样的,我在代码中尽可能多地解释了,如果你不明白什么,请问...:

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