Imports System.Windows.Forms

Public Class Form1
  Inherits System.Windows.Forms.Form
  Private mnMain As MainMenu
  Private mnFile As MenuItem
  Private mnOpen, mnQuit As CmdMenu
  Friend WithEvents rdButn As CmdHolder.comdButton
  Private evh As EventHandler

  Private Sub init()
    'Wywoływana z konstruktora
    evh = New EventHandler(AddressOf CommandHandler)
    AddHandler rdButn.Click, evh
    rdButn.setCommand(New RedCommand(Me))
    mnMain = New MainMenu()
    mnFile = New MenuItem("File")
    mnOpen = New CmdMenu("Open", New FileOpenCommand(), evh)
    mnQuit = New CmdMenu("Exit", New ExitCommand(Me), evh)
    mnMain.MenuItems.AddRange(New MenuItem() {mnFile})
    mnFile.MenuItems.AddRange(New MenuItem() {mnOpen, mnQuit})
    Me.Menu = mnMain
    AddHandler mnOpen.Click, evh
    AddHandler mnQuit.Click, evh
  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.rdButn = New CmdHolder.comdButton()
    Me.SuspendLayout()
    '
    'rdButn
    '
    Me.rdButn.Location = New System.Drawing.Point(128, 96)
    Me.rdButn.Name = "rdButn"
    Me.rdButn.Size = New System.Drawing.Size(64, 32)
    Me.rdButn.TabIndex = 0
    Me.rdButn.Text = "Red"
    '
    'Form1
    '
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(328, 253)
    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.rdButn})
    Me.Name = "Form1"
    Me.Text = "Command holder form"
    Me.ResumeLayout(False)

  End Sub

#End Region

  Public Sub CommandHandler(ByVal sender As Object, _
             ByVal e As EventArgs)
    Dim cmdh As CommandHolder
    Dim cmd As Command
    cmdh = CType(sender, CommandHolder)
    cmd = cmdh.getCommand
    cmd.Execute()
  End Sub
End Class
