我有一张表格。在表单内有一个button1。 我想将button1拖到窗体内的任何位置,并在它掉落时复制它,但button1中的代码仍然存在。
语言不重要可能是C#或VB.NET
答案 0 :(得分:0)
请尝试以下操作,我刚刚写完并且效果很好。
为您的计划添加计时器(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 :(得分:0)
在进入代码之前,您需要执行一些步骤,我将使用图片向您展示这些步骤,以便您更轻松。
1.在“设置”标签中添加项目以存储重复按钮的位置:
2.代码,这是整个表单的整个代码,我试着尽可能多地解释代码中的注释但是如果你需要什么,请随意问:
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
希望对你有所帮助,请确保投票,所以我知道确实如此;)
答案 2 :(得分:0)
以下是如何实现项目的最后一件事:
在您的表单设计中添加另一个名为(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