Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  A Simple Photo Browser  »  Rearranging Thumbnails at Run-Time
 »  Home  »  Windows Development  »  A Simple Photo Browser  »  Rearranging Thumbnails at Run-Time
 »  Home  »  Windows Development  »  Win Forms  »  A Simple Photo Browser  »  Rearranging Thumbnails at Run-Time
A Simple Photo Browser
by Chris Mills | Published  04/14/2006 | .NET Newbie Windows Development Win Forms | Rating:
Chris Mills
Chris graduated from the Robert Gordon University, Aberdeen UK in 2002 with an MEng in Electronic and Computer Engineering. Chris now works for an engineering company based in Aberdeen and develops condition monitoring and data logging solutions (both hardware and software development), he also develops tools for data analysis, visualisation and data mining.  

View all articles by Chris Mills...
Rearranging Thumbnails at Run-Time

The final change to our application in this article is modifying it to rearrange the thumbnails displayed on the panel pnlThumbnails if it is resized.  We used the Dock Properties of many of the components on the form so they would automatically be easily resized with the form they are on - but if we want the thumbnails to be rearranged when the panel that contains them is resized we will have to do it ourselves!

We'll start by adding a function to rearrange the thumbnails if the panel containing them is resized.  This function calls the "SetThumbnailPosition" function we created earlier for each thumbnail on the panel containing them.  We also get the current AutoScrollPosition for the panel when we start the operation and reset it to this position when the redraw is complete; this will prevent the thumbnails panel from being scrolled back to the top after the redraw. Add the following function to your code:

    Private Sub RearrangeThumbnails(ByRef cntlThumbnailContainer As ScrollableControl)

        Dim pOldScrollPos As Point
        Dim iAddedThumbnails As Integer = 0

        ' Get the Current Autoscroll position of the control
        With cntlThumbnailContainer.AutoScrollPosition
            pOldScrollPos = New Drawing.Point(Math.Abs(.X), Math.Abs(.Y))
        End With

        ' Set the new position of each thumbnail to fill the ScrollableControl using
        ' the SetThumbnailPosition function
        For Each tnThumbnail As Thumbnail In cntlThumbnailContainer.Controls
            SetThumbnailPosition(tnThumbnail, cntlThumbnailContainer, iAddedThumbnails)
            iAddedThumbnails += 1  ' Increment the count of added thumbnails by one
        Next

        ' Set the containers AutoScroll position to where it was before it was resized
        cntlThumbnailContainer.AutoScrollPosition = pOldScrollPos
        cntlThumbnailContainer.AutoScroll = True

    End Sub

We now add an event handler that will call this function when the panel is resized.  The function is shown below.

    Private Sub pnlThumbnails_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles pnlThumbnails.Resize
        RearrangeThumbnails(DirectCast(pnlThumbnails, ScrollableControl))
    End Sub

All modifications are now complete and the application is ready for use!  Whilst its functionality is somewhat limited compared with many modern photo editing applications, it does allow the user to browse and view photos with relative ease. 


This is the end of this article, but obviously there is loads of scope for improvement in the application.  You are free to use the code in this article in any of your own projects.  The photo browser could be improved dramatically by relatively simple tasks like improving the user interface by adding context menus, toolbar or statusbar, or by adding the ability to save modified images (the Image object has methods for this too) - where you take it from here is up to you, but the possibilities are almost endless!

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:4.91304347826087 out of 5
 23 people have rated this page
Article Score27239
Attachments
Comments    Submit Comment

Comment #1  (Posted by Ged on 04/14/2006)
Rating
Clear comprehensive and useful - Great stuff - Thanks!
 
Comment #2  (Posted by an unknown user on 04/21/2006)
Rating
WOW! Great! Thanks!
 
Comment #3  (Posted by an unknown user on 04/24/2006)
Rating
Very nice, would make a perfect tutorial for potential .NET programmers. Thanx
 
Comment #4  (Posted by Javier on 05/03/2006)
Rating
Excelent article, but I have a question: Does it work with vb6? Thanks
 
Comment #5  (Posted by Javier on 05/03/2006)
Rating
Hi Chris. My name is Javier. I´m writing from Argentina. I had read your article "A Simple Photo Browser" and I wish to know if that code works in vb6? I have to develop an app like that and I find your article very useful. Thank you.

PD: sorry for my bad English
 
Comment #6  (Posted by an unknown user on 10/07/2006)
Rating
Some weeks ago, I tried to make a user control on my own, but it didn't work. Now I finally got a great example of one. Thanks
 
Comment #7  (Posted by an unknown user on 11/10/2006)
Rating
Very clear and completely documented explanation!
thx
 
Comment #8  (Posted by an unknown user on 11/10/2006)
Rating
Very clear and completely documented explanation!
thx
 
Comment #9  (Posted by an unknown user on 11/21/2006)
Rating
Everything is clear and easy to understand. It helped me a lot. Thanks.
 
Comment #10  (Posted by an unknown user on 12/30/2006)
Rating
The best I've seen so far... Thanks.
 
Comment #11  (Posted by an unknown user on 01/23/2007)
Rating
Clear,Understandable and pupil not assumed to be a .Net expert.
 
Comment #12  (Posted by Graham on 01/24/2007)
Rating
This will not run, there is an error with
Dim tnThumbnail As Thumbnail
Squiggley under Thumbnail
Error Message = Type 'Thumbnail' not Defined.
 
Comment #13  (Posted by an unknown user on 01/28/2007)
Rating
This is very nice. Cleared all my querries.
 
Comment #14  (Posted by Asha on 07/13/2007)
Rating
Excellent article.Keep up the good work.Great description of code too!!
 
Comment #15  (Posted by an unknown user on 11/03/2007)
Rating
Thank you so much
 
Comment #16  (Posted by an unknown user on 11/05/2007)
Rating
Excellent article, Chris
 
Comment #17  (Posted by an unknown user on 01/02/2008)
Rating
Great article. I found it just when I need it.
 
Comment #18  (Posted by an unknown user on 02/17/2008)
Rating
thanks so much, it's really helpful
 
Sponsored Links