Results 1 to 6 of 6

Thread: Using PictureBox with FOR NEXT Loop

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2017
    Posts
    26

    Using PictureBox with FOR NEXT Loop

    I haven't done anything with programming since Apple BASIC back in high school. I bought Visual Studio and started
    playing around with Visual Basic.

    I started a Windows Form application, inserted a picturebox in the form and then put in an image to display (PICTURE1.jpg). I
    added 2 more pictures to My.Resources as well (PICTURE2.jpg & PICTURE3.jpg). All pictures are the same size.

    I double clicked the picture in the form to setup a ClickEvent.

    In code view, it showed


    Code:
    Public Class Form1
    
    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
    
    End sub
    
    End Class
    After I inserted into the private sub: PictureBox1.Image = My.Resources.PICTURE2 the program runs
    by showing PICTURE1 and when I click anywhere on the picture it shows/replaces PICTURE 1 with PICTURE2.
    Left-clicking on PICTURE2 does nothing further and the form will only exit by manually closing it. So, that looked like this:
    Code:
    Public Class Form1
    
    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
     PictureBox1.Image = My.Resources.PICTURE2
    End sub
    
    End Class
    ---------------------------------------------------------------------------------------------------
    I want to show/replace the 3rd picture, PICTURE3, when left-clicking on PICTURE2 and then, when one
    left-clicks on PICTURE3, exit the program. I thought of using a For Next loop to do it:
    Code:
     For i = 1 To 3
                If i = 1 Then PictureBox1.Image = My.Resources.PICTURE2
                If i = 2 Then PictureBox1.Image = My.Resources.PICTURE3
                If i = 3 Then Application.Exit()
            Next
    but placing it between the
    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click

    End sub

    doesn't work. The first picture was displayed but left-clicking on it just exited the program. I then tried putting the FOR NEXT
    outside of the Private Sub (the Private Sub contained within) but that didn't work either. Any help would be appreciated.
    Last edited by Shaggy Hiker; Jan 11th, 2017 at 04:42 PM. Reason: Added CODE tags

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Using PictureBox with FOR NEXT Loop

    Welcome to the forums. I edited your post to add [CODE][/CODE] tags, which you can do by pressing the # button and pasting or typing the code between the tags, or adding the code, then selecting it and pressing the # tag. It makes it look a bit nicer.

    A For Next loop won't work because it will happen to fast. In fact, what is happening is that the second image is loaded, then the third image, then the application exits. There is never a pause in there, so the application never gets a chance to redraw the screen, so you never see the images. You wouldn't anyways, cause it would all happen in a tiny fraction of a millisecond, so the screen wouldn't even flash before it finished.

    What you could do would be to add an integer variable at form scope (not in the click handler, though there would be a way to do it in the click handler, too). In the click handler, add 1 to the integer variable. So, the click handler might look like this:
    Code:
    private myCount As Integer
    
    Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
     myCount+=1
     If myCount = 1 Then
       PictureBox1.Image = My.Resources.Picture2
     ElseIf myCount = 2 Then
      PictureBox1.Image = My.Resources.Picture3
     Else
      Application.Exit()
     End If
    End sub
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2017
    Posts
    26

    Re: Using PictureBox with FOR NEXT Loop

    I thank you for the help and I have tried your example and it works I'm not sure why it's different than the FOR NEXT. I thought the waiting for the click event would provide the pause. They both have a variable that's being incremented (I in my FOR NEXT example) and myCount in your code but I guess it's enough to know it is different. I also see how you added 1 to your variable myCount. I would have used myCount = myCount + 1 so I'll save the way you did it for future reference too.

    Thanks for explaining about the Code tags and how to use them too. It does look nicer.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Using PictureBox with FOR NEXT Loop

    Quote Originally Posted by Fred Biles View Post
    I thought the waiting for the click event would provide the pause.
    That is actually not correct, the click event starts it off, and the loop happens all 3 times straight away - there is no pausing.

    In order to get a pause between the actions you need to somehow "pause" the code or even "stop" it (as in this case), and as you want user interaction to re-start it, something like Shaggy Hikers suggestion is the way to do it.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Using PictureBox with FOR NEXT Loop

    You can see that by putting a breakpoint in either the loop or non-loop version. When execution stops on the breakpoint, you can keep pressing F11 to step through it. What you will see is that without the loop, the execution goes line by line until it reaches the end of the click handler method. As far as the application is concerned, it then goes into standby, during which time it is processing any messages received. Those messages are user interaction primarily, such as clicks or scrolling, but could also be timer ticks and system events. If you did the same thing with the loop version, you would see that execution would perform all iterations of the loop, one after the other, until the loop is finished, then it would proceed to the end of the click method and go into standby.

    One of the messages that gets handled is Paint messages, which is the only time that the screen draws. When you put a new picture in the picturebox, it doesn't change the screen directly. What happens is that it puts the image into some memory associated with the picturebox and posts a message to indicate that the screen area for the picturebox is invalid and needs to be redrawn. That message won't get processed until the application is in that standby, so the actual drawing doesn't happen when the image is put in the picturebox, it happens after the click event handler has completed and the application is free to process messages. So, with the loop, you do all iterations of the loop before finishing the event handler, and therefore the screen never gets a chance to draw.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jan 2017
    Posts
    26

    Re: Using PictureBox with FOR NEXT Loop

    Thanks for the more detailed explanation. It makes more sense to me now.

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