Page 1 of 2 12 LastLast
Results 1 to 40 of 48

Thread: [RESOLVED] Moving a picture box

  1. #1

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Resolved [RESOLVED] Moving a picture box

    I have an array of picture boxes on my form. Some elements of the array are empty. That is a picture box with no picture. I want to move one of the pictures, with the mouse, to an empty box. The pace it came from will be an empty picture box. I also want the picture I am moving to snap into the empty picture box. I will worry about the rules to allow the picture to go there.

    Any help would be appreciated.

    Thanks

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Moving a picture box

    Do you want to swap the position of the pictureboxes, or do you just want to swap the picture within the box.
    You can probably just use drag and drop to accomplish this, but I usually do it manually.

    In the first case, you can simply save the position of the picturebox you start to drag, and then drag the picturebox and when it is released do some calculation to determine which box space it was dropped on and do a final Move to move the picturebox to that location, and do another Move on the empty picturebox to move it to the saved position.

    In the second case, you code do the same initially, but when you drop the box and determine which Picturebox it is dropped on, you set the destination picturebox's picture to the current picturebox's picture, clear the current picturebox and put it back where it started.

    Since I assume this is in regards to your card game, perhaps the first method is what you want.
    I can probably put a quick example together of the manual way of doing it.

    In the meantime, perhaps you want to try the code in this post which is a game I started on but never went as far as I liked. It plays a solitaire game called Seahaven, and you can read the details in the post.

    I'll try creating that quick example of the drop and swap that you mentioned and post it here in some number of minutes, hopefully.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    There is no swap needed. The source picbox picture moves, manually, to the empty picbox position. The source picbox becomes empty.

  4. #4

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    Passel, I read the post you referenced. You mention cards.dll and cards32.dll. I do not have either one on my computer. I am running VB6 on WIN10. Even If I had the dll's I don't know how to install, register, and use them.
    Some time ago I tried to install and register another dll and member of the forum strongly suggested not to do it. I am sure he was not referring to the specific dll.

  5. #5
    Frenzied Member
    Join Date
    May 2014
    Location
    Kallithea Attikis, Greece
    Posts
    1,289

    Re: Moving a picture box

    You have a hot spot on the picture box.A hot spot is nothing else but two variables X, Y. So if you have a card of WidthXHeight, you have a Hot Spot at the center, so X= Width/2+Left and Y=Height/2+Top. You place a card if the hot spot of the target picture X1, Y1 is near to X, Y. You use Abs() function and put the subtract and check for the distance : Abs(X1-X)<DistanceX and Abs(Y1-Y)<DistanceY. If the test give True then the place of card end.
    For moving a picture box you have to store the starting difference of the mouse X and Y from the actual Card, and always move that card to the same difference so the hot spot isn't the same as MouseX and MouseY. You have to place in account the starting differences when the mouse click event start the moving.
    Maybe this helps you.
    Last edited by georgekar; Nov 30th, 2020 at 04:20 PM.

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Moving a picture box

    Quote Originally Posted by AccessShell View Post
    Passel, I read the post you referenced. You mention cards.dll and cards32.dll. I do not have either one on my computer. I am running VB6 on WIN10. Even If I had the dll's I don't know how to install, register, and use them.
    Some time ago I tried to install and register another dll and member of the forum strongly suggested not to do it. I am sure he was not referring to the specific dll.
    Yes, I mention them. And what I mention is the fact that newer windows doesn't include them, so the example doesn't use them. It did use them, but the code that used them was commented out and instead the pictureboxes were filled with a background color and a number was written in the picturebox so you have 52 "cards", but they don't look like normal playing cards in the example, they are just two shades of red and two shades of black (the suits) and the number 0 to 12, or 1 to 13 (I don't remember which) for the rank. (p.s. actually I think I printed 1 to 10 and A, Q, K in the picture)

    So, the example can still demonstrate dragging pictureboxes around to play the game, but without the normal card images. If you put card images in the picturebox in another manner, then it will look like a normal card game.

    As is, it still plays like a normal solitaire game, just with crude "card images".

    Anyway, I did create an example off the cuff. It will swap a picturebox with any other picturebox in a grid.
    In your case, if you want to have empty spaces in your grid, you can change the code to set the gridPic array's value to something that will indicate the grid position is empty.
    You would then just check to see if the grid position is empty, and if so, move the card to that grid position and set gridPic array of the position the card was moved from to whatever value you used for an empty position.

    This is just one of many ways your requirement could be done.

    You can still check out the example I referred to in post #2, since it runs without the card dll, and see how it allowed positioning the cards, and dragging multiple cards from one position to another (if you have a run, and the space necessary to move the number of cards). It used a form of linked lists to tie cards together that are added to a common stack position.

    --------

    To use the example code below, start a new project and add a picturebox to the form and set its index in the property window to 0.
    Size the picturebox in the IDE to the size you want to represent a card, and place it in the upper left position of where you want the 4x4 grid of cards in the example to be drawn. Then paste the example code into the form's module and run.

    Then, as I said, you drag the picturebox over another picturebox to swap them. You can change the code to leave empty spaces, rather than swap pictureboxes.
    You can just use what you learn to create something different, or modify it to suit.
    Code:
    Option Explicit
    
    Private Type location
      X As Integer
      Y As Integer
    End Type
    
    Dim gridPos(15) As location
    Dim gridPic(15) As Integer
    
    Dim srcGridIdx As Integer, destGridIdx As Integer
    
    
    Private Sub Form_Load()
      ScaleMode = vbPixels
      
      
      Dim i As Integer
      Dim xinc As Integer
      Dim yinc As Integer
      
      For i = 0 To 15
        gridPic(i) = i  'set grid to pic index
      Next
      
      Dim X As Integer, Y As Integer
      
      With Picture1(0)
        .ScaleMode = vbPixels
        .BackColor = QBColor(Rnd * 16)
        gridPos(0).X = .Left
        gridPos(0).Y = .Top
        .Tag = 0
        
        xinc = Picture1(0).Width + 20
        yinc = Picture1(0).Height + 20
        For i = 1 To 15
          Load Picture1(i)
          Picture1(i).ScaleMode = vbPixels
          Picture1(i).Tag = i 'set tag to grid index
          X = i Mod 4
          Y = i \ 4
          gridPos(i).X = .Left + X * xinc
          gridPos(i).Y = .Top + Y * yinc
          
          Picture1(i).Left = gridPos(i).X
          Picture1(i).Top = gridPos(i).Y
          Picture1(i).BackColor = QBColor(Rnd * 16)
          Picture1(i).Visible = True
        Next
      End With
    End Sub
    
    Private Sub Picture1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
      Picture1(Index).ZOrder  'bring this picture to the top so is dragged over other pics
      
    End Sub
    
    Private Sub Picture1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
      Static lx As Single, ly As Single
      
      If Button = vbLeftButton Then
        With Picture1(Index)
          srcGridIdx = .Tag
          .Move .Left + (X - lx), .Top + (Y - ly)
        End With
      Else
        lx = X: ly = Y
      End If
    End Sub
    
    Private Sub Picture1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
      Dim closestGridIdx As Integer
      Dim Dist As Double
      Dim Dx As Double, Dy As Double
      Dim MinDist As Double
      Dim i As Integer
      Dim px As Single, py As Single
      
      MinDist = 1000# * 1000#
      px = Picture1(Index).Left
      py = Picture1(Index).Top
      
      For i = 0 To 15
        Dx = px - gridPos(i).X: Dx = Dx * Dx
        Dy = py - gridPos(i).Y: Dy = Dy * Dy
        Dist = Dx + Dy
        If Dist < MinDist Then
          MinDist = Dist
          closestGridIdx = i
        End If
      Next
      
      Dim destPic As Integer
      destPic = gridPic(closestGridIdx)
      
      'swap the two picturebox positions
      Picture1(destPic).Move gridPos(srcGridIdx).X, gridPos(srcGridIdx).Y
      Picture1(Index).Move gridPos(closestGridIdx).X, gridPos(closestGridIdx).Y
      
      'update the indexes
      Picture1(destPic).Tag = srcGridIdx
      Picture1(Index).Tag = closestGridIdx
      gridPic(srcGridIdx) = destPic
      gridPic(closestGridIdx) = Index
      
    End Sub
    p.s.s. I tracked down a VB6 version where I copied the card face images into a picturebox, so that bitmap is included in the .frx file of the project. It is the same code as the post #2 example, except the pictureboxes are set to images cut out of the larger picturebox, so you have the expected card images.
    I'll include that version here, but it is a larger download since the card images are included in the project.

    I think how to play the game is explained in the post linked to in post #2 above.
    The comments in the code may also explain it as well, or not.
    Attached Files Attached Files
    Last edited by passel; Nov 30th, 2020 at 05:18 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  7. #7

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    Passel
    I tried to run the code in post 6. I had to comment out "Option Explicit".
    I think gridPos(i) is a table of the top left coordinated of the pic box?
    I think Index is just i?
    I don't know why I can use gridPos in MouseMOve, but not in MouseUP.

    I'm still trying to figure out how 'update the indices work'.

  8. #8

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    I think I need to redefine the problem.

    I have 52 positions. That is slots for 52 pictures. Four positions are empty. I need to move, let’s say the picture in position 32 to the empty position 4. The movement is done manually with the mouse. If I Mouseup near the x,y (top, left) coordinates of position 4 the picture snaps into position 4 place. Otherwise, it snaps back to the position 32 place.

    Right now I have 52 pictureBoxes. 48 have pictures and 4 are empty. Do I move the picture, or do I move the pictureBox?

  9. #9
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Moving a picture box

    if u are making a "game", I would work completely different.
    instead of pictureboxes and arrays, I would instead use 1 picturebox to show all cards, using GDI32 or Direct2D.

    all cards would be rendered with a "game-loop" that will have all the "RECT" of the cards.
    its easy enough to know "when" your mouse is over a card. using the "mouseup/down trigger, u can decide when a card is "hold" and thats when u can move it around, and when u "release", it will "put" the card into the location.

    if you are using "spots", meaning, theres "spots" where the cards can be placed, u have an array of those spots, that when u release the card into any of the spot, calculating if the mouse pointer is inside any of the "spots", it will positioning it inside that.

    to learn to make a game, its best to do it correctly from start, moving pictureboxes will eventually create issues that you would avoid if you learned to make it as a "game" from start.

    I recommend you to learn Direct2D, its fast and will help with DPI and the monitor sync for you.
    Last edited by baka; Nov 30th, 2020 at 11:00 PM.

  10. #10

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    I am trying to write just a card game. No speed is required. No fancy scenery is required. I read up on Direct2D and think this is not needed for a simple card game. I do not have characters running around shooting or building bridges, etc.
    I may be wrong.

  11. #11
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Moving a picture box

    of course its your pick. its doable to use pictureboxes.
    and each picturebox u can click and hold the mouse, to "attach" it to the "mouse".
    using the "x,y" of the picturebox position when you click the mouse, this value u "store" and its used to know how it will follow the mouse.pointer.
    when u release, u do a LOOP to all the card-spots, and if the center.position of the picturebox is within the card-spot, u place it there.
    u can use the API PtInRect that can be useful for this purpose.

  12. #12
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Moving a picture box

    Yes, for a card game, the picturebox method is adequate.
    The code attached to post #6 uses pictureboxes.

    When I converted that code to VB.Net I added code to animate sending free cards home, which moved the pictureboxes part of the way home in a loop. It supported moving multiple freed cards at the same time, starting each with a delayed start time so sequential cards were spaced out, not moved in a clump.

    Using pictureboxes, just looping and moving the multiple pictureboxes each pass provided a nice animation of the cards moving to the various home stacks.
    I then changed that existing code to draw the cards instead of using pictureboxes, as an example of how the existing code could be fairly simply modified to draw the cards rather than use pictureboxes.

    Drawing the cards is much faster than using pictureboxes, so the animation of sending the cards home in a loop became too quick. All the cards just quickly moved in a blur and were home faster than you could appreciate where they started from and how they traveled to the home stack. So, I had to change from using a loop, to using a timer that would regulate the cycle time so you could appreciate the animation of the cards traveling home.

    So, it is definitely faster to draw the cards rather than use pictureboxes, but that is true even if you don't go all the way and use DirectX or other high-end graphic driver library.
    Just using the built in PaintPicture is faster than using pictureboxes, and you can use bitblt or stretch blit for even more speed, and will not be much more complicated than using pictureboxes. Moving to DirectX is a much steeper learning curve, and not really necessary for simple card game animation, if animation is even desired.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  13. #13
    The Idiot
    Join Date
    Dec 2014
    Posts
    2,721

    Re: Moving a picture box

    it all depends what kind of programmer u want to be.
    if this is a one-time-project to create a game, and in the future u will mostly work with tools/utilities, why bother.
    but if this is a start of making games, that will evolve with more and more complex projects I would definitely go directly into any of the game creating engines.
    and to start "easy" is actually what one should do if in the same time learning a new engine. you do 2-in-1 here. and when/if in the future u want to create something more sophisticated u already know the basics.
    otherwise we will get this person, in one year asking for more advice on a more complicated project without knowing anything and the knowledge required for that could be huge compared to a card-game.

  14. #14

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    Passel you are correct. There is a swap. The mouse moving the card is just an illusion for the player. The player "moves the card to the destination" but the code loads the destination with the picture. The code also loads no picture at the source position.

  15. #15
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Moving a picture box

    Quote Originally Posted by AccessShell View Post
    Passel you are correct. There is a swap. The mouse moving the card is just an illusion for the player. The player "moves the card to the destination" but the code loads the destination with the picture. The code also loads no picture at the source position.
    Ok, so this is the second case as described in post #2.

    I've updated the example code from post #6 to do what I described in post #2.
    When the picturebox is "released", it determines which picturebox is closest to the release point and then moves the dragged picturebox back to its original location, so the pictureboxes always remain in their original positions.

    Then the picture in each picturebox is swapped with the image in the other.
    I did it this way as a convenience as I'm not initially loading the .Picture of the pictureboxes with an image, so it is Null.
    Setting AutoRedraw to True establishes an image buffer in memory for the picturebox, so .Image is not null, it is a bitmap filled with the background color of the picturebox.
    When I Print a number in the picturebox, it is updating this memory bitmap and then updating the screen automatically with the .Image bitmap as needed.
    That is why it is called AutoRedraw, the screen image of the picturebox is updated (redrawn) automatically with the content of the .Image buffer when needed.

    But you can only draw in that .Image buffer, you can't set it to a bitmap or picture.
    You can set the .Picture property to a bitmap or picture though. The .Picture context is another memory buffer associated with the picturebox.
    The picturebox can be thought of as having three levels of graphical content (not counting the backcolor which could be considered a fourth, lowest level layer).
    The topmost layer is the screen context, i.e. what you see. If AutoReDraw is false, any drawing you do is done directly to the screen area of the picturebox. That screen area can be wiped out and would need to be redrawn to restore it. If AutoDraw is false, the Paint event is generated when you need to redraw the screen area to restore it.

    The second (lower) layer is the .Image context. If AutoRedraw is True, then your drawing is drawn in this buffer, and the screen is updated from this buffer. If the screen area of the picturebox gets wiped out and needs to be redrawn, you don't get a Paint event. The screen is "AutoRedrawn" by copying the .Image buffer to the screen, so any drawing you've done in the .Image buffer is automatically persistent and you don't have to redraw it yourself.

    The third (lower) layer is the .Picture context. You can load different image types into this context, and the control will then update the .Image context with a screen compatible bitmap image of what is contained in the .Picture context. If you have AutoRedraw set True and you draw things in the picturebox (updating the .Image context, and the screen image is automatically redrawn from that .Image context), if you issue a Cls to clear the picturebox, it just copies the .Picture image to the .Image context to clear your drawing, but still maintain the static image that was loaded in the .Picture context.

    If you have Autoredraw set True, if you issue a Cls statement, the .Picture context is copied to the .Image context (wiping out what was in the .Image buffer before), and the screen is updated from the .Image context.

    So, since the .Image context is a copy of the .Picture context (but as a screen compatible bitmap version of what is in the .Picture context) the example code swaps the images, by copying the .Image context of one Picturebox into the .Picture context of the other. The .Image context of that picturebox is then updated automatically from the .Picture context, so the .Image is now the same (a clone) of the .Image of the source Picturebox.

    Thus, the swap is done by copying the .Image of the destination into a temporary stdpicture object, then copying the .Image of the source Picturebox to the .Picture context of the destination, and then copying the temporary picture object to the .Picture of the source Picturebox. The .Image of both are updated when the .Picture is updated, so is a bitmap copy of the .Picture unless you draw on it.

    Same requirements as before, start a new project, add a picturebox and set its index to 0, then paste in the code (replacing any code you may have).
    Or if you have the previous project saved, just replace all the existing code with this code. If you compare the code you can see the few changes.
    Since the pictureboxes never switch positions, the updating of the indexes cross references is no longer needed. The cross references may not be needed at all now, but I didn't examine the rest of the code to see if that stuff could be removed or simplified.
    Code:
    Option Explicit
    
    Private Type location
      X As Integer
      Y As Integer
    End Type
    
    Dim gridPos(15) As location
    Dim gridPic(15) As Integer
    
    Dim srcGridIdx As Integer, destGridIdx As Integer
    
    
    Private Sub Form_Load()
      ScaleMode = vbPixels
      
      
      Dim i As Integer
      Dim xinc As Integer
      Dim yinc As Integer
      
      For i = 0 To 15
        gridPic(i) = i  'set grid to pic index
      Next
      
      Dim X As Integer, Y As Integer
      
      With Picture1(0)
        .AutoRedraw = True
        .ScaleMode = vbPixels
        .BackColor = QBColor(Rnd * 16)
        gridPos(0).X = .Left
        gridPos(0).Y = .Top
        .Tag = 0
        Picture1(0).Print 0
        
        xinc = Picture1(0).Width + 20
        yinc = Picture1(0).Height + 20
        For i = 1 To 15
          Load Picture1(i)
          Picture1(i).AutoRedraw = True
          Picture1(i).ScaleMode = vbPixels
          Picture1(i).Tag = i 'set tag to grid index
          X = i Mod 4
          Y = i \ 4
          gridPos(i).X = .Left + X * xinc
          gridPos(i).Y = .Top + Y * yinc
          
          Picture1(i).Left = gridPos(i).X
          Picture1(i).Top = gridPos(i).Y
          Picture1(i).Visible = True
          If i < 12 Then
            Picture1(i).BackColor = QBColor(Rnd * 16)
            Picture1(i).Print i
          Else
            Picture1(i).BackColor = BackColor
          End If
        Next
      End With
    End Sub
    
    Private Sub Picture1_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
      Picture1(Index).ZOrder  'bring this picture to the top so is dragged over other pics
      
    End Sub
    
    Private Sub Picture1_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
      Static lx As Single, ly As Single
      
      If Button = vbLeftButton Then
        With Picture1(Index)
          srcGridIdx = .Tag
          .Move .Left + (X - lx), .Top + (Y - ly)
        End With
      Else
        lx = X: ly = Y
      End If
    End Sub
    
    Private Sub Picture1_MouseUp(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
      Dim closestGridIdx As Integer
      Dim Dist As Double
      Dim Dx As Double, Dy As Double
      Dim MinDist As Double
      Dim i As Integer
      Dim px As Single, py As Single
      
      MinDist = 1000# * 1000#
      px = Picture1(Index).Left
      py = Picture1(Index).Top
      
      For i = 0 To 15
        Dx = px - gridPos(i).X: Dx = Dx * Dx
        Dy = py - gridPos(i).Y: Dy = Dy * Dy
        Dist = Dx + Dy
        If Dist < MinDist Then
          MinDist = Dist
          closestGridIdx = i
        End If
      Next
      
      Dim destPic As Integer
      destPic = gridPic(closestGridIdx)
      
      'swap the two picturebox positions
     ' Picture1(destPic).Move gridPos(srcGridIdx).X, gridPos(srcGridIdx).Y
     ' Picture1(Index).Move gridPos(closestGridIdx).X, gridPos(closestGridIdx).Y
      
      'move dragged picture back to its original location
      Picture1(Index).Move gridPos(srcGridIdx).X, gridPos(srcGridIdx).Y
      
      'update the indexes
     ' Picture1(destPic).Tag = srcGridIdx
     ' Picture1(Index).Tag = closestGridIdx
     ' gridPic(srcGridIdx) = destPic
     ' gridPic(closestGridIdx) = Index
      
      Dim tp As StdPicture
      Set tp = Picture1(destPic).Image
      Picture1(destPic).Picture = Picture1(Index).Image
      Picture1(Index).Picture = tp
      
    End Sub
    Last edited by passel; Dec 1st, 2020 at 04:37 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  16. #16

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    I have played with Passel's new coded for some time now and cannot modify it for my needs.
    1. on load - place no picture in i = 1. Just have an outline of the picture box, no color.
    2. on move - move a picturebox with a color to the outlined picturebox (any i to 1)
    move the original outlined, no color picturebox to the now empty space

    Thanks

  17. #17
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Moving a picture box

    What does "i" mean?
    i = 1,
    any i to 1

    Is i an index into an array, and a 1 in the array(i) means the picturebox with the same index is empty?
    What other values are in the array?

    1. on load - place no picture in i = 1. Just have an outline of the picture box, no color.
    In the example code, the last four pictureboxes (the bottom row), have "no picture", they are just the outline of the picturebox with the backcolor the same has the form's backcolor so look empty.

    2. on move - move a picturebox with a color to the outlined picturebox (any i to 1)
    move the original outlined, no color picturebox to the now empty space
    From that it now sounds like you want to swap pictureboxes. That is what the original example in post #6 did.

    In post #14 you said you wanted to swap the pictures, not the pictureboxes, so that is what the example in post #15 does.

    It is up to you to add code when you release the button and find the space dropped on whether that space is empty or not. My code doesn't check, it just swaps (either the pictureboxes, post#6, or the pictures, post #15). Just add a check to verify the space is empty before doing the swap, and if not, don't do the swap but return the dragged picturebox back to its original location.

    From the little you posted in post #16, I'm not sure just what your issue is, and what you really want to happen.
    You should have the logic all worked out and the design written down in plain language first, and once you understand what you want to do, write code to implement the design you've worked out.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  18. #18

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    I want swap this way. Say position 1 (0 to 3) has a picture in it. Position 3 (0 to 3) has no picture it it. I want to drag the pic in position 1 to position 3. I want position 1 to have no picture it it. All this code in the mousemove event.

    Sorry for the poor explanation. I think of this as a swap. I think you don't.

  19. #19
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Moving a picture box

    Here's a goofy "pick 4 cards" using no PictureBox at all. Just click on cards to pick them.

    Name:  sshot.jpg
Views: 1382
Size:  60.2 KB

    As usual, the forum software converted that screenshot to a crummy bloated JPEG. Oh well, I guess they like to waste disk and bandwidth or they'd fix that.

    Not much here really besides a bit of arithmetic. Most of the attachment size consists of image resources.
    Attached Files Attached Files

  20. #20
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Moving a picture box

    Also, see my post to your other similar thread. Or, look at this attachment.
    Attached Files Attached Files
    Sam I am (as well as Confused at times).

  21. #21

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    SamOscarBrown. Very nice. Small code. Why does the move not always work? Are there rules?
    I would have to merge the Picture1_DragDrop sub routine with a Picture1_MouseMove sub routine so I can see the card actually being dragged (or moved) to the new location.

  22. #22
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Moving a picture box

    Quote Originally Posted by AccessShell View Post
    SamOscarBrown. Very nice. Small code. Why does the move not always work? Are there rules?
    I would have to merge the Picture1_DragDrop sub routine with a Picture1_MouseMove sub routine so I can see the card actually being dragged (or moved) to the new location.

    I am not sure why yours doesn’t work all the time. I only tested a few moves. Under what circumstances does it not allow you to drag a card to the open (no image) picture box? On cell now so can’t test more right now
    Sam I am (as well as Confused at times).

  23. #23

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    The drag (or no drag)seems to be random. I can try to drag any card to the open card. Sometimes it works sometimes it doesn't. I can't give you a specific case. It might be as simple as the mousse itself.

  24. #24
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Moving a picture box

    as long as the upper left corner of the dragged pb is within the blank PB, and you release the mouse then, it should work fine. I can't duplicate what is happening on your machine. Sorry.
    Sam I am (as well as Confused at times).

  25. #25

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    as long as the upper left corner of the dragged pb is within the blank PB, and you release the mouse then, it should work fine.
    Are you saying that with mousemove I can snap into place? Or are you saying with dragdrop you can snap into place?
    If with mousemove, do you need to anything special, or different than a normal mousemove. I find that with a mousemove, wherevever you stop (mouseup) the object. Maybe that means in the mouseup event I write code to snap into place?

  26. #26
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Moving a picture box

    I think you have to have the upper left corner in the destination and you have to have the mouse in the destination. If either is outside the bounds of the destination, it won't drop, in the first case the upper left corner isn't in bounds so the location test rejects the drop. In the second case, if the mouse is outside the bounds, then the DragDrop event doesn't happen, i.e. it is canceled and the dragged object is left in its original position.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  27. #27
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Moving a picture box

    What you say is correct, passel. Both the mouse pointer and upper corner of the pb must be within the blank pb loc
    Sam I am (as well as Confused at times).

  28. #28

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    I have made some progress. I have successfully the case where the move is invalid.
    I discovered (I think) the with dragdrop only the pic gets moved. with mousemove the PB with it's contents gets moved. Also the mousemove actually shows the movement. When I used mousemove and the move was invalid (faked an invalid move) the pb with its content moved back to the source position just like I had dopen. Mousemove also visibly shows the dragging operation to the source position.

  29. #29
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Moving a picture box

    Quote Originally Posted by AccessShell View Post
    Are you saying that with mousemove I can snap into place? Or are you saying with dragdrop you can snap into place?
    If with mousemove, do you need to anything special, or different than a normal mousemove. I find that with a mousemove, wherevever you stop (mouseup) the object. Maybe that means in the mouseup event I write code to snap into place?
    Just look at my code...notice it is using the dragdrop of the PB's. So, YES, I am using dragdrop to 'snap in place' (actually, just setting lefts and tops to equal that of the blank PB). Why are you trying something different with a mousemove?

    If you REALLY want us to help more DIRECTLY, attached a zipped file of your entire project (less .exe's of course).

    Edit: to prove that the PictureBox is moved (not simply of switching images), put this line right before Exit Sub in my code. Make a few swaps and notice in the debug window (Immediate Window) that it shows the left and top values to where the PB was moved.
    Last edited by SamOscarBrown; Dec 4th, 2020 at 12:57 PM.
    Sam I am (as well as Confused at times).

  30. #30

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    Here's the fileAS.zip

  31. #31
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Moving a picture box

    I see you are not using DragDrap, but only mousemoves,down.

    Think that is your issue...I'll look at it in a bit, but in the meantime, you have this code:

    Code:
    'Position the form    Me.Top = (Screen.Height - Me.Height) / 2
        Me.Left = (Screen.Width - Me.Width) / 2
    Not needed...simply set, in the IDE, the Form's StartUpPosition property to 2-CenterScreen.
    Sam I am (as well as Confused at times).

  32. #32
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Moving a picture box

    Sorry...dupe post
    Sam I am (as well as Confused at times).

  33. #33

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    Thank you for the tip on centering the screen. I never new that. AS far as mousemove, I wanted to see the pb getting dragged to the dest location.

  34. #34

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    Thank you for the tip on centering the screen.&nbsp; I never new that.&nbsp; As far as mousemove, I wanted to see the pb getting dragged to the dest location.

  35. #35
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Moving a picture box

    At a quick glance of your project, I see you dim MouseDownX and MouseDownY, but you never set them...so they are always zero.

    Also, your code does not check for a valid move or not, so that your boolean always is false, hence, the PB returns to its original location.

    You have to do something similar as I did in my example, loop through all the other pb's to see which PB the mouse cursor (or the upper left of the moving PB) falls over. So, you check to see if the picture of the PB is blank or not (and/or if it is a valid move, depending on your game rules).
    Sam I am (as well as Confused at times).

  36. #36

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    I have not written the game rules yet. I just put in true or false to test the moving of the cards. If I can get the dragdrop code to show the 'pulling' of the card, I would use it. Mousemove shows the 'pulling of the card'. I know that MouseDownX and MouseDownY. The will be used when I can make the card move to the dest whle faking a valid move.

  37. #37
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Moving a picture box

    Understand...so in your testing, first loop through the other PB's (blank or otherwise) and see if you can determent the left and top values of the PB which you are over... and then set your source PB lefts and tops to those values....see my code for an example.
    Sam I am (as well as Confused at times).

  38. #38

    Thread Starter
    Fanatic Member AccessShell's Avatar
    Join Date
    Oct 2013
    Posts
    790

    Re: Moving a picture box

    That's exactly where I am right now. Trying to figure out where I am (the source card's new position).

  39. #39
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,145

    Re: Moving a picture box

    loop through the pb's....you can then find each of their lefts and tops (and you know their widths and heights), so if your mouse cursor x & y fall within those ranges, set the moving PB's values to the lefts and tops of the one you are over when you release the mouse button. I did that in my example already.
    Sam I am (as well as Confused at times).

  40. #40
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Moving a picture box

    I think you're getting ahead of yourself, when you're focusing on how you're going to move the card, and then figure out how to implement the rules later.

    I think you should focus on the rules first, and the game play and how you're going to organize the information you need to keep track of the state of the game, and what are valid moves and invalid moves, and how you can help give feedback to the player to help the game along.

    Somehow, I haven't come across the game before, so I looked online for it, and found several sites where you can play it online, and I tried three of them out, for comparison of how you play the game.

    The three I tried, had three different implementations, so I assume you should have a good idea of how you want to implement the game, from a user interface perspective (i.e. you want to drag a card to an open space).
    I think a couple of implementations allowed dragging the card to an open space, or you had the option of double clicking the card to move it to a space in addition to dragging the card. One of the implentations, you simply clicked on the card or you clicked on the open space, and it moved the card. One of them you had the option of clicking on the card and then clicking on the space and it moved the card.

    One of the implementations had a 10 minute time out period, so you had to get through the game (with up to three re-deals) in the 10 minutes, or is just stopped after 10 minutes and you only got credit for the cards you had "home", whether you've dealt once or four times. That one was a bit frustrating for a beginner, because you had to make decisions fairly quickly so couldn't spend a lot of time scanning the cards and planning strategy between moves.

    On the other hand, the actual moves you can legally make is limited, and as such, when you write the game you don't actually have to spend a lot of time looping and searching since you won't have 48 choices of what card can move into the empty space. 44 times out of 48, there is either only one card that can be moved into the space, or no card that can be moved into a space.

    If a King is to the left of the space, that space is not available. No card can be placed there. A card can only be placed in the space if it is of the same suit and one rank higher than the card to the left of the space. So, there is only one possible card that can be placed there, if the card to the left is not a King.
    The exception is the first column. If a space is in the first column, there is no card to the left, so the only card that can be placed there is a two.
    It doesn't matter which two is place there, but only a two can go there.

    And once a two has been placed in the first column, it is locked. It can't be moved again and it establishes the suit that that row will have to be complete the game. So the objective is to place the three of the suit next to the two, and then the four next to the three and so on.

    Of course you already know this because I wouldn't think you would be trying to write the game if you're weren't already familiar with playing it.

    But knowing those restrictions up front should be "encoded" into how the game is developed at the start, not after you've figured out how to move the cards.

    Common things that should be implemented and are implemented in other versions of the game.

    1. For each of the open spaces, determine what card (could be no card) or cards (only in the case where the space is in the first column and there are multiple 2's that are not already locked into the first column) that can be moved, and if the user tries to select a card that isn't movable don't allow them to select it, i.e. they should not be able to drag cards that are not legal to drag.
    1.a Most games give some indication of the cards that can be dragged or selected, to help the user find them quicker rather than have them get weary from having to scan the deck multiple times trying to find the one card that can be moved into that spot.

    2. You have to lock the cards once they have been moved into their "home" position. When you re-deal, you don't re-deal these cards.
    Of course you don't necessarily have to have "lock" logic, if you already have the legal move logic, since any card that is in its home position will never be legal to move, so it is "locked" by circumstance. The challenge is when you have to pick up and re-deal the cards that are not locked.
    Being able to loop by position and access the card that is there is an important feature to have and you should have that worked out at the beginning.

    3. I guess this is optional, but indicating what cards can be moved by highlighting them in some fashion (change the color, add a border, flash a border, etc.) would be good to have and make the game less wearisome.

    Your drag logic can be a lot simpler if you implement it so that you already know which cards can be dragged, and where they can be dragged.
    If a draggable card is picked, you only have to see if they drag it to the space where it can be dropped.
    Don't allow non-droppable cards to be dragged.

    I think the priority is know how you want to keep track of which card is in which position, where the spaces are and what card is to its left, and which cards can be dragged. When you need to pick up cards for a re-deal, then knowing which card is in which position is equally important.

    The index of the picturebox can be used to identify the suit and rank of the card, but that is easier to do when the user interacts with the card causing an event that passes the index to you. It is harder when you have to loop through the positions with software and identify what index is at each position.
    That is why it is best to have an array or list where you keep such information to track the state of the game and where the cards are. That way you can do your logic against those values and then distribute the pictureboxes to match those locations, rather than do a lot of scanning and calculating based on card position.

    There could be an exception to that if you use a userscale mode for the container. You could change the scale mode to run from 0 to 14 horizontally and 0 to 5 vertically, for instance, and then just place your cards at positions 1 to 13 horizontally, and 1 to 4 vertically. That way the cards position literally because a column, row index that is easy to use directly. But that is only for good for converting a card (by index), into a position. When you want to go the other way, i.e. when picking up cards for a re-deal, or identifying which positions have which cards, you really want to have a way to look that information up without having to search the picturebox controls to what card is at a given position.
    Last edited by passel; Dec 5th, 2020 at 07:20 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width