none
How to implement design of sprites from one form to another?

    Question

  • Morning all this is a process that I want to carry out in my main section of my program. (Something of important significance)

    In English: when I select a specific item within the combo box such as "war factory" I can drag it from the picturebox in one form and drop it anywhere on the game board that I have designed and it will show a msg to indicate deployment is success and purchase success (when I do the dragdrop the sprite will fit into 1 square. 

    Here are some screenshots of what I basically have done successfully and the code for it and what my intentions are for the code.

    The first two below shows that I can rag and drop a picture from one picturebox to another form picturebox. 

    Public Class Form1
        Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
            PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Copy)
        End Sub
    
        Private Sub PictureBox2_DragDrop(sender As Object, e As DragEventArgs) Handles PictureBox2.DragDrop
            PictureBox2.Image = e.Data.GetData(DataFormats.Bitmap)
        End Sub
    
        Private Sub PictureBox2_DragEnter(sender As Object, e As DragEventArgs) Handles PictureBox2.DragEnter
    
            If e.Data.GetDataPresent(DataFormats.Bitmap) Then 'If the thing you are copying is text
            e.Effect = DragDropEffects.Copy 'Copy cursor
            Else
                e.Effect = DragDropEffects.None 'If not text leave cursor as is
            End If
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox2.AllowDrop = True
            Me.Show()
            Form2.Show()
        End Sub
    End Class

    Public Class Form2
    
        Private Sub PictureBox3_DragEnter(sender As Object, e As DragEventArgs) Handles PictureBox3.DragEnter
    
            If e.Data.GetDataPresent(DataFormats.Bitmap) Then 'If the thing you are copying is text
                e.Effect = DragDropEffects.Copy 'Copy cursor
            Else
                e.Effect = DragDropEffects.None 'If not text leave cursor as is
            End If
        End Sub
    
        Private Sub PictureBox3_DragDrop(sender As Object, e As DragEventArgs) Handles PictureBox3.DragDrop
            PictureBox3.Image = e.Data.GetData(DataFormats.Bitmap)
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox3.AllowDrop = True
        End Sub
    End Class

    Here is the code which does it if anyone wants insight. 

    Then the second screenshots show how I would like to carry it out for my main form. 

    If you think there are better suggestions to this idea let me know this might be the hard way round what I plan on doing is when I select the multicolumn combobox tanks and select light panzer it will show an image in the picturebox when I select light scorcher tank it will show an image of that and same applies to the building combobox



    WRA


    Tuesday, April 18, 2017 2:04 PM

Answers

  • Hi Wali,

    What is your question exactly?

    It appears you have the proper code methods to drag and drop when done correctly?

    Use the methods Acamar has described to write down each step the code needs to do for each step the user does. Then add the code one line at a time checking to make sure that line works as intended.

    When the object is dropped on your picture grid you need to get the drop point location coordinates (that will be pixels). Then create a temp sprite object. Create a picturebox control for the sprite and ref it from the sprite object .pic property. Assign the other properties to the temp sprite like location (however you locate things). Add the temp sprite to the list of game sprites.

    Redraw the map.

    So its not the drag and dropping its the making the sprite that you need to do. Is that your question?

    Making this sprite is basically the same as the other sprites you made for your game in form load. Just do the same thing and adjust for this exact method. Even make a sub routine GetNewSprite(),

    Show the code you have for the drop and make sprite that you have done if you have specific questions about that.

    • Marked as answer by Waliur Rahman Wednesday, April 19, 2017 8:15 PM
    Tuesday, April 18, 2017 3:35 PM

