Trying to make a panel control that automaticly generates a thumbnail for the pictures added to it.

I cant seem to find anything wrong with the code I have except that it only displays the first picturebox added, it does add the other ones to the panels controls but they are not shown although there visible properties are true.

can anyone tell me why this is happening???

{cThumbnail.vb is attached (also was throughly debuged and works properly)}

pnl is a panel control on the user control


VB Code:
  1. Public Class ThumbPanel
  2.     Inherits System.Windows.Forms.UserControl
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.  
  14.     End Sub
  15.  
  16.     'UserControl1 overrides dispose to clean up the component list.
  17.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  18.         If disposing Then
  19.             If Not (components Is Nothing) Then
  20.                 components.Dispose()
  21.             End If
  22.         End If
  23.         MyBase.Dispose(disposing)
  24.     End Sub
  25.  
  26.     'Required by the Windows Form Designer
  27.     Private components As System.ComponentModel.IContainer
  28.  
  29.     'NOTE: The following procedure is required by the Windows Form Designer
  30.     'It can be modified using the Windows Form Designer.  
  31.     'Do not modify it using the code editor.
  32.     Friend WithEvents pnl As System.Windows.Forms.Panel
  33.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  34.         Me.pnl = New System.Windows.Forms.Panel
  35.         Me.SuspendLayout()
  36.         '
  37.         'pnl
  38.         '
  39.         Me.pnl.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  40.                     Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
  41.         Me.pnl.AutoScroll = True
  42.         Me.pnl.Location = New System.Drawing.Point(0, 0)
  43.         Me.pnl.Name = "pnl"
  44.         Me.pnl.Size = New System.Drawing.Size(496, 152)
  45.         Me.pnl.TabIndex = 0
  46.         '
  47.         'ThumbPanel
  48.         '
  49.         Me.Controls.Add(Me.pnl)
  50.         Me.Name = "ThumbPanel"
  51.         Me.Size = New System.Drawing.Size(232, 152)
  52.         Me.ResumeLayout(False)
  53.  
  54.     End Sub
  55.  
  56. #End Region
  57.  
  58.     Public Event Thumbnail_SingleClicked(ByRef ctrlControl As Control)
  59.     Public Event Thumbnail_DoubleClicked(ByRef ctrlControl As Control)
  60.  
  61.     'Properties
  62.     'Thumbnail Properties
  63.     Private m_ThumbMaxWidth As Long
  64.     Private m_ThumbMaxHeight As Long
  65.     'Spacing Settings
  66.     Private m_ThumbSpaceWidth As Long ' How much distance between Thumbnails
  67.     Private m_ThumbSpaceHeight As Long ' how much distance between Rows
  68.  
  69.     Public Property MaxThumbnailWidth() As Long
  70.         Get
  71.             Return m_ThumbMaxWidth
  72.         End Get
  73.         Set(ByVal Value As Long)
  74.             m_ThumbMaxWidth = Value
  75.         End Set
  76.     End Property
  77.  
  78.     Public Property MaxThumbnailHeight() As Long
  79.         Get
  80.             Return m_ThumbMaxHeight
  81.         End Get
  82.         Set(ByVal Value As Long)
  83.             m_ThumbMaxHeight = Value
  84.         End Set
  85.     End Property
  86.  
  87.     Public Property ThumbSpaceWidth() As Long
  88.         Get
  89.             Return m_ThumbSpaceWidth
  90.         End Get
  91.         Set(ByVal Value As Long)
  92.             m_ThumbSpaceWidth = Value
  93.         End Set
  94.     End Property
  95.  
  96.     Public Property ThumbSpaceHeight() As Long
  97.         Get
  98.             Return m_ThumbSpaceHeight
  99.         End Get
  100.         Set(ByVal Value As Long)
  101.             m_ThumbSpaceHeight = Value
  102.         End Set
  103.     End Property
  104.  
  105.     'Methods
  106.  
  107.     Public Function AddImage(ByRef imgImage As Image) As Long
  108.         'Returns ControlID
  109.         Dim Thumb As New cThumbnail
  110.  
  111.         With Thumb
  112.             .MaxThumbnailHeight() = m_ThumbMaxHeight
  113.             .MaxThumbnailWidth() = m_ThumbMaxWidth
  114.             .SourceImage() = imgImage
  115.             .GenerateThumbnail()
  116.  
  117.             pnl.Controls.Add(GetPictureBox(.ThumbnailImage))
  118.             CType(pnl.Controls.Item(pnl.Controls.Count - 1), Windows.Forms.PictureBox).Image = .ThumbnailImage
  119.             pnl.Controls.Item(pnl.Controls.Count - 1).Update()
  120.         End With
  121.     End Function
  122.  
  123.     Private Function GetPictureBox(ByRef imgImage As Image) As PictureBox
  124.         Dim pic As New PictureBox
  125.  
  126.         With pic
  127.             .BorderStyle = BorderStyle.FixedSingle
  128.             .SizeMode = PictureBoxSizeMode.CenterImage
  129.             .Height = m_ThumbMaxHeight
  130.             .Width = m_ThumbMaxWidth
  131.             NewTopLeft(.Top, .Left)
  132.             .Image = imgImage
  133.             .Visible = True
  134.         End With
  135.  
  136.         'Create Handlers
  137.  
  138.         AddHandler pic.Click, New System.EventHandler(AddressOf SingleClicked)
  139.         AddHandler pic.DoubleClick, New System.EventHandler(AddressOf DoubleClicked)
  140.  
  141.         Return pic
  142.     End Function
  143.  
  144.     Private Function NewTopLeft(ByRef lngNewTop As Long, ByRef lngNewLeft As Long)
  145.         Dim ctrl As Control
  146.  
  147.         If pnl.Controls.Count = 0 Then
  148.             'No pictures have been added yet!
  149.             lngNewTop = m_ThumbSpaceHeight
  150.             lngNewLeft = m_ThumbSpaceWidth
  151.             Exit Function
  152.         End If
  153.  
  154.         ctrl = pnl.Controls.Item(Me.Controls.Count - 1)
  155.  
  156.         lngNewTop = ctrl.Top + ctrl.Height + m_ThumbSpaceHeight
  157.         lngNewLeft = ctrl.Left + ctrl.Width + m_ThumbSpaceWidth
  158.  
  159.         If pnl.Width < (lngNewLeft + m_ThumbMaxWidth) Then
  160.             'we need to go down and all the way to the left
  161.             'so the left is the easiest
  162.             lngNewLeft = m_ThumbSpaceWidth
  163.             lngNewTop = lngNewTop + m_ThumbMaxHeight + m_ThumbMaxHeight
  164.         End If
  165.     End Function
  166.  
  167.     Public Function ClearAll()
  168.         pnl.Controls.Clear()
  169.     End Function
  170.  
  171.     'Picturebox Event Handlers
  172.  
  173.     Private Sub SingleClicked(ByVal sender As Object, ByVal e As System.EventArgs)
  174.         RaiseEvent Thumbnail_SingleClicked(sender)
  175.     End Sub
  176.  
  177.     Private Sub DoubleClicked(ByVal sender As Object, ByVal e As System.EventArgs)
  178.         RaiseEvent Thumbnail_DoubleClicked(sender)
  179.     End Sub
  180.  
  181. End Class