0

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

floyd
  • 13
  • 3

3 Answers3

0

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 :)

Mousa Alfhaily
  • 1,260
  • 3
  • 20
  • 38
  • @floyd , You'er welcome :), please accept the answer if it really helped you so it can help others. – Mousa Alfhaily Sep 18 '17 at 15:25
  • if i duplicate the button can the data inside the button also can be copied? – floyd Sep 19 '17 at 03:10
  • You mean the text inside the button? – Mousa Alfhaily Sep 19 '17 at 04:16
  • yes the text , designs, and codes inside the button. Is that even possible? Could you help me.? – floyd Sep 25 '17 at 03:49
  • @floyd , Yes, you can do that by adding the line (`NewBUT.Text = Button1.Text`), i edited my answer, check it :) – Mousa Alfhaily Sep 25 '17 at 07:04
  • Can the codes inside the button be also copied to the new duplicated button? For Example: If the Original have an if else or try and catch code inside the button will it be copied to the duplicated button? Please help me. – floyd Sep 27 '17 at 03:45
  • @floyd , yes, you can use [AddHandler](http://msdn.microsoft.com/en-us/library/7taxzxka%28v=VS.100%29.aspx) to add a handler for any event, i'm going to edit my answer to show you how, please don't forget to accept the answer if it helped you. – Mousa Alfhaily Sep 27 '17 at 06:33
  • can you help me in another problem. This is the link: https://stackoverflow.com/questions/46439217/how-to-connect-database-to-report-or-report-wizard-in-visual-basic-2012 Please help me – floyd Sep 28 '17 at 03:54
  • @floyd , Sorry i don't know how to help you with that one. – Mousa Alfhaily Sep 28 '17 at 04:37
  • it's okay. Thank you – floyd Sep 28 '17 at 05:41
  • Can i save the position of the button and also the button to the database so that when i open the form i can retrieve the position from the database and put it in the button.? – floyd Sep 29 '17 at 05:37
  • So you want to re-create all the buttons that you duplicated when you re-open the form? – Mousa Alfhaily Sep 29 '17 at 08:48
  • @floyd This is a bit complicated because there is a unknown number of buttons, so they could be 10 or 100 or whatever, but yes this will still be possible and you can do that, – Mousa Alfhaily Sep 29 '17 at 09:53
  • could you help me on how to do this. You are my only hope. Please. – floyd Sep 29 '17 at 13:32
  • @floyd It's okay, im going to post another answer for that because it will be very big for the above. – Mousa Alfhaily Sep 29 '17 at 17:28
  • @Mouda you are my guardian angel. – floyd Sep 29 '17 at 22:12
  • please help me again. Here it is: In a form there are multiple buttons which was placed those are (button1, button2..... and so on) i want to drag and drop those buttons to the place in the form where i want to. I don't want to duplicate those buttons but rather just move its position just by drag and drop. And if i will close the form and open it again. Its position will still be the same from the last time I closed it. Please help. It's the same from the last time but these time i don't want to duplicate the buttons. Please help @Mousa – floyd Dec 29 '17 at 08:01
  • @floyd I'm gonna add another 3rd answer with what you want above and all the old program included, is that good for you? Please let me know, also please consider voting up the my answer if they was useful :) – Mousa Alfhaily Dec 29 '17 at 12:08
  • Please help me. This is the last thing needed to finish my system. Please. – floyd Dec 30 '17 at 03:09
  • should i add the latest code to the last line of the last code or should i replace the old code with a new code? – floyd Jan 06 '18 at 02:28
  • @floyd Replace every thing from the old code with the new one... hope it helped. – Mousa Alfhaily Jan 06 '18 at 07:16
  • there's an error at the 'next' line after the CreateButtonX +=1. It says 'Object reference not set to an instance of an object' – floyd Jan 08 '18 at 23:08
  • @floyd Are you using the newest code in my last answer? Please make sure you add the next line : Dim CreateButtonX As Integer = 0 Please comment on my last answer so i know what code you are referring to. – Mousa Alfhaily Jan 08 '18 at 23:18
  • please help me again. hihi. this is much simplier code than the last two or three.: I put already some buttons in the form. e.g. i want those buttons to drag and drop wherever i want.i dont want to drag and copy them. just normal drag and drop without duplication. and when i close and open the form again i will see the last place where the buttons went. – floyd Jan 09 '18 at 01:31
  • this is the example screenshot of what the form looks like. https://www.datafilehost.com/d/fc280e8c i want these three buttons to transfer wherever i want – floyd Jan 09 '18 at 01:35
  • Please help me. if you don't understand my question just ask me to clarify it. – floyd Jan 09 '18 at 14:34
  • Hello @floyd this here should work will all the functions with 3 buttons as you want, here is the code : [https://pastebin.com/zCP1G1ir](https://pastebin.com/zCP1G1ir), i hope it is clear enough. – Mousa Alfhaily Jan 09 '18 at 19:27
  • what if I will add 50 more buttons? is that okay? – floyd Jan 09 '18 at 22:06
  • hello @mousa 50 buttons. is that okay? I will do the code just tell me what should I do. – floyd Jan 09 '18 at 22:09
  • @floyd yes that is okay, you can add as much as you want, just compare the old code with the new one to know how ;) – Mousa Alfhaily Jan 09 '18 at 22:16
  • Thanks @Mousa I'll try. I will message here if anything goes wrong. – floyd Jan 12 '18 at 13:02
  • Hi @Mousa Looks like my explanation to you on how it really work was wrong so i created a PDF explanation. Please help me. Please. here's the link. just download it. https://www.datafilehost.com/d/06528d42 . again please mr. mousa help me. – floyd Jan 14 '18 at 13:53
  • Okay, but you asked for a duplication functionality at first! I will update my answer to what you're requesting in the PDF file and inform you about it. – Mousa Alfhaily Jan 14 '18 at 18:14
  • @floyd Here it is, as you want, first : Add these to your application `Settings` tab, it should look like [this image](https://imgur.com/a/Tq1Il), and here is the code, i commented some note, hope you are understanding everything : [the code here](https://pastebin.com/ZZeXg2u6). – Mousa Alfhaily Jan 17 '18 at 13:55
  • Thanks @mousa it really work. but i have a problem now. How can i compare two dates in visual basic? i cant find any good answer in Google. – floyd Feb 22 '18 at 15:17
  • You're welcome @floyd :) ..., comparing dates is pretty simple, you can check this answer here : [https://stackoverflow.com/questions/618878/how-to-compare-just-the-date-part-and-not-the-time-of-two-dates](https://stackoverflow.com/questions/618878/how-to-compare-just-the-date-part-and-not-the-time-of-two-dates) – Mousa Alfhaily Feb 24 '18 at 16:12
  • hello @Mousa can you help me again? I have a problem regarding in my print. I can't connect my database to the dataset this is the error when i'm attempting to connect it https://www.datafilehost.com/d/1065fef5 . By the way I am using Vertigo for my database. Please help. – floyd Mar 16 '18 at 05:47
  • @floyd So sorry, but i don't know how to help you with that :( – Mousa Alfhaily Mar 16 '18 at 13:47
  • it's okay @mousa – floyd Mar 17 '18 at 09:54
0

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 :

enter image description here enter image description here

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 ;)

Mousa Alfhaily
  • 1,260
  • 3
  • 20
  • 38
0

Here is how to implement you last thing to your project :

First :

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
Community
  • 1
  • 1
Mousa Alfhaily
  • 1,260
  • 3
  • 20
  • 38