none
Visual Basic Slideshow Help RRS feed

  • Question

  • Hi all, 

    I've been desperately scouring the internet for some help on this topic, but have unfortunately come up short.

    I was trying to create a Form Application that acts as a slideshow, and was successful by using Timer_Tick event (code below)...however; after talking to my college tutor apparently the assignment criteria calls for the use of a 'While Loop'. 

    I have been trying for a while now to incorporate a While Loop into the main body of this code, but I am unfortunately breaking it left right and centre.  

    If anyone has any hints or tips I'd be very grateful. 

    Kind regards, Will Hammond. 

    Public Class Form1
        Dim count As Integer
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            count = count + 1
            If count = 1 Then
                PictureBox1.Image = Image.FromFile("C:\Users\William Hammond\Desktop\JPEGs\seagull.jpeg")
            ElseIf count = 2 Then
                PictureBox1.Image = Image.FromFile("C:\Users\William Hammond\Desktop\JPEGs\car.jpg")
    
                count = 0
            End If
        End Sub
    End Class

    Tuesday, January 9, 2018 8:15 PM

All replies

  • I was trying to create a Form Application that acts as a slideshow, and was successful by using Timer_Tick event (code below)...however; after talking to my college tutor apparently the assignment criteria calls for the use of a 'While Loop'.

    You should ask for an explanation of how a While loop fits this task.  A timer and the tick event is the correct way to do a slideshow.  Windows is event driven, and changing the image should be in response to an event, such as the timer tick or a mouse click.   Trying to do it with a While loop will only work if you can somehow delay the program for an interval.
    https://msdn.microsoft.com/en-us/library/system.threading.thread.sleep(v=vs.110).aspx

    Tuesday, January 9, 2018 9:01 PM
  • Will,

    I think you're making a mistake about how to start out.

    Think more abstractly; for example, you'll have a collection of images from some source. With a collection there's a first and a last. Under some situations there will be a next and a previous.

    With that, what activates those states isn't important yet. It can later be a timer's tick or button press or who knows what.

    See what I mean at all?


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Tuesday, January 9, 2018 9:56 PM
  • If you can post the requirements as stated by your teacher or instructor then many of us would be interested in what that is. If they truly do expect you to use a while loop then it sounds like the instructor's instructions are unrealistic.

    Something else that would be relevant is if your tutor misunderstood the requirements. That seems to be a possibility.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, January 9, 2018 10:08 PM
  • Hi WIill,

    If you want to do sildeshow in the winform, why you must use While in your code? We can use different code to achieve the same function, foe example i do one example about sildeshow, please take a look.

    I create folder (images) to store images firstly, put one Timer, one picturebox, one ToolStrip in the form, set Timer.enable =true

     'Array of images
        Dim pictures() As String = {"apples.jpg", "flower1.jpg", "mountain.jpg", "sunset.jpg", "waterfall.jpg"}
        'This wil be used to increment array index.
        Dim i As Integer = 0
        Private Sub Frmsildershow_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            'Set the first image when form loads.
            PictureBox1.Image = Image.FromFile("images/" + pictures(0))
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            'increment i by 1
            i += 1
            'if I = pictures.length set it to 0
            If pictures.Length = i Then
                i = 0
            End If
            'Load image.
            PictureBox1.Image = Image.FromFile("images/" + pictures(i))
    
        End Sub
    
        Private Sub BtnPrevious_Click(sender As Object, e As EventArgs) Handles BtnPrevious.Click
            'Disable timer, prevents next image from loading..
            Timer1.Enabled = False
            'if I = 0 set it to pictures.length
            If i = 0 Then
                i = pictures.Length
            End If
            'decrement by 1
            i -= 1
            'Load image
            PictureBox1.Image = Image.FromFile("images/" + pictures(i))
            'Re-Enable timer, resets and image loads in 5 seconds..
            Timer1.Enabled = True
    
        End Sub
    
        Private Sub BtnNext_Click(sender As Object, e As EventArgs) Handles BtnNext.Click
            'Disable timer, prevents next image from loading..
            Timer1.Enabled = False
            'increment by 1
            i += 1
            'if I = pictures.length set it to 0
            If pictures.Length = i Then
                i = 0
            End If
            'Load image.
            PictureBox1.Image = Image.FromFile("images/" + pictures(i))
            'Re-Enable timer, resets and image loads in 5 seconds..
            Timer1.Enabled = True
        End Sub

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, January 10, 2018 9:21 AM
    Moderator
  • Will,

    You can make a timer from a while loop like this. The problem is that when running in a loop like this the application does not respond to events. There are many ways to avoid that. First is don't do it this way.

    There is an old vb function DoEvents which will allow your application to respond to events by releasing control back to the system when executed. Then your app will process events.

    Some will poo poo using doevents because when the events are processed it can cause unexpected results and be hard to debug and maintain. However for a simple thing like this example it works.

    To see how the do events works, remove it from the example and run the example. Then try to move the form or click the stop button while the timer is running, nothing will happen, the app is not responding to the events. Add the doevents statement back and run. Now when you click the stop button the app responds to the click event.

    I have used some other things you may not have learned yet. But its just to show you.

    You will have to figure out the rest of the slide show and what is appropriate for your class work. This example is just to get you using a while and thinking about what you want to do for your project. You should probably discuss the example with your instructor and tell them where you got it.

    The button controls are made in the code. Just cut and paste the example into an empty form and run.

    Public Class Form6
        Private WithEvents button1 As New Button With {.Parent = Me, .Location = New Point(100, 200), .Text = "Start"}
        Private WithEvents button2 As New Button With {.Parent = Me, .Location = New Point(200, 200), .Text = "Stop", .Enabled = False}
        Private nextTime As Date
        Private stopShow As Boolean = True
    
        Private Sub Form6_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
        End Sub
    
        Private Sub Form6_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            With e.Graphics
                Using f As New Font("tahoma", 18, FontStyle.Bold)
                    .Clear(Color.Maroon)
                    .DrawString(Now.TimeOfDay.ToString, f, Brushes.AntiqueWhite, 30, 50)
                    .DrawString(nextTime.TimeOfDay.ToString, f, Brushes.AntiqueWhite, 30, 100)
                End Using
            End With
        End Sub
    
        Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click
            'add five seconds to the current time for the stop time
            nextTime = Now.AddSeconds(5)
            button1.Enabled = False
            button2.Enabled = True
            stopShow = False
    
            While Now < nextTime
    
                Refresh()
    
                Application.DoEvents()
    
                If stopShow Then Exit While
            End While
    
    
            button1.Enabled = True
            button2.Enabled = False
        End Sub
    
        Private Sub button2_Click(sender As Object, e As EventArgs) Handles button2.Click
            stopShow = True
        End Sub
    End Class

    Wednesday, January 10, 2018 10:19 AM
  • Here is a very simple example using a While loop. ;-)

            Dim pictureFolder As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
            Dim fileEntries As String() = Directory.GetFiles(pictureFolder)
            Dim fileName As String
            For Each fileName In fileEntries
                PictureBox1.Image = Image.FromFile(fileName)
                Dim slideInterval As New Stopwatch
                slideInterval.Start()
                'slide show delay of five seconds
                While slideInterval.ElapsedMilliseconds <= 5000
                    System.Windows.Forms.Application.DoEvents()
                End While
            Next filename


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, January 10, 2018 2:33 PM
  • There is an old vb function DoEvents which will allow your application to respond to events by releasing control back to the system when executed. Then your app will process events.

    Some will poo poo using doevents because when the events are processed it can cause unexpected results and be hard to debug and maintain. However for a simple thing like this example it works.

    That is right, DoEvents is old. It was created in the older unmanaged VB due to limitations in unmanaged VB. When Microsoft designed the improvements for VB.Net they designed it so that VB can do nearly everything that C# can do. So in VB.Net there is never any need for DoEvents.

    Those of us accustomed to doing things the Windows API way, such as the way that a C++ Windows API programmer would do it (using messages sent by Windows and using callbacks) don't like DoEvents because we know it is unnecessary and we know how to do it the Windows way. I have never seen a use of DoEvents that can't be done in a better way. It is usually both a problem and a solution, such as here. In this, the while loop just keeps the application busy doing something when there is nothing to do. That is very unfriendly to the other applications in the system. Other applications can be limited in their ability to run. When there are multiple cores then at least the while loop is limited to just one core.



    Sam Hobbs
    SimpleSamples.Info

    Wednesday, January 10, 2018 7:47 PM
  • Hi all,

    Thank you all for commenting, I really appreciate the quick responses and helpful information that was in them. 

    I was able to figure something out thanks to everyone. 

    Code is below (: 

    Public Class Form1
        Dim count As Integer
        Dim images As New Collection
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            count = count + 1
            PictureBox1.Image = Bitmap.FromFile(images(count))
            While images.Count() = count
                count = 0
            End While
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            images.Add("D:\William\Pictures\Me n Hannah\hw1.jpg")
            images.Add("D:\William\Pictures\Me n Hannah\hw2.jpg")
            images.Add("D:\William\Pictures\Me n Hannah\pic3.jpeg")
            Timer1.Start()
        End Sub
    End Class

    Thursday, January 11, 2018 8:03 PM
  • I was able to figure something out thanks to everyone. 

    That is an innovative solution, but if this is submitted as an assignment you should make it clear that you have done it like that only in order to meet the requirements indicated by the tutor, because that is a very strange usage of a While loop.

    Thursday, January 11, 2018 8:42 PM