Public Class Form1
  Inherits System.Windows.Forms.Form
  Private names As ArrayList
  Private fol As Folder
  Private Const pLeft As Integer = 30, pTop As Integer = 30
  Private Const HSpace As Integer = 70, VSpace As Integer = 80
  Private rect As vbPatterns.Rectangle
  Private selectedName As String
  Friend WithEvents Pic As System.Windows.Forms.PictureBox
  Private folFact As FolderFactory
  Private Sub init()
    AddHandler Pic.Paint, New PaintEventHandler(AddressOf picPaint)
    names = New ArrayList()
    names.Add("Adam")
    names.Add("Bill")
    names.Add("Charlie")
    names.Add("Dave")
    names.Add("Edward")
    names.Add("Fred")
    names.Add("George")
    fol = New Folder(Color.Bisque)
    AddHandler Pic.MouseMove, (AddressOf evMouse)
    rect = New vbPatterns.Rectangle()
    rect.setSize(50, 30)
    folFact = New FolderFactory()

  End Sub
  '-----
  'Rysuje foldery wewnątrz PictureBox
  Private Sub picPaint(ByVal sender As Object, ByVal e As PaintEventArgs)
    Dim i As Integer, x As Integer, y As Integer, cnt As Integer
    Dim g As Graphics = e.Graphics
    x = pLeft
    y = pTop
    cnt = 0
    For i = 0 To names.Count - 1
      fol = folFact.getFolder(selectedName = CType(names(i), String))
      fol.draw(g, x, y, CType(names(i), String))
      cnt = cnt + 1
      If cnt > 2 Then
        cnt = 0
        x = pLeft
        y = y + VSpace
      Else
        x = x + HSpace
      End If
    Next
  End Sub
  '-----
  Public Sub evMouse(ByVal sender As Object, ByVal e As MouseEventArgs)
    Dim x, y, i, cnt As Integer
    Dim oldname As String
    Dim found As Boolean
    oldname = selectedName        'Przechowaj poprzednią nazwę
    x = pLeft
    y = pTop
    i = 0
    cnt = 0
    found = False
    While i < names.Count And Not found
      rect.init(x, y)
      'Sprawdź czy kursor myszy wewnatrz prostokąta
      If rect.contains(e.X, e.Y) Then
        selectedName = CType(names(i), String)
        found = True
      End If
      i = i + 1
      cnt = cnt + 1
      'Sprawdź następny prostokąt
      If cnt > 2 Then
        cnt = 0
        x = pLeft
        y = y + VSpace
      Else
        x = x + HSpace
      End If

    End While
    'Odśwież jeśli kursor myszy w innym prostokącie niż dotychczas
    If found And oldname <> selectedName Then
      Pic.Refresh()
    End If
  End Sub

#Region " Windows Form Designer generated code "

  Public Sub New()
    MyBase.New()

    'This call is required by the Windows Form Designer.
    InitializeComponent()

    'Add any initialization after the InitializeComponent() call
    init()
  End Sub

  'Form overrides dispose to clean up the component list.
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub

  'Required by the Windows Form Designer
  Private components As System.ComponentModel.Container

  'NOTE: The following procedure is required by the Windows Form Designer
  'It can be modified using the Windows Form Designer.  
  'Do not modify it using the code editor.
  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    Me.Pic = New System.Windows.Forms.PictureBox()
    Me.SuspendLayout()
    '
    'Pic
    '
    Me.Pic.Location = New System.Drawing.Point(8, 8)
    Me.Pic.Name = "Pic"
    Me.Pic.Size = New System.Drawing.Size(280, 264)
    Me.Pic.TabIndex = 0
    Me.Pic.TabStop = False
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 273)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Pic})
    Me.Name = "Form1"
    Me.Text = "Flyweight Folder"
    Me.ResumeLayout(False)

  End Sub

#End Region

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

  End Sub
End Class
