dcsimg
Results 1 to 15 of 15

Thread: Rotating Picturebox Array...Again

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    27

    Rotating Picturebox Array...Again

    Ok so i used the code kebo gave me earlier when using tablelayout pannels, but the computer i'm using is really slow and it ended up being really glitchy. I tried using the same code only modified except using two 2D arrays and pictureboxes, but it doesn't work. I see what my problem is, but don't quite understand how to fix it. Any help? Thanks!
    Code:
           Select Case rotation Mod 4
                Case 0
                    'no rotation
    
                    For i As Integer = 0 To 10
                        For j As Integer = 0 To 10
                            rotatearray(i, j) = array(i, j)
                        Next
                    Next
    
                Case 1
                    'rotate right 90
    
                    For i As Integer = 0 To 10
                        For j As Integer = 10 To 0 Step -1
                            rotatearray(i, j) = array(i, j)
                        Next
                    Next
    
                Case 2
                    'rotate right 90
    
                    For i As Integer = 10 To 0 Step -1
                        For j As Integer = 10 To 0 Step -1
                            rotatearray(i, j) = array(i, j)
                        Next
                    Next
    
                Case 3
                    'rotate right 270
    
                    For i As Integer = 10 To 0 Step -1
                        For j As Integer = 0 To 10
                            rotatearray(i, j) = array(i, j)
                        Next
                    Next
            End Select
    
            For x As Integer = 0 To 10
                For y As Integer = 0 To 10
                    array(x, y) = rotatearray(x, y)
                    array(x, y).BringToFront()
                    Me.Controls.Add(array(x, y))
                Next
            Next

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,242

    Re: Rotating Picturebox Array...Again

    I see what my problem is
    Well that's nice and all but it might help to, you know, like tell us? (To be said out loud with the appropriate rising inflection or this'll be another one of my Parodies Lost!)
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    27

    Re: Rotating Picturebox Array...Again

    Haha well right now I'm making my rotate array = my original array exactly, and then making my original array back equal to my rotate array, which does absolutely nothing then to rotate it. But i don't know what to do to fix that, or even if i need the second array or if that's just causing more confusion than it's worth.

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,708

    Re: Rotating Picturebox Array...Again

    My original code works by adding the picture boxes to the table layout panel in a different order depending on the rotation. The critical part of making the code work is 2 fold; the arrays need to be iterated in the correct order (up or down) and the correct array dimension (either i or j) needs to be either on the inside or outside. Go back and take a look at the original implementation I posted and compare the direction of the array and the dimensions the are on the inside for/next loops. The problem you have in in the order you are iterating the dimensions... you have some of the i's and j's swapped.
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

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

    Re: Rotating Picturebox Array...Again

    <edit> See I overlapped posting with the last two...</edit>

    kebo's code cleared a list and changed the order he added the controls to the list.
    You're doing this:

    rotatearray(i, j) = array(i, j)

    Since you're always assigning (i,j) to (i,j), it doesn't matter in what order you index (i,j), (forward, backward, inside out, ...), you will always have the same result, nothing changed.

    You probably want to just loop i,j the same way each time (your destination), and use a different set of local variables that select your array source indicies based on your desired rotation, or based off of the looping i,j variables.
    i.e in his example to rotate right, he modified i to loop backwards, so (I didn't test this and wouldn't go about this in this manner, but... assuming the original worked)
    Code:
                    'rotate right 90
    
                    For i As Integer = 0 To 10
                        For j As Integer = 0 To 10
                            rotatearray(i, j) = array(10 - i, j)  'Reverse the increment i on the source
                        Next
                    Next

  6. #6
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,708

    Re: Rotating Picturebox Array...Again

    Also if you plan on using a 10X10 array as your code suggest, then this method will probably look bad. You would be essential populating 100 controls into a TLP and the user will see them being added. There are thing you can do to avoid it, but ideally you should populate the TLP with all of the controls, then change the properties. This code can be adapted to do that. If you need help, just ask.
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    27

    Re: Rotating Picturebox Array...Again

    Ok i fixed that now but it still isn't working. Didn't fix it though. Watched it through and it Doesn't change anything...

  8. #8
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,708

    Re: Rotating Picturebox Array...Again

    Code:
     For x As Integer = 0 To 10
                For y As Integer = 0 To 10
                    array(x, y) = rotatearray(x, y)
                    array(x, y).BringToFront()
                    Me.Controls.Add(array(x, y))
                Next
            Next
    are you using a table layout panel? That code suggest not. Without it, my code doesn't work directly
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,723

    Re: Rotating Picturebox Array...Again

    I don't know what you're doing, but I probably wouldn't use a bunch of pictureboxes for it.
    In any case, since I'm not real clear on what array(x,y) and rotatearray(x,y) really are arrays of,
    I'm thinking that perhaps they are arrays of pictureboxes, and you're trying to move the pictureboxes by changing the order they are in the array.
    The order that the pictureboxes appear in the array have no effect on the order or where they are displayed on the form, unless you redraw the pictureboxes based on that order.
    That was what kebo's code did, it added the pictureboxes to a table layout panel which placed them one after another as you added them, in your desired order.

    In your case, you are probably just shuffling the pictureboxes around in the array, but you are not then looping through the array and repostitioning the boxes linearly, as you find them in the array.
    I imagine there has to be some overhead in adding them to a table layout which might be slower than repositioning them yourself. I don't know, haven't played with the table layout much at all.

    As a separate example using pictureboxes (although as already mentioned, I probably wouldn't be using controls for whatever you're doing), this code can just be pasted into a new project and will create the pictureboxes and fill a square area of the form (either the top, or left depending on the shortest, width or height) with an 11x11 array of pictureboxes.
    It will create an image on the fly to put in the picturebox with a number associated with the picturebox. If you click on a box, it can change the background color so you can create a pattern and verify the boxes are moving around the way you expect.
    You press the "a", "s", "d" and "f" keys to change the "orientation" to rotated left, normal, rotated right, rotated 180.
    Code:
    Option Explicit On
    
    Public Class Form1
      Private PBarray(10, 10) As PictureBox  'Array of pictureboxes
      Private PBOrigLocation(10, 10) As Point 'Array of locations the picturebox are originally assigned
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim boxSize As Integer
        Dim MinSize As Integer = Math.Min(Me.ClientSize.Width, Me.ClientSize.Height) 'find the lesser value, height or width
        Dim cnt As Integer       'just to count the picturebox and put the count as an image in the pictureboxes
        boxSize = MinSize \ 11   'Fit 11x11 squares inside the form
        Me.KeyPreview = True     'Use the keys a,s,d,f to select one of the rotations
    
        For y As Integer = 0 To 10
          For x As Integer = 0 To 10
            PBOrigLocation(x, y) = New Point(x * boxSize, y * boxSize)  'Save the location in the array
            PBarray(x, y) = New PictureBox                              'Create a new picturebox
            cnt += 1                                                    'increment the count of pictureboxes
            With PBarray(x, y)                                          'With the picturebox created
              .Tag = cnt                                                '  Put the count in the tag so we can reference it
              .Location = PBOrigLocation(x, y)                          '  Set the location to the calculated location
              .ClientSize = New Size(boxSize, boxSize)                  '  Size the box to our calculated size
              .BorderStyle = BorderStyle.FixedSingle                    '  Display a border so we see the size of the box
              .Image = New Bitmap(boxSize, boxSize)                     '  Create a new bitmap and assign it to the picturebox
              Using g As Graphics = Graphics.FromImage(.Image)          '  Using a graphic object created for that image
                g.ScaleTransform(boxSize / 24, boxSize / 24)               ' Scale the text to fit about 3 chars in the box
                g.DrawString(cnt.ToString, Me.Font, Brushes.Black, 0, 0)   ' Draw the box number (count) to the image
              End Using                                                 '  Release the Graphics object
              .Visible = True                                           '  make the picturebox visible
              Me.Controls.Add(PBarray(x, y))                            '  Add it to the form's control collection
              AddHandler .MouseDown, AddressOf Pb_MouseDown             '  Add a mouseDown handler for the picturebox
            End With
          Next
        Next
      End Sub
    
      Private Sub Form1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        Select Case e.KeyChar.ToString.ToLower
          Case "a" : rotateLeft()
          Case "s" : noRotate()
          Case "d" : rotateRight()
          Case "f" : rotate180()
        End Select
      End Sub
    
    
      Private Sub Pb_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs)
        Dim bp As PictureBox = DirectCast(sender, PictureBox)
        If e.Button = Windows.Forms.MouseButtons.Left Then
          bp.BackColor = Color.Aqua  'Change the backcolor of the mouse clicked on
        Else
          bp.BackColor = Control.DefaultBackColor
        End If
        Me.Text = bp.Tag.ToString 'Print the tag value to the titlebar to show we know which box was clicked
      End Sub
    
      Private Sub noRotate()
        For Y As Integer = 0 To 10
          For X As Integer = 0 To 10
            PBarray(X, Y).Location = PBOrigLocation(X, Y)     'Everything in its original place
          Next
        Next
      End Sub
    
      Private Sub rotateRight()
    
        For Y As Integer = 0 To 10
          For X As Integer = 0 To 10
            PBarray(X, Y).Location = PBOrigLocation(10 - Y, X)  'Make 1 to 11 go down the right column
          Next
        Next
    
      End Sub
    
      Private Sub rotateLeft()
    
        For Y As Integer = 0 To 10
          For X As Integer = 0 To 10
            PBarray(X, Y).Location = PBOrigLocation(Y, 10 - X) 'Make 1 to 11 go up the left column
          Next
        Next
      End Sub
    
      Private Sub rotate180()
    
        For Y As Integer = 0 To 10
          For X As Integer = 0 To 10
            PBarray(X, Y).Location = PBOrigLocation(10 - X, 10 - Y)  'Make 1 to 11 go right to left, on the bottom row
          Next
        Next
      End Sub
    
    End Class
    Last edited by passel; Jan 20th, 2014 at 07:52 PM. Reason: Change Debug.Print to Me.Text =

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    27

    Re: Rotating Picturebox Array...Again

    No Kebo i'm trying to use pictureboxes because when i used the tablelayout with 121 plots, it took about 30 seconds for it to rotate, and you would watch it slowly moving everything. I wasn't sure if the same code would work then but with pictureboxes, but apparently not quite exactly

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,723

    Re: Rotating Picturebox Array...Again

    <edit>. Just to be clear, the code in my previous post is using pictureboxes, which should be what you're aiming for </edit>.

    Don't know if you tried the code above.
    You simply paste it into a new project and run, and it should work.
    You can then see if you can figure out how it works.
    The size of the pictureboxes is based on the size of the form at startup, so you might want to increase the size of the form in the IDE before running.
    A little change and I'm sure it could also resize on the fly with the form resize event.
    The boxes move in a fraction of a second on my machine.
    An example of three of the rotations from that code:
    Name:  PictureBoxArrayRotate.png
Views: 867
Size:  47.8 KB

    p.s. (Went ahead and added some code to do some dynamic resizing)
    Added a variable to keep track of the last rotation so the pictureboxes could be reposition in the same layout as they were last when the form is resized, and the pictureboxes resized.
    Changed the SizeMode of the pictureboxes to stretch the image, so we don't have to redraw the image ourselves (but will probably be fuzzier than if we redrew the pictures ourselves).
    Added code to the resize event to resize the pictureboxes and update the original position array based on that size.
    Code:
    Option Explicit On
    
    Public Class Form1
      Private PBarray(10, 10) As PictureBox  'Array of pictureboxes
      Private PBOrigLocation(10, 10) As Point 'Array of locations the picturebox are originally assigned
      Private LastRotate As Integer
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim boxSize As Integer
        Dim MinSize As Integer = Math.Min(Me.ClientSize.Width, Me.ClientSize.Height) 'find the lesser value, height or width
        Dim cnt As Integer       'just to count the picturebox and put the count as an image in the pictureboxes
        boxSize = MinSize \ 11   'Fit 11x11 squares inside the form
        Me.KeyPreview = True     'Use the keys a,s,d,f to select one of the rotations
    
        For y As Integer = 0 To 10
          For x As Integer = 0 To 10
            PBOrigLocation(x, y) = New Point(x * boxSize, y * boxSize)  'Save the location in the array
            PBarray(x, y) = New PictureBox                              'Create a new picturebox
            cnt += 1                                                    'increment the count of pictureboxes
            With PBarray(x, y)                                          'With the picturebox created
              .Tag = cnt                                                '  Put the count in the tag so we can reference it
              .SizeMode = PictureBoxSizeMode.StretchImage               '  If we resize the picturebox, stretch the image
              .Location = PBOrigLocation(x, y)                          '  Set the location to the calculated location
              .ClientSize = New Size(boxSize, boxSize)                  '  Size the box to our calculated size
              .BorderStyle = BorderStyle.FixedSingle                    '  Display a border so we see the size of the box
              .Image = New Bitmap(boxSize, boxSize)                     '  Create a new bitmap and assign it to the picturebox
              Using g As Graphics = Graphics.FromImage(.Image)          '  Using a graphic object created for that image
                g.ScaleTransform(boxSize / 24, boxSize / 24)               ' Scale the text to fit about 3 chars in the box
                g.DrawString(cnt.ToString, Me.Font, Brushes.Black, 0, 0)   ' Draw the box number (count) to the image
              End Using                                                 '  Release the Graphics object
              .Visible = True                                           '  make the picturebox visible
              Me.Controls.Add(PBarray(x, y))                            '  Add it to the form's control collection
              AddHandler .MouseDown, AddressOf Pb_MouseDown             '  Add a mouseDown handler for the picturebox
            End With
          Next
        Next
      End Sub
    
      Private Sub Form1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        Select Case e.KeyChar.ToString.ToLower
          Case "a" : rotateLeft()
          Case "s" : noRotate()
          Case "d" : rotateRight()
          Case "f" : rotate180()
        End Select
      End Sub
    
    
      Private Sub Pb_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs)
        Dim bp As PictureBox = DirectCast(sender, PictureBox)
        If e.Button = Windows.Forms.MouseButtons.Left Then
          bp.BackColor = Color.Aqua  'Change the backcolor of the mouse clicked on
        Else
          bp.BackColor = Control.DefaultBackColor
        End If
        Me.Text = bp.Tag.ToString 'Print the tag value to the titlebar to show we know which box was clicked
      End Sub
    
      Private Sub noRotate()
        LastRotate = 0
        For Y As Integer = 0 To 10
          For X As Integer = 0 To 10
            PBarray(X, Y).Location = PBOrigLocation(X, Y)     'Everything in its original place
          Next
        Next
      End Sub
    
      Private Sub rotateRight()
        LastRotate = 1
    
        For Y As Integer = 0 To 10
          For X As Integer = 0 To 10
            PBarray(X, Y).Location = PBOrigLocation(10 - Y, X)  'Make 1 to 11 go down the right column
          Next
        Next
    
      End Sub
    
      Private Sub rotateLeft()
        LastRotate = 2
    
        For Y As Integer = 0 To 10
          For X As Integer = 0 To 10
            PBarray(X, Y).Location = PBOrigLocation(Y, 10 - X) 'Make 1 to 11 go up the left column
          Next
        Next
      End Sub
    
      Private Sub rotate180()
        LastRotate = 3
    
        For Y As Integer = 0 To 10
          For X As Integer = 0 To 10
            PBarray(X, Y).Location = PBOrigLocation(10 - X, 10 - Y)  'Make 1 to 11 go right to left, on the bottom row
          Next
        Next
      End Sub
    
      Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
        Dim boxSize As Integer
        Dim MinSize As Integer = Math.Min(Me.ClientSize.Width, Me.ClientSize.Height) 'find the lesser value, height or width
        boxSize = MinSize \ 11   'Fit 11x11 squares inside the form
        If PBarray(0, 0) IsNot Nothing Then
          For y As Integer = 0 To 10
            For x As Integer = 0 To 10
              PBOrigLocation(x, y) = New Point(x * boxSize, y * boxSize)  'Save the location in the array
              PBarray(x, y).ClientSize = New Size(boxSize, boxSize) 'resize the pictureboxes for the new size
            Next
          Next
          Select Case (LastRotate)
            Case 0 : noRotate()
            Case 1 : rotateRight()
            Case 2 : rotateLeft()
            Case 3 : rotate180()
          End Select
        End If
      End Sub
    End Class
    I am curious though of what these 121 plots are, and why you would want to rotate them in this manner. There could be a cleaner approach than using all these pictureboxes.
    Last edited by passel; Jan 21st, 2014 at 04:45 PM. Reason: Realized rotateRight,Left were reversed in Resize code (fixed)

  12. #12
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,708

    Re: Rotating Picturebox Array...Again

    yes, the TLP is certain not adequate for adding and removing controls when dealing with anything larger than about a 3x3 array. It does however provide an easier solution if the form needs to be resized. By simply putting controls on the form, makes using anchors and docking nearly useless. If form resizing is not a requirement the passel's approach is probably ideal.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  13. #13
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,723

    Re: Rotating Picturebox Array...Again

    I know if I feel I've given a useful solution, but it appears that it is under-appreciated or ignored, it bothers me a little, but I try to console myself by convincing myself that it just wasn't understood, and let it go.

    Looking back on JaneTheSardine's posts to see if I could get a handle on what the objective is here, and whether there would be a better approach than using Pictureboxes, I see that .paul. gave a pretty useful solution in the previous thread and that it has not been used, and I have to assume it is because it wasn't understood, since it is a good solution and my solution shouldn't have been necessary.

    My solution and .paul.s are similar in that we both create an array to hold the original position of the pictureboxes so we have a known reference to rotate from. And we both have an array of pictureboxes references so we can get at the picturebox objects easily.
    His arrays are 1 dimensional, and mine are 2 dimensional, but that is just an implementation, not a critical difference.
    But a major difference is, given that we didn't have example code or picture of how the OP layed out the pictureboxes, I chose in my example to create and layout the pictureboxes dynamically at runtime to simplify someone running my example, and .paul. wrote his code to work with a prexisting matrix of pictureboxes.

    My code will have to be understood and adapted into existing code, whereas .paul.s, because it is designed to extract the positions and create the picturebox list from pictureboxes already (it is presumed) existing on the form, and use Min, Max and GetUpperBound calls to treat the matrix of pictureboxes in a reusable/adaptive manner, should have made it much easier to plug into the existing code.

    The OP in the original thread said it would be a 12x12 array.
    In this thread, it is now an 11x11 array.
    But, because .paul.s code is adaptive, the code doesn't have to be modified to accomodate different matrix sizes. It will work with any square matrix of pictureboxes, whether 3x3, 20x20, or any reasonable size you would desire.

    So, to hopefully rectify the pass on what .paul. did in the first thread, I'll add some of .paul.s code to my example to illustrate how easy it should have been to insert it into the OPs original code (unless it is really messed up), and add a few comments that might help understand what his code is doing.
    There isn't that much to add.
    Search for the '.paul. in the code to see where it was modified.
    I only replaced my RotateLeft function with his, so you can see how it fits in. The Right rotation would be just as simple but is left as an exercise for the interested.

    First, of course is the declaration of the two arrays that .paul. uses.
    '.paul. code test
    Private pictureboxes() As PictureBox
    Private originalLocations() As Point

    Array pictureboxes will hold a reference to all the pictureboxes on the form (here is the one possible catch, if you have more pictureboxes on the form that are not part of the matrix, it will pick those up too).
    Array originalLocations will hold the original, unrotated, locations of all the pictureboxes, which is needed both to determine where boxes need to go and tie the original box locations with the original boxes so you have a base reference that can identify what row,column the picturebox was originally associated with regardless of where it has been moved because of rotations.

    Next is the initialization of those arrays, which uses Linq to find, and collect the desired information and build the arrays.
    All controls of type PictureBox are collected and converted to an array assigned to pictureboxes.
    All controls of type PictureBox are found, and their location is collected, converted to an array and assigned to originalLocations.
    '.paul. code
    pictureboxes = Me.Controls.OfType(Of PictureBox).ToArray
    originalLocations = Me.Controls.OfType(Of PictureBox).Select(Function(pb) pb.Location).ToArray

    Then, I merely substituded a Sub called paulRotateLeft, which contains the picturebox RotateLeft relocation portion of .paul.s code (from his Button1_Click) to be called instead of my RotateLeft sub, so you see that it accomplishes the exact same thing, but uses Min, Max and GetUpperBound to be more generic and work regardless of matrix size.
    I had to add one line to .pauls. code to update LastRotate variable I created to use in repositioning and resizing the pictureboxes with form resize.

    Last, in my Resize code I had to update .paul.s originalLocations array since I was modifying the original position of the pictureboxes in the resize event.
    So, when you look at it, very little code had to be added to implement .paul.s solution, so that should be the way to go, since you already have existing pictureboxes.
    (unless you want to give more detail and we can perhaps remove the pictureboxes altogether).

    The updated code, with .paul. code inserted.
    Code:
    Option Explicit On
    
    Public Class Form1
    
      Private PBarray(10, 10) As PictureBox  'Array of pictureboxes
      Private PBOrigLocation(10, 10) As Point 'Array of locations the picturebox are originally assigned
      Private LastRotate As Integer
    
      '.paul. code test
      Private pictureboxes() As PictureBox
      Private originalLocations() As Point
    
      Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim boxSize As Integer
        Dim MinSize As Integer = Math.Min(Me.ClientSize.Width, Me.ClientSize.Height) 'find the lesser value, height or width
        Dim cnt As Integer       'just to count the picturebox and put the count as an image in the pictureboxes
        boxSize = MinSize \ 11   'Fit 11x11 squares inside the form
        Me.KeyPreview = True     'Use the keys a,s,d,f to select one of the rotations
    
        For y As Integer = 0 To 10
          For x As Integer = 0 To 10
            PBOrigLocation(x, y) = New Point(x * boxSize, y * boxSize)  'Save the location in the array
            PBarray(x, y) = New PictureBox                              'Create a new picturebox
            cnt += 1                                                    'increment the count of pictureboxes
            With PBarray(x, y)                                          'With the picturebox created
              .Tag = cnt                                                '  Put the count in the tag so we can reference it
              .SizeMode = PictureBoxSizeMode.StretchImage               '  If we resize the picturebox, stretch the image
              .Location = PBOrigLocation(x, y)                          '  Set the location to the calculated location
              .ClientSize = New Size(boxSize, boxSize)                  '  Size the box to our calculated size
              .BorderStyle = BorderStyle.FixedSingle                    '  Display a border so we see the size of the box
              .Image = New Bitmap(boxSize, boxSize)                     '  Create a new bitmap and assign it to the picturebox
              Using g As Graphics = Graphics.FromImage(.Image)          '  Using a graphic object created for that image
                g.ScaleTransform(boxSize / 24, boxSize / 24)               ' Scale the text to fit about 3 chars in the box
                g.DrawString(cnt.ToString, Me.Font, Brushes.Black, 0, 0)   ' Draw the box number (count) to the image
              End Using                                                 '  Release the Graphics object
              .Visible = True                                           '  make the picturebox visible
              Me.Controls.Add(PBarray(x, y))                            '  Add it to the form's control collection
              AddHandler .MouseDown, AddressOf Pb_MouseDown             '  Add a mouseDown handler for the picturebox
            End With
          Next
        Next
    
        '.paul. code
        pictureboxes = Me.Controls.OfType(Of PictureBox).ToArray
        originalLocations = Me.Controls.OfType(Of PictureBox).Select(Function(pb) pb.Location).ToArray
    
      End Sub
    
      Private Sub Form1_KeyPress(sender As Object, e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        Select Case e.KeyChar.ToString.ToLower
          ' Case "a" : rotateLeft()
          Case "a" : paulRotateLeft()
          Case "s" : noRotate()
          Case "d" : rotateRight()
          Case "f" : rotate180()
        End Select
      End Sub
    
    
      Private Sub Pb_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs)
        Dim bp As PictureBox = DirectCast(sender, PictureBox)
        If e.Button = Windows.Forms.MouseButtons.Left Then
          bp.BackColor = Color.Aqua  'Change the backcolor of the mouse clicked on
        Else
          bp.BackColor = Control.DefaultBackColor
        End If
        Me.Text = bp.Tag.ToString 'Print the tag value to the titlebar to show we know which box was clicked
      End Sub
    
      Private Sub noRotate()
        LastRotate = 0
        For Y As Integer = 0 To 10
          For X As Integer = 0 To 10
            PBarray(X, Y).Location = PBOrigLocation(X, Y)     'Everything in its original place
          Next
        Next
      End Sub
    
      Private Sub rotateRight()
        LastRotate = 1
    
        For Y As Integer = 0 To 10
          For X As Integer = 0 To 10
            PBarray(X, Y).Location = PBOrigLocation(10 - Y, X)  'Make 1 to 11 go down the right column
          Next
        Next
    
      End Sub
    
      Private Sub paulRotateLeft()
        '.paul. code to rotate left
        Dim minYLocation As Integer = originalLocations.Min(Function(p) p.Y)
        Dim maxYLocation As Integer = originalLocations.Max(Function(p) p.Y)
        LastRotate = 2 'add my piece so we refresh when resized
        For x As Integer = 0 To pictureboxes.GetUpperBound(0)
          pictureboxes(x).Location = New Point(originalLocations(x).Y, minYLocation + maxYLocation - originalLocations(x).X)
        Next
    
      End Sub
    
      Private Sub rotateLeft()
        LastRotate = 2
    
        For Y As Integer = 0 To 10
          For X As Integer = 0 To 10
            PBarray(X, Y).Location = PBOrigLocation(Y, 10 - X) 'Make 1 to 11 go up the left column
          Next
        Next
      End Sub
    
      Private Sub rotate180()
        LastRotate = 3
    
        For Y As Integer = 0 To 10
          For X As Integer = 0 To 10
            PBarray(X, Y).Location = PBOrigLocation(10 - X, 10 - Y)  'Make 1 to 11 go right to left, on the bottom row
          Next
        Next
      End Sub
    
      Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
        Dim boxSize As Integer
        Dim MinSize As Integer = Math.Min(Me.ClientSize.Width, Me.ClientSize.Height) 'find the lesser value, height or width
        boxSize = MinSize \ 11   'Fit 11x11 squares inside the form
        If PBarray(0, 0) IsNot Nothing Then
          For y As Integer = 0 To 10
            For x As Integer = 0 To 10
              PBOrigLocation(x, y) = New Point(x * boxSize, y * boxSize)  'Save the location in the array
              PBarray(x, y).ClientSize = New Size(boxSize, boxSize) 'resize the pictureboxes for the new size
    
              'Update .paul. originalLocations array (convert my 2D to his 1D array)
              originalLocations(y * 11 + x) = PBOrigLocation(x, y)
    
            Next
          Next
          Select Case (LastRotate)
            Case 0 : noRotate()
            Case 1 : rotateRight()
            Case 2 : paulRotateLeft() 'rotateLeft()
            Case 3 : rotate180()
          End Select
        End If
      End Sub
    
    
    End Class

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    27

    Re: Rotating Picturebox Array...Again

    Passel: Sorry i have been busy the past few days and haven't had time to work on that program, but i did use some of your code and ideas and it worked perfectly. Now i'm just trying to make it rotate with a button to rotate right and a button to rotate left, but that shouldn't be too difficult to do. It makes alot more sense now too using the array of points instead of two arrays of pictureboxes. Thanks!!!!!! You guys are the best!

    How do i mark this as resolved?
    Last edited by JaneTheSardine; Jan 22nd, 2014 at 12:13 PM.

  15. #15
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,723

    Re: Rotating Picturebox Array...Again

    Quote Originally Posted by JaneTheSardine View Post
    ... How do i mark this as resolved?
    You would think that might be a Frequently Asked Question, let's check it out.

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