'//----------------------------------------------------------------------
'//【アイテムの追加 - BeginUpdate】- BeginUpdateDemo
'//
'//
'//                                                       by Masato Hirai
'//----------------------------------------------------------------------
Imports System
Imports System.Collections
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Reflection
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 NumericLabel1 As GeniusHirai.ColorfulControls.NumericLabel
  Friend WithEvents CheckBox1 As GeniusHirai.ColorfulControls.BigCheckBox
  Friend WithEvents NumericUpDown1 As System.Windows.Forms.NumericUpDown
  Friend WithEvents Button1 As GeniusHirai.ColorfulControls.OvalButton
  Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
  Friend WithEvents Label1 As GeniusHirai.ColorfulControls.RichLabel
  Friend WithEvents Label2 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.Button1 = New GeniusHirai.ColorfulControls.OvalButton
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.Label1 = New GeniusHirai.ColorfulControls.RichLabel
Me.NumericLabel1 = New GeniusHirai.ColorfulControls.NumericLabel
Me.CheckBox1 = New GeniusHirai.ColorfulControls.BigCheckBox
Me.NumericUpDown1 = New System.Windows.Forms.NumericUpDown
Me.Label2 = New System.Windows.Forms.Label
CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.Button1.FaceColor = GeniusHirai.ColorfulControls.OvalButton.FaceColorConstant.Red
Me.Button1.Location = New System.Drawing.Point(240, 520)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(80, 64)
Me.Button1.TabIndex = 11
Me.Button1.Text = "Add"
'
'ListBox1
'
Me.ListBox1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
            Or System.Windows.Forms.AnchorStyles.Left) _
            Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.ListBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
Me.ListBox1.Font = New System.Drawing.Font("MS UI Gothic", 27.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.ListBox1.ItemHeight = 42
Me.ListBox1.Location = New System.Drawing.Point(16, 168)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.Size = New System.Drawing.Size(304, 340)
Me.ListBox1.TabIndex = 0
'
'Label1
'
Me.Label1.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(192, Byte), CType(255, Byte))
Me.Label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.Label1.Dock = System.Windows.Forms.DockStyle.Top
Me.Label1.Font = New System.Drawing.Font("MS UI Gothic", 27.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.Label1.ForeColor = System.Drawing.Color.Blue
Me.Label1.Location = New System.Drawing.Point(0, 0)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(336, 66)
Me.Label1.TabIndex = 7
Me.Label1.Text = "BeginUpdate"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'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.Yellow
Me.NumericLabel1.Location = New System.Drawing.Point(152, 80)
Me.NumericLabel1.Name = "NumericLabel1"
Me.NumericLabel1.Size = New System.Drawing.Size(172, 63)
Me.NumericLabel1.TabIndex = 8
Me.NumericLabel1.Text = "NumericLabel1"
Me.NumericLabel1.UpperPoint = 5
Me.NumericLabel1.ZoomMode = GeniusHirai.ColorfulControls.NumericLabel.ZoomModes.Mini
'
'CheckBox1
'
Me.CheckBox1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.CheckBox1.BackColor = System.Drawing.Color.Transparent
Me.CheckBox1.Font = New System.Drawing.Font("MS UI Gothic", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.CheckBox1.Location = New System.Drawing.Point(16, 536)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.Size = New System.Drawing.Size(216, 32)
Me.CheckBox1.TabIndex = 8
Me.CheckBox1.Text = "BeginUpdate"
'
'NumericUpDown1
'
Me.NumericUpDown1.Font = New System.Drawing.Font("MS UI Gothic", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.NumericUpDown1.Increment = New Decimal(New Integer() {1000, 0, 0, 0})
Me.NumericUpDown1.Location = New System.Drawing.Point(16, 112)
Me.NumericUpDown1.Maximum = New Decimal(New Integer() {100000, 0, 0, 0})
Me.NumericUpDown1.Name = "NumericUpDown1"
Me.NumericUpDown1.TabIndex = 7
Me.NumericUpDown1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
Me.NumericUpDown1.Value = New Decimal(New Integer() {100, 0, 0, 0})
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.BackColor = System.Drawing.Color.Transparent
Me.Label2.Font = New System.Drawing.Font("MS UI Gothic", 18.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.Label2.Location = New System.Drawing.Point(8, 80)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(112, 27)
Me.Label2.TabIndex = 5
Me.Label2.Text = "試行回数:"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(15, 32)
Me.BackgroundImage = CType(resources.GetObject("$this.BackgroundImage"), System.Drawing.Image)
Me.ClientSize = New System.Drawing.Size(336, 598)
Me.Controls.Add(Me.NumericLabel1)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.CheckBox1)
Me.Controls.Add(Me.NumericUpDown1)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Button1)
Me.Font = New System.Drawing.Font("MS UI Gothic", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(128, Byte))
Me.Name = "Form1"
Me.Text = "アイテムの追加"
CType(Me.NumericUpDown1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

    End Sub

#End Region



Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
  Label1.BackColor _
    = Color.FromName(ListBox1.SelectedItem)
End Sub

'//
'// ListBoxのオーナードロー
'//
Private Sub ListBox_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
  If ListBox1.Items.Count = 0 Then Exit Sub
  Dim brColor As Brush
  Try
    Dim brText As Brush
    If (e.State And DrawItemState.Selected) = _
      DrawItemState.Selected Then
      brText = Brushes.White
    Else
      brText = Brushes.Black
    End If
    e.DrawBackground()
    Dim rct As New Rectangle( _
      e.Bounds.X + 5, e.Bounds.Y + 5, _
      e.Bounds.Height - 10, e.Bounds.Height - 10)
    If ListBox1.DataSource Is Nothing Then
      brColor = New SolidBrush(Color.FromName(ListBox1.Items(e.Index)))
    Else
      brColor = New SolidBrush(ListBox1.Items(e.Index))
    End If
    e.Graphics.DrawRectangle(Pens.Black, rct)
    e.Graphics.FillRectangle(brColor, rct)
    If Not ListBox1.Items(e.Index).GetType.Equals(GetType(Color)) Then
      e.Graphics.DrawString( _
        ListBox1.Items(e.Index), _
        ListBox1.Font, brText, _
        rct.Left + rct.Width + 5, e.Bounds.Y)
    Else
      e.Graphics.DrawString( _
        CType(ListBox1.Items(e.Index), Color).Name, _
        ListBox1.Font, brText, _
        rct.Left + rct.Width + 5, e.Bounds.Y)
    End If
    e.DrawFocusRectangle()
  Finally
    If Not brColor Is Nothing Then
      brColor.Dispose()
    End If
  End Try
End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim LOOPS As Integer = NumericUpDown1.Value
    ListBox1.Items.Clear()
    Dim t As Integer = Environment.TickCount

    If CheckBox1.Checked Then ListBox1.BeginUpdate()

    For n As Integer = 1 To LOOPS
      For Each p As System.Reflection.PropertyInfo _
            In GetType(Color).GetProperties()
        If p.PropertyType.Equals(GetType(Color)) Then
          ListBox1.Items.Add(p.Name)
        End If
      Next
    Next

    If CheckBox1.Checked Then ListBox1.EndUpdate()

    NumericLabel1.Value = Environment.TickCount - t
  End Sub

End Class