All replies

  • Morning all this is a process that I want to carry out in my main section of my program. (Something of important significance)

    In English: when I select a specific item within the combo box such as "war factory" I can drag it from the picturebox in one form and drop it anywhere on the game board that I have designed and it will show a msg to indicate deployment is success and purchase success (when I do the dragdrop the sprite will fit into 1 square. 

    Here are some screenshots of what I basically have done successfully and the code for it and what my intentions are for the code.

    The first two below shows that I can rag and drop a picture from one picturebox to another form picturebox. 

    Public Class Form1
        Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
            PictureBox1.DoDragDrop(PictureBox1.Image, DragDropEffects.Copy)
        End Sub
    
        Private Sub PictureBox2_DragDrop(sender As Object, e As DragEventArgs) Handles PictureBox2.DragDrop
            PictureBox2.Image = e.Data.GetData(DataFormats.Bitmap)
        End Sub
    
        Private Sub PictureBox2_DragEnter(sender As Object, e As DragEventArgs) Handles PictureBox2.DragEnter
    
            If e.Data.GetDataPresent(DataFormats.Bitmap) Then 'If the thing you are copying is text
            e.Effect = DragDropEffects.Copy 'Copy cursor
            Else
                e.Effect = DragDropEffects.None 'If not text leave cursor as is
            End If
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox2.AllowDrop = True
            Me.Show()
            Form2.Show()
        End Sub
    End Class


    Public Class Form2
    
        Private Sub PictureBox3_DragEnter(sender As Object, e As DragEventArgs) Handles PictureBox3.DragEnter
    
            If e.Data.GetDataPresent(DataFormats.Bitmap) Then 'If the thing you are copying is text
                e.Effect = DragDropEffects.Copy 'Copy cursor
            Else
                e.Effect = DragDropEffects.None 'If not text leave cursor as is
            End If
        End Sub
    
        Private Sub PictureBox3_DragDrop(sender As Object, e As DragEventArgs) Handles PictureBox3.DragDrop
            PictureBox3.Image = e.Data.GetData(DataFormats.Bitmap)
        End Sub
    
        Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox3.AllowDrop = True
        End Sub
    End Class

    Here is the code which does it if anyone wants insight. 

    Then the second screenshots show how I would like to carry it out for my main form. 

    If you think there are better suggestions to this idea let me know this might be the hard way round what I plan on doing is when I select the multicolumn combobox tanks and select light panzer it will show an image in the picturebox when I select light scorcher tank it will show an image of that and same applies to the building combobox


    WRA

    Thursday, April 13, 2017 10:30 AM
  • Hi Wali,

    What is your question exactly?

    It appears you have the proper code methods to drag and drop when done correctly?

    Use the methods Acamar has described to write down each step the code needs to do for each step the user does. Then add the code one line at a time checking to make sure that line works as intended.

    When the object is dropped on your picture grid you need to get the drop point location coordinates (that will be pixels). Then create a temp sprite object. Create a picturebox control for the sprite and ref it from the sprite object .pic property. Assign the other properties to the temp sprite like location (however you locate things). Add the temp sprite to the list of game sprites.

    Redraw the map.

    So its not the drag and dropping its the making the sprite that you need to do. Is that your question?

    Making this sprite is basically the same as the other sprites you made for your game in form load. Just do the same thing and adjust for this exact method. Even make a sub routine GetNewSprite(),

    Show the code you have for the drop and make sprite that you have done if you have specific questions about that.

    • Marked as answer by Waliur Rahman Wednesday, April 19, 2017 8:15 PM
    Tuesday, April 18, 2017 3:35 PM
  • Woah thanks for the ideas tommy so the example is along the lines of what I have to do but need to have few kinks to it. Very last complex issue and I am just excited :) once the game is finished I have to do the write up :( its gonna be long hours and boredom but i will feel the sweet taste of success in the end

    WRA

    Tuesday, April 18, 2017 4:51 PM
  • Quick question tommy you know much about databases and visual basic? Its because I got the part where I connect to the database the next part is updating the winning players medals to their account is what I gotta do

    WRA

    Tuesday, April 18, 2017 4:52 PM
  • Alright I have manage to do the part where I drag the picturebox image from one form to another form picturebox but that isn't really what I should Intend for it to do. 

    Quick question Tommy? Do I need to use anything in the form section "the lighting logo" in the properties section? Such as the dragenter feature except it does it to do windows form.


    WRA

    Wednesday, April 19, 2017 4:12 PM
  • Would you believe tommy this problem was much easier than the one which took me 3 weeks to solve? XD. 

    I manage to implement the tank onto the game grid, get it to move and attack or be attacked. All the characteristics from the original. 

    Thanks you and Acamar you dont know how much help you have been, I stressed that I wouldn't have finished this game in time before the hand in date of my coursework but without you and many others point me in the right direction I have become a better programmer than I was a long time ago. For that I thank you :) 

    Here is a screenshot of it working perfectly: 

    I just gotta do a minor adjustment with the backcolor and it is perfection there. 


    WRA

    Wednesday, April 19, 2017 8:14 PM
  • Thus far this is the least complex issue in a long time I knew you would love this one Tommy. 

    WRA

    Wednesday, April 19, 2017 8:15 PM
  • Thus far this is the least complex issue in a long time I knew you would love this one Tommy. 

    WRA


    You are welcome.


    Quick question tommy you know much about databases and visual basic?

    No. But lots here do.


    Alright I have manage to do the part where I drag the picturebox image from one form to another form picturebox but that isn't really what I should Intend for it to do.

    So you got it working how you wanted?


    Do I need to use anything in the form section "the lighting logo" in the properties section? Such as the dragenter feature except it does it to do windows form

    I dont know what you mean. I think no. Drag enter is the mouse event you use to show the drag pointer.


    I knew you would love this one Tommy

    Again not sure what you mean. One of the pictureboxes has white background is that what you mean?


    Well looks good. Sounds like you are satisfied and have a little bragging rights. But, I think you need to drill the basics.

    :)


    Wednesday, April 19, 2017 11:17 PM
  • Fair enough there are still few adjustments to do such as make sure it can deploy the new troop anywhere on the game board rather than a given for instance, fix the backcolor etc. It will take time but sure I will get my head around it.

    WRA


    Thursday, April 20, 2017 8:49 AM
  • Tommy quick question: What if I wanted to make 2 or more of the same sprites? for example I want to construct more than 2 gold mines for instance?

    This is the code which created my grand cannon: would I need to do something new to produce more copies of the same tank to do one at a time?

    Private Sub Plain_Turf(sender As Object, e As DragEventArgs) Handles MyBase.DragEnter, MyBase.DragDrop
            DoubleBuffered = True
    
            Sprites(4).ColRow = New Point(5, 6)
            Sprites(4).HP = 15
            Sprites(4).AP = 5
            Sprites(4).Visible = 1
    
            If e.Data.GetDataPresent(DataFormats.Bitmap) Then 'If the thing you are copying is text
                e.Effect = DragDropEffects.Copy 'Copy cursor
            Else
                e.Effect = DragDropEffects.None 'If not text leave cursor as is
            End If
    
            Dim pic = New PictureBox()
            With pic
                .Name = "Pic"
                .Size = GridSize
                .BackgroundImageLayout = ImageLayout.Stretch
                Dim ImageFile As String = IO.Path.Combine("C:\Users\BusinessOffice1Room\Documents\Waliur File\A Level\Computer Science\Coursework\Work In progression\Import or Export Images\PngFiles\Game Loadup", "Grand Cannon.png")
                .BackgroundImage = Image.FromFile(ImageFile)
                .BackColor = Color.Transparent
                Me.Controls.Add(pic)
                AddHandler .MouseDown, AddressOf Pics_MouseDown
                AddHandler .MouseDoubleClick, AddressOf Pics_DoubleClick
                AddHandler .MouseMove, AddressOf pics_MouseMove
                AddHandler .MouseUp, AddressOf pics_MouseUp
            End With
            Plain_Turf_Resize(1, Nothing)
        End Sub

    So as stated in my answer as soon as I drag the picturebox from the player1 game grid and onto the game board, that is successfully done. 

    All I need help is with 1) sprites can position anywhere rather than a given location

    2) Make more copies of that sprite to use such as gold mine.


    WRA

    Thursday, April 20, 2017 7:58 PM
  • Create a function and call it when you want to make a tank.

    Here is a non working example. I have no way to test this code but something like this.

    I just put your code in two functions.

    Also note how I made a form level variable to save the image bitmap in and copy that when a tank sprite is make. The image is read once when you start the form. The you should dispose it too but I don't show that.

    So you will have to debug this and insert it in your case exactly. It just shows use of functions to do something over and over.

    Like I say I cant test it and so this wont work as is.

    wait....

    I changed the example to pass the bitmap to the picturebox function. There are lots of ways to do this.

    Public Class Form7
        Private Class sprite
    
        End Class
        Private sprites As New List(Of sprite)
    
        Private TankBmp As New Bitmap(IO.Path.Combine("C:\Users\BusinessOffice1Room\Documents\Waliur File\A Level\Computer Science\Coursework\Work In progression\Import or Export Images\PngFiles\Game Loadup", "Grand Cannon.png"))
    
    
        Private Function GetTankSprite(thisName As String) As sprite
    
            Dim tank As sprite
            tank.Name = thisName
            tank.pic = GetNewPictureBox(thisName & " Picturebox", TankBmp)
            tank.ColRow = New Point(5, 6)
            tank.HP = 15
            tank.AP = 5
            tank.Visible = 1
    
            Return tank
        End Function
    
        Private Function GetNewPictureBox(thisName As String, thisBmp As Bitmap) As PictureBox
            Dim pic = New PictureBox()
            With pic
                .Name = thisName
                .Size = GridSize
                .BackgroundImageLayout = ImageLayout.Stretch
                .BackgroundImage = thisBmp.Clone
                .BackColor = Color.Transparent
                Me.Controls.Add(pic)
                AddHandler .MouseDown, AddressOf Pics_MouseDown
                AddHandler .MouseDoubleClick, AddressOf Pics_DoubleClick
                AddHandler .MouseMove, AddressOf pics_MouseMove
                AddHandler .MouseUp, AddressOf pics_MouseUp
            End With
    
            Return pic
        End Function
    
        Private Sub Form7_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            Dim tank1 As sprite = GetTankSprite("Tank 1")
            sprites.add(tank1)
    
            Dim tank2 As sprite = GetTankSprite("Tank 2")
            sprites.add(tank2)
    
        End Sub
    End Class

    Thursday, April 20, 2017 8:47 PM
  • What if I wanted to make 2 or more of the same sprites? for example I want to construct more than 2 gold mines for instance?

    You might want to re-think your design.  Although drag/drop usually drags and drops a particular object (such as a selected portion of text) there is no rule that says it must be so.   For your design it appears that it may be more effective if your drag drop object is actually a code to indicate what needs to be created when it is dropped.  You would need to change the code for when the drag is initiated, to look at user options currently selected in the form dragged from, as well as the object being dragged, and create an appropriate code that indicated the required drop action.  That code (which might be a custom drag drop object, or might just be a string) is then confirmed at the drop location and the required action taken.  So if the code said the equivalent of 'panzer tank' then you know to create a panzer sprite (and everything associated with that) at the drop location.  Your code will indicate things like the object to be created, its battle condition (it might not always be fully armed) and, most importantly, which player it belongs to.

    Thursday, April 20, 2017 10:42 PM
  • I came across something that I needed to sort out which is for when I do the part where the actual tank Implements, I just want it to implement that tank only. 

    So for example I want this set of code: 

        Private Sub Plain_Turf_G_Cannon(sender As Object, e As DragEventArgs) Handles MyBase.DragEnter, MyBase.DragDrop
            DoubleBuffered = True
            Sprites(4).ColRow = New Point(3, 6)
            Sprites(4).HP = 15
            Sprites(4).AP = 5
            Sprites(4).Visible = 2
    
            If e.Data.GetDataPresent(DataFormats.Bitmap) Then 'If the thing you are copying is text
                e.Effect = DragDropEffects.Copy 'Copy cursor
            Else
                e.Effect = DragDropEffects.None 'If not text leave cursor as is
            End If
    
            Dim pic = New PictureBox()
            With pic
                .Name = "Pic"
                .Size = GridSize
                .BackgroundImageLayout = ImageLayout.Stretch
                Dim ImageFile As String = IO.Path.Combine("C:\Users\BusinessOffice1Room\Documents\Waliur File\A Level\Computer Science\Coursework\Work In progression\Import or Export Images\PngFiles\Game Loadup\Grand Cannon Blue.PNG")
                .BackgroundImage = Image.FromFile(ImageFile)
                .BackColor = Color.Transparent
                Me.Controls.Add(pic)
                AddHandler .MouseDown, AddressOf Pics_MouseDown
                AddHandler .MouseDoubleClick, AddressOf Pics_DoubleClick
                AddHandler .MouseMove, AddressOf pics_MouseMove
                AddHandler .MouseUp, AddressOf pics_MouseUp
            End With
            Plain_Turf_Resize(1, Nothing)
        End Sub

    Just to drag drop only the grand cannon but the problem here is It implements another turret called plasma turret so I want to make them do a bit at a time basically. 

    In other words if cannon is dragged it is dropped into the game board alone and same applies for the plasma turret. Not the two together. 


    WRA

    Saturday, April 22, 2017 3:43 PM
  • In other words if cannon is dragged it is dropped into the game board alone and same applies for the plasma turret. Not the two together.

    That's what I said above.  You need to set the dragdrop information at the start of the drag drop operation so that the drag drop object indicates what is being dragged and dropped, and you need to look at that information when it gets dropped to see what you need to create.

    Saturday, April 22, 2017 9:29 PM
  • I see, thanks for pointing it out for me Acamar. I get it now the order of which the code executes is very important. I believe that will solve my problem and I am good to go and will be able to finish the rest of my write up for coursework.

    WRA


    Sunday, April 23, 2017 9:31 AM