'//----------------------------------------------------------------------
'//【OnError v.s. Try Catchの比較】- OnErrorDemo
'//
'//
'//                                                       by Masato Hirai
'//----------------------------------------------------------------------
Imports System
Imports Microsoft.VisualBasic
Imports System.Windows.Forms
Imports GeniusHirai.ColorfulControls

Public Class Form1 : Inherits Form

#Region " Windows フォーム デザイナで生成されたコード "

    Public Sub New()
        MyBase.New()

        ' この呼び出しは Windows フォーム デザイナで必要です。
        InitializeComponent()

        ' InitializeComponent() 呼び出しの後に初期化を追加します。

    End Sub

    ' Form は、コンポーネント一覧に後処理を実行するために dispose をオーバーライドします。
    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

    ' Windows フォーム デザイナで必要です。
    Private components As System.ComponentModel.IContainer

    ' メモ : 以下のプロシージャは、Windows フォーム デザイナで必要です。
    'Windows フォーム デザイナを使って変更してください。  
    ' コード エディタを使って変更しないでください。
  Friend WithEvents Button1 As GeniusHirai.ColorfulControls.OvalButton
  Friend WithEvents Button2 As GeniusHirai.ColorfulControls.OvalButton
  Friend WithEvents NumericLabel1 As GeniusHirai.ColorfulControls.NumericLabel
  Friend WithEvents NumericLabel2 As GeniusHirai.ColorfulControls.NumericLabel
  Friend WithEvents RichLabel1 As GeniusHirai.ColorfulControls.RichLabel
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New GeniusHirai.ColorfulControls.OvalButton
Me.Button2 = New GeniusHirai.ColorfulControls.OvalButton
Me.NumericLabel1 = New GeniusHirai.ColorfulControls.NumericLabel
Me.NumericLabel2 = New GeniusHirai.ColorfulControls.NumericLabel
Me.RichLabel1 = New GeniusHirai.ColorfulControls.RichLabel
Me.SuspendLayout()
'
'Button1
'
Me.Button1.FaceColor = GeniusHirai.ColorfulControls.OvalButton.FaceColorConstant.Gray
Me.Button1.Font = New System.Drawing.Font("Arial", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Button1.ForeColor = System.Drawing.Color.White
Me.Button1.Location = New System.Drawing.Point(16, 88)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(224, 104)
Me.Button1.TabIndex = 2
Me.Button1.Text = "On Error"
'
'Button2
'
Me.Button2.FaceColor = GeniusHirai.ColorfulControls.OvalButton.FaceColorConstant.Gray
Me.Button2.Font = New System.Drawing.Font("Arial", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Button2.ForeColor = System.Drawing.Color.White
Me.Button2.Location = New System.Drawing.Point(16, 200)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(224, 104)
Me.Button2.TabIndex = 3
Me.Button2.Text = "Try Catch"
'
'NumericLabel1
'
Me.NumericLabel1.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.NumericLabel1.BackColor = System.Drawing.Color.Black
Me.NumericLabel1.ForeColor = System.Drawing.Color.MediumSpringGreen
Me.NumericLabel1.Location = New System.Drawing.Point(264, 88)
Me.NumericLabel1.Name = "NumericLabel1"
Me.NumericLabel1.Size = New System.Drawing.Size(220, 98)
Me.NumericLabel1.TabIndex = 4
Me.NumericLabel1.Text = "NumericLabel1"
Me.NumericLabel1.UpperPoint = 4
Me.NumericLabel1.ZoomMode = GeniusHirai.ColorfulControls.NumericLabel.ZoomModes.Small
'
'NumericLabel2
'
Me.NumericLabel2.Anchor = CType((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.NumericLabel2.BackColor = System.Drawing.Color.Black
Me.NumericLabel2.ForeColor = System.Drawing.Color.MediumSpringGreen
Me.NumericLabel2.Location = New System.Drawing.Point(264, 200)
Me.NumericLabel2.Name = "NumericLabel2"
Me.NumericLabel2.Size = New System.Drawing.Size(220, 98)
Me.NumericLabel2.TabIndex = 5
Me.NumericLabel2.Text = "NumericLabel2"
Me.NumericLabel2.UpperPoint = 4
Me.NumericLabel2.ZoomMode = GeniusHirai.ColorfulControls.NumericLabel.ZoomModes.Small
'
'RichLabel1
'
Me.RichLabel1.BackColor = System.Drawing.Color.MediumSlateBlue
Me.RichLabel1.Dock = System.Windows.Forms.DockStyle.Top
Me.RichLabel1.Font = New System.Drawing.Font("Arial", 27.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.RichLabel1.ForeColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(128, Byte))
Me.RichLabel1.Location = New System.Drawing.Point(0, 0)
Me.RichLabel1.Name = "RichLabel1"
Me.RichLabel1.Size = New System.Drawing.Size(496, 64)
Me.RichLabel1.TabIndex = 6
Me.RichLabel1.Text = "On Error v.s. Try Catch"
Me.RichLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 12)
Me.BackColor = System.Drawing.SystemColors.ControlDarkDark
Me.ClientSize = New System.Drawing.Size(496, 318)
Me.Controls.Add(Me.RichLabel1)
Me.Controls.Add(Me.NumericLabel2)
Me.Controls.Add(Me.NumericLabel1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "On Error vs Try Catch"
Me.ResumeLayout(False)

    End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  Button1.FaceColor = OvalButton.FaceColorConstant.Blue
  Me.Refresh()
  Dim t As Integer = Environment.TickCount

  Foo(20000)

  NumericLabel1.Value = Environment.TickCount - t
  Button1.FaceColor = OvalButton.FaceColorConstant.Gray
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  Button2.FaceColor = OvalButton.FaceColorConstant.Blue
  Me.Refresh()
  Dim t As Integer = Environment.TickCount

  Acme(20000)

  NumericLabel2.Value = Environment.TickCount - t
  Button2.FaceColor = OvalButton.FaceColorConstant.Gray
End Sub

Private Sub Foo(ByVal loops As Integer)
  Dim a, b As Integer
  For k As Integer = 1 To loops
    '//
    '// VBの On Error ステートメント
    '//
    On Error Resume Next
    b = 100 / a
    If Err.Number <> 0 Then
      Console.Write(Err.Description)
    End If
    On Error GoTo 0

  Next
End Sub

Private Sub Acme(ByVal loops As Integer)
  Dim a, b As Integer
  For k As Integer = 1 To loops
    '//
    '// 構造化例外処理
    '//
    Try
      b = 100 / a
    Catch ex As Exception
      Console.Write(ex.Message())
    End Try

  Next
End Sub


End Class
