Public Class Form1
  Inherits System.Windows.Forms.Form
  Private redC As RedCommand
  Private blueC As BlueCommand
  Friend WithEvents Pic As System.Windows.Forms.PictureBox
  Private undoC As UndoCommand
  '-----
  Private Sub init()
    Dim evh As New EventHandler(AddressOf CommandHandler)
    redC = New RedCommand(Pic)
    blueC = New BlueCommand(Pic)
    undoC = New UndoCommand()
    RedButton.setCommand(redC)
    BlueButton.setCommand(blueC)
    UndoButton.setCommand(undoC)
    AddHandler RedButton.Click, evh
    AddHandler BlueButton.Click, evh
    AddHandler UndoButton.Click, evh
    AddHandler Pic.Paint, New PaintEventHandler(AddressOf PaintHandler)
  End Sub
  '-----
  Public Sub CommandHandler(ByVal sender As Object, ByVal e As EventArgs)
    Dim cmdh As CommandHolder
    Dim cmd As Command
    'pobierz command
    cmdh = CType(sender, CommandHolder)
    cmd = cmdh.getCommand
    undoC.add(cmd)                'dodaj do listy wykonanych operacji
    cmd.Execute()
  End Sub
  '-----
  Public Sub PaintHandler(ByVal sender As Object, ByVal e As PaintEventArgs)
    Dim g As Graphics = e.Graphics
    redC.draw(g)
    blueC.draw(g)
  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
  Friend WithEvents UndoButton As UndoCmd.CmdButton
  Friend WithEvents BlueButton As UndoCmd.CmdButton
  Friend WithEvents RedButton As UndoCmd.CmdButton

  '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.BlueButton = New UndoCmd.CmdButton()
    Me.UndoButton = New UndoCmd.CmdButton()
    Me.Pic = New System.Windows.Forms.PictureBox()
    Me.RedButton = New UndoCmd.CmdButton()
    Me.SuspendLayout()
    '
    'BlueButton
    '
    Me.BlueButton.Location = New System.Drawing.Point(224, 240)
    Me.BlueButton.Name = "BlueButton"
    Me.BlueButton.Size = New System.Drawing.Size(48, 24)
    Me.BlueButton.TabIndex = 2
    Me.BlueButton.Text = "Blue"
    '
    'UndoButton
    '
    Me.UndoButton.Location = New System.Drawing.Point(120, 240)
    Me.UndoButton.Name = "UndoButton"
    Me.UndoButton.Size = New System.Drawing.Size(48, 24)
    Me.UndoButton.TabIndex = 1
    Me.UndoButton.Text = "Undo"
    '
    'Pic
    '
    Me.Pic.BackColor = System.Drawing.SystemColors.ActiveCaptionText
    Me.Pic.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
    Me.Pic.Location = New System.Drawing.Point(8, 24)
    Me.Pic.Name = "Pic"
    Me.Pic.Size = New System.Drawing.Size(272, 176)
    Me.Pic.TabIndex = 3
    Me.Pic.TabStop = False
    '
    'RedButton
    '
    Me.RedButton.Location = New System.Drawing.Point(16, 240)
    Me.RedButton.Name = "RedButton"
    Me.RedButton.Size = New System.Drawing.Size(48, 24)
    Me.RedButton.TabIndex = 0
    Me.RedButton.Text = "Red"
    '
    '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.BlueButton, Me.UndoButton, Me.RedButton})
    Me.Name = "Form1"
    Me.Text = "Undo Command demo"
    Me.ResumeLayout(False)

  End Sub

#End Region

End Class
