拖动并复制表单中的按钮

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了拖动并复制表单中的按钮相关的知识,希望对你有一定的参考价值。

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

语言没关系可能是C#或VB.NET

答案

以下是如何为项目实现最后一件事:

First :

在你的表单设计中添加另一个名为(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
另一答案

尝试下面这个,我只是写了,它完美地工作。

添加一个计时器到你的程序(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

希望能帮助你:)

另一答案

在进入代码之前,您需要执行一些步骤,我将使

以上是关于拖动并复制表单中的按钮的主要内容,如果未能解决你的问题,请参考以下文章

HTML代码片段

Android:单击片段中的按钮时如何通知活动? [复制]

Final Cut Pro X中的音视频片段如何自由拖动?

在片段中添加按钮? [复制]

Android 片段复制

如何使用两个提交按钮,并区分使用哪个提交表单? [复制]