dcsimg
Results 1 to 6 of 6

Thread: [RESOLVED] Getting the Row and Col and Index of a picturebox array element

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    550

    Resolved [RESOLVED] Getting the Row and Col and Index of a picturebox array element

    I have this array of pictureboxes 5 x 5 (the dimensions may change from one time to another so it wont always be a 5 x 5 array) as arranged per below diagram. I am moving one of the pictureboxes around the grid and then dropping it on one of the other pictureboxes. Once I drop the picturebox how do I get the index of the target picturebox and it's row and col number.

    Note: The width and height of the pictureboxes will change from one time to another if that makes any difference

    I'm using the following to move the picturebox
    Code:
    Dim lx As Integer, ly As Integer
      '
      '
    Private Sub pictureTile_MouseDown(Index As Integer, Button As Integer, Shift As Integer, X As 
    Single, Y As Single)
     If Button = vbLeftButton Then
       lx = X
       ly = Y
     End If
    End Sub
    
    Private Sub pictureTile_MouseMove(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton Then
       pictureTile(Index).Left = pictureTile(Index).Left + X - lx
       pictureTile(Index).Top = pictureTile(Index).Top + Y - ly
     End If
    End Sub
    
    Private Sub pictureTile_Mouseup(Index As Integer, Button As Integer, Shift As Integer, X As Single, Y As Single)
     If Button = vbLeftButton Then
       '
       ' Here I need to get index, row, and col of target picturebox
       '
     End If
    End Sub

    Code:
    +----+----+----+----+----+
    | 0  | 1  | 2  | 3  | 4  |
    +----+----+----+----+----+
    | 5  | 6  | 7  | 8  | 9  |
    +----+----+----+----+----+
    | 10 | 11 | 12 | 13 | 14 |
    +----+----+----+----+----+
    | 15 | 16 | 17 | 18 | 19 |
    +----+----+----+----+----+
    | 20 | 21 | 22 | 23 | 24 |
    +----+----+----+----+----+

  2. #2
    Frenzied Member
    Join Date
    Apr 2012
    Posts
    1,098

    Re: Getting the Row and Col and Index of a picturebox array element

    If you don't know where you're going, any road will take you there...

    My VB6 love-children: Vee-Hive and Vee-Launcher

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    550

    Re: Getting the Row and Col and Index of a picturebox array element

    the code example from that Thread doesn't work for what I need here. That thread was when I already knew either the index or the row and column but in this case I don't know either. If you read my 1st post you will note that I said that I move a picturebox on top of another picturebox and I need the index, row and column of the target picturebox. All I know is the Left and Top of the picturebox I'm moving

    If I use the method as described by passel ---- >Index = col * NumRows + Row then the only information I have is the left and top of the picturebox I'm moving so if I applied that to the equation i get this:

    Index of the picturebox I'm moving = 4

    After picturebox 4 is dropped:
    Left of that picturebox = 251
    Top of that picturebox = 214

    Now I know that the Left and Top values are within picturebox 12

    so, using those values I get this:

    NewIndex = 214 * 5 + 251

    NewIndex = 1321

    So it looks like I can't use the Left and Top properties as is so maybe there's something I need to do to these values. What to do?
    Last edited by Code Dummy; May 20th, 2019 at 05:12 PM.

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,337

    Re: Getting the Row and Col and Index of a picturebox array element

    If you look at the code I posted in my version of the puzzle, I move numbers around in a two dimensional array which is the index of the picturebox in that position.
    When you click on the picturebox, you are given the index, and I do a quick search through the 2d array to find what position that picturebox is in the array.

    I guess, given you were thinking about left,top which I hadn't considered before, off the top of my head, you could change the Scale of the Container (assuming you're using a Form or Picturebox for the container of the tiles) to be one greater than the number of dimensions in each direction, i.e. in your case for a 5x5 array of pictureboxes, if you host those in another picturebox, you can set its scale to Scale 0,0,6,6.
    Then when you position the pictureboxes you would put them at the coordinates, like (0,0), (0,1), (0,2) etc.

    That way the left, top position of the picturebox would tell you directly what column and row it was in.

    Since the positions are of type Single, the value may not exactly round to an integer, so you might need to round and truncate it yourself to be safe, e.g.
    Code:
    Dim Col As Integer
    
    Col = Int(PicTile.Left + 0.1)

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    550

    Re: Getting the Row and Col and Index of a picturebox array element

    OK, I got the index of the target picturebox based on the upper left corner of the source picturebox:

    SrcLeft is the left of the source picturebox
    SrcTop is the top of the source picturebox

    120 is the width of a picturebox
    100 is the height of a picturebox

    Code:
    SrcLeft = SrcLeft \ 120
    SrcTop = SrcTop \ 100
    TargetIndex = SrcTop * NumRows + SrcLeft
    Now, all I need to do is to figure out where the mouse pointer is because I think I might want to use the X,Y of the mouse to determine the target picturebox rather than the X,Y of the source picturebox

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    550

    Re: Getting the Row and Col and Index of a picturebox array element

    I got it now. The mouse pointer is easy. Just add the X and Y to the left and top of the picturebox and do the same as in previous post

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width