Results 1 to 29 of 29

Thread: Overlapping Pictureboxes when turning

Threaded View

  1. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Overlapping Pictureboxes when turning

    I gave an example of one way to set up a control array to make looping the code easier back in post #6.

    You can create an array of the control type you want, in this case TextBox, and then assign a reference to your textboxes to that array when you start up.
    You can then reference the textboxes using the array reference, or the control reference, whichever is convenient.
    Code:
    'In your form's declaration area
      Private TxtModulos() As TextBox
    
    
    'In your form load event
       TxtModulos = {TxtModulo1, TxtModulo2, TxtModulo3, TxtModulo4, TxtModulo5, TxtModulo6, TxtModulo7, TxtModulo8, TxtModulo9, TxtModulo10} 
    
    'And in your code
      For i as Integer = 0 to 9
        TxtModulos(i).Text = Math.Round(ModuloPos(i))
      Next
    '
    '
    '
    Quote Originally Posted by DesPurpleLightning View Post
    ...
    I'm calculating the position of every mover every 100ms. And i'm doing this still 10 times..
    If i make a function so i only do it once. But repeat it 10 times. Will this be faster?
    I'm not quite sure what you mean here, but I would have a single timer that ticked at 100ms (in truth, that will probably be 109ms so you will probably be updating at around 9.2 hz, not 10 hz, but being off by about 10% may not be an issue with what you're doing).

    So, in the Single Timer's Tick Event, I would
    1. Calculate the new position of every mover.
    2. Invalidate the Picturebox so it redraws the movers at the new locations
    3. Update any other GUI controls like textboxes.

    Step 3 could probably be included as part of step 1.
    The important part would be to avoid doing any invalidation in your position calculations.
    Only doing the invalidation once, will mean you refresh your image at about 10 hz, whereas if you cause an invalidation as part of you individual, you could potentially be causing the screen to update 100 times per second instead of 10.
    But that would be more likely if you had 10 different timers, as it looked like your original post's code seemed to indicate. The window updates could "sneak" in between the various timer ticks.
    If you update all 10 positions one after the other within a single timer event, then the window refreshes are not going to happen in that string of processing because your code doesn't exit out of the event handler between updates, so the window can't process a OnPaint message. Multiple invalidations would combined and result in only one Paint event (ideally), but it would be best to design your code (in most scenarios) to only trigger one invalidate to refresh the window, not multiple ones.

    Which also brings me to the following code.
    Quote Originally Posted by DesPurpleLightning View Post
    ...
    Code:
       Private Sub PbXTS_Paint(sender As Object, e As PaintEventArgs) Handles PbXTS.Paint
    
            Dim g As Graphics = e.Graphics
            g.DrawImage(PbAs1.Image, PbAs1.Location)
            g.DrawImage(PbAs2.Image, PbAs2.Location)
            g.DrawImage(PbAs3.Image, PbAs3.Location)
            g.DrawImage(PbAs4.Image, PbAs4.Location)
            g.DrawImage(PbAs5.Image, PbAs5.Location)
            g.DrawImage(PbAs6.Image, PbAs6.Location)
            g.DrawImage(PbAs7.Image, PbAs7.Location)
            g.DrawImage(PbAs8.Image, PbAs8.Location)
            g.DrawImage(PbAs9.Image, PbAs9.Location)
            g.DrawImage(PbAs10.Image, PbAs10.Location)
            PbXTS.Invalidate(Bounds)
    
        End Sub
    That seems like it would cause major issues, and I'd be surprised if it worked very well at all.
    You essentially have a circular reference (fortunately not recursive), that should cause Paint events for the picturebox to be continually generated as fast as Windows will process them.

    You just got done updating the picturebox with all your new drawing, and the last line says to invalidate all that work and redraw the picturebox again.
    In fact, Bounds refers to the Form's Bounds, so by definition is probably larger than the bounds of your picturebox, and depending on where the form is on the desktop may be just odd, causing some portions of the picturebox to not be included in the invalidation.
    Remove that line from that code, and add it like I said, to your timer code after you've calculated all the new positions (i.e. step 2).
    Also, remove the Bounds argument, just invalidate the whole picturebox.

    PbXTS.Invalidate()
    Last edited by passel; Nov 12th, 2015 at 12:15 PM.

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