【VB6→VB.NET】FlexGridのHighLightマイグレーション

```Form1.vb

Public Class Form1

    Private Const flexHighLightNever As Integer = 0
    Private Const flexHighlightAlways As Integer = 1

    Private _BackColorSel As Color
    Private _CellBackColor As Color
    Private _HighLight As Integer
    Private _ForeColorSel As Color
    Private _CellForeColor As Color

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load

        With Me.DataGridView1

            For i As Integer = 0 To 100
                .Rows.Add("111", "222", "333", "444", "555")
            Next

        End With

        For iCol = 0 To DataGridView1.Columns.Count - 1
            For iRow = 0 To DataGridView1.Rows.Count - 1
                Dim cellStyle As DataGridViewCellStyle = DataGridView1(iCol, iRow).Style
                Dim rand As New Random
                Dim red As Integer = rand.Next(256)
                Dim green As Integer = rand.Next(256)
                Dim blue As Integer = rand.Next(256)
                cellStyle.BackColor = Color.FromArgb(red, green, blue)
                red = rand.Next(256)
                green = rand.Next(256)
                blue = rand.Next(256)
                cellStyle.ForeColor = Color.FromArgb(red, green, blue)
            Next
        Next

        Dim dcellStyle As DataGridViewCellStyle = DataGridView1.DefaultCellStyle
        Dim drand As New Random
        Dim dred As Integer = drand.Next(256)
        Dim dgreen As Integer = drand.Next(256)
        Dim dblue As Integer = drand.Next(256)
        dcellStyle.BackColor = Color.FromArgb(dred, dgreen, dblue)
        dred = drand.Next(256)
        dgreen = drand.Next(256)
        dblue = drand.Next(256)
        dcellStyle.ForeColor = Color.FromArgb(dred, dgreen, dblue)

        Me.HighLight = flexHighLightNever
        Me.BackColorSel = Color.Blue
        Me.ForeColorSel = Color.White
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If Me.HighLight = flexHighlightAlways Then
            Me.HighLight = flexHighLightNever
            Button1.Text = "HighLightなし"
        Else
            Me.HighLight = flexHighlightAlways
            Button1.Text = "HighLightあり"
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect Then
            Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
            Button2.Text = "行選択モード"
        Else
            Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
            Button2.Text = "単一選択モード"
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        With Me.DataGridView1

            .Rows.Add("111", "222", "333", "444", "555")

        End With

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Dim rand As New Random
        Dim red As Integer = rand.Next(256)
        Dim green As Integer = rand.Next(256)
        Dim blue As Integer = rand.Next(256)
        Me.BackColorSel = Color.FromArgb(red, green, blue)
        red = rand.Next(256)
        green = rand.Next(256)
        blue = rand.Next(256)
        Me.ForeColorSel = Color.FromArgb(red, green, blue)
    End Sub

    ''' <summary>
    ''' BackColorSelプロパティ
    ''' </summary>
    ''' <returns></returns>
    Property BackColorSel As Color
        Get
            Return _BackColorSel
        End Get
        Set
            _BackColorSel = Value

            Me.SetSelectionDefaultColor(Me.HighLight, Value, Me.ForeColorSel)
            For iCol = 0 To DataGridView1.Columns.Count - 1
                For iRow = 0 To DataGridView1.Rows.Count - 1
                    Me.SetSelectionColor(Me.HighLight, Value, Me.ForeColorSel, iCol, iRow)
                Next
            Next

        End Set
    End Property

    ''' <summary>
    ''' ForeColorSelプロパティ
    ''' </summary>
    ''' <returns></returns>
    Property ForeColorSel As Color
        Get
            Return _ForeColorSel
        End Get
        Set
            _ForeColorSel = Value

            Me.SetSelectionDefaultColor(Me.HighLight, Me.BackColorSel, Value)
            For iCol = 0 To DataGridView1.Columns.Count - 1
                For iRow = 0 To DataGridView1.Rows.Count - 1
                    Me.SetSelectionColor(Me.HighLight, Me.BackColorSel, Value, iCol, iRow)
                Next
            Next

        End Set
    End Property

    ''' <summary>
    ''' HighLightプロパティ
    ''' </summary>
    ''' <returns></returns>
    Property HighLight As Integer
        Get
            Return _HighLight
        End Get
        Set
            _HighLight = Value

            Me.SetSelectionDefaultColor(Value, Me.BackColorSel, Me.ForeColorSel)
            For iCol = 0 To DataGridView1.Columns.Count - 1
                For iRow = 0 To DataGridView1.Rows.Count - 1
                    Me.SetSelectionColor(Value, Me.BackColorSel, Me.ForeColorSel, iCol, iRow)
                Next
            Next

        End Set
    End Property

    ''' <summary>
    ''' 選択色の設定を行います。(DefaultCellStyle)
    ''' </summary>
    ''' <param name="iHighLight"></param>
    ''' <param name="cBackColorSel"></param>
    ''' <param name="cForeColorSel"></param>
    Private Sub SetSelectionDefaultColor(ByVal iHighLight As Integer, ByVal cBackColorSel As Color, ByVal cForeColorSel As Color)

        Dim dcsDefaultCellStyle As DataGridViewCellStyle = DataGridView1.DefaultCellStyle
        Dim rdcsRowsDefaultCellStyle As DataGridViewCellStyle = DataGridView1.RowsDefaultCellStyle

        If iHighLight = flexHighLightNever Then
            'HighLightなし

            '文字色、背景色を選択色と同じにする。
            dcsDefaultCellStyle.SelectionBackColor = dcsDefaultCellStyle.BackColor
            dcsDefaultCellStyle.SelectionForeColor = dcsDefaultCellStyle.ForeColor
            DataGridView1.DefaultCellStyle = dcsDefaultCellStyle
            rdcsRowsDefaultCellStyle.SelectionBackColor = rdcsRowsDefaultCellStyle.BackColor
            rdcsRowsDefaultCellStyle.SelectionForeColor = rdcsRowsDefaultCellStyle.ForeColor
            DataGridView1.RowsDefaultCellStyle = rdcsRowsDefaultCellStyle
        Else
            'HighLightあり

            '選択色(文字色、背景色)を設定する。
            dcsDefaultCellStyle.SelectionBackColor = cBackColorSel
            dcsDefaultCellStyle.SelectionForeColor = cForeColorSel
            DataGridView1.DefaultCellStyle = dcsDefaultCellStyle
            rdcsRowsDefaultCellStyle.SelectionBackColor = cBackColorSel
            rdcsRowsDefaultCellStyle.SelectionForeColor = cForeColorSel
            DataGridView1.RowsDefaultCellStyle = rdcsRowsDefaultCellStyle
        End If
    End Sub

    ''' <summary>
    ''' 選択色の設定を行います。
    ''' </summary>
    ''' <param name="iHighLight"></param>
    ''' <param name="cBackColorSel"></param>
    ''' <param name="cForeColorSel"></param>
    ''' <param name="iCol"></param>
    ''' <param name="iRow"></param>
    Private Sub SetSelectionColor(ByVal iHighLight As Integer, ByVal cBackColorSel As Color, ByVal cForeColorSel As Color, ByVal iCol As Integer, ByVal iRow As Integer)

        Dim cellStyle As DataGridViewCellStyle = DataGridView1(iCol, iRow).Style

        If iHighLight = flexHighLightNever Then
            'HighLightなし

            '文字色、背景色を選択色と同じにする。
            cellStyle.SelectionBackColor = cellStyle.BackColor
            cellStyle.SelectionForeColor = cellStyle.ForeColor
        Else
            'HighLightあり

            '選択色(文字色、背景色)を設定する。
            cellStyle.SelectionBackColor = cBackColorSel
            cellStyle.SelectionForeColor = cForeColorSel
        End If
    End Sub

    Private Sub DataGridView1_Click(sender As Object, e As EventArgs) Handles DataGridView1.Click
        For i As Integer = 0 To DataGridView1.SelectedCells.Count - 1
            If DataGridView1(DataGridView1.SelectedCells(i).ColumnIndex, DataGridView1.SelectedCells(i).RowIndex).Style.BackColor <> Color.Blue Then
                DataGridView1(DataGridView1.SelectedCells(i).ColumnIndex, DataGridView1.SelectedCells(i).RowIndex).Style.BackColor = Color.Blue
                DataGridView1(DataGridView1.SelectedCells(i).ColumnIndex, DataGridView1.SelectedCells(i).RowIndex).Style.ForeColor = Color.White
                Me.SetSelectionColor(Me.HighLight, Me.BackColorSel, Me.ForeColorSel, DataGridView1.SelectedCells(i).ColumnIndex, DataGridView1.SelectedCells(i).RowIndex)
            Else
                DataGridView1(DataGridView1.SelectedCells(i).ColumnIndex, DataGridView1.SelectedCells(i).RowIndex).Style.BackColor = Color.SkyBlue
                DataGridView1(DataGridView1.SelectedCells(i).ColumnIndex, DataGridView1.SelectedCells(i).RowIndex).Style.ForeColor = Color.White
                Me.SetSelectionColor(Me.HighLight, Me.BackColorSel, Me.ForeColorSel, DataGridView1.SelectedCells(i).ColumnIndex, DataGridView1.SelectedCells(i).RowIndex)
            End If
        Next
    End Sub
End Class
```

```Form1.Designer.vb
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'フォームがコンポーネントの一覧をクリーンアップするために dispose をオーバーライドします。
    <System.Diagnostics.DebuggerNonUserCode()>
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

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

    'メモ: 以下のプロシージャは Windows フォーム デザイナーで必要です。
    'Windows フォーム デザイナーを使用して変更できます。  
    'コード エディターを使って変更しないでください。
    <System.Diagnostics.DebuggerStepThrough()>
    Private Sub InitializeComponent()
        Me.DataGridView1 = New System.Windows.Forms.DataGridView()
        Me.Column1 = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.Column2 = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.Column3 = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.Column4 = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.Column5 = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.Button4 = New System.Windows.Forms.Button()
        CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'DataGridView1
        '
        Me.DataGridView1.AllowUserToDeleteRows = False
        Me.DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
        Me.DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.Column1, Me.Column2, Me.Column3, Me.Column4, Me.Column5})
        Me.DataGridView1.Location = New System.Drawing.Point(12, 12)
        Me.DataGridView1.MultiSelect = False
        Me.DataGridView1.Name = "DataGridView1"
        Me.DataGridView1.ReadOnly = True
        Me.DataGridView1.RowHeadersWidthSizeMode = System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode.DisableResizing
        Me.DataGridView1.RowTemplate.Height = 21
        Me.DataGridView1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect
        Me.DataGridView1.Size = New System.Drawing.Size(546, 200)
        Me.DataGridView1.TabIndex = 0
        '
        'Column1
        '
        Me.Column1.Frozen = True
        Me.Column1.HeaderText = "Column1"
        Me.Column1.Name = "Column1"
        Me.Column1.ReadOnly = True
        Me.Column1.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable
        '
        'Column2
        '
        Me.Column2.Frozen = True
        Me.Column2.HeaderText = "Column2"
        Me.Column2.Name = "Column2"
        Me.Column2.ReadOnly = True
        Me.Column2.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable
        '
        'Column3
        '
        Me.Column3.Frozen = True
        Me.Column3.HeaderText = "Column3"
        Me.Column3.Name = "Column3"
        Me.Column3.ReadOnly = True
        Me.Column3.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable
        '
        'Column4
        '
        Me.Column4.Frozen = True
        Me.Column4.HeaderText = "Column4"
        Me.Column4.Name = "Column4"
        Me.Column4.ReadOnly = True
        Me.Column4.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable
        '
        'Column5
        '
        Me.Column5.Frozen = True
        Me.Column5.HeaderText = "Column5"
        Me.Column5.Name = "Column5"
        Me.Column5.ReadOnly = True
        Me.Column5.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(12, 218)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(134, 40)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "HighLightなし"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(152, 218)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(129, 40)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "行選択モード"
        Me.Button2.UseVisualStyleBackColor = True
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(287, 218)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(129, 40)
        Me.Button3.TabIndex = 3
        Me.Button3.Text = "行追加"
        Me.Button3.UseVisualStyleBackColor = True
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(422, 218)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(136, 40)
        Me.Button4.TabIndex = 4
        Me.Button4.Text = "色を変える"
        Me.Button4.UseVisualStyleBackColor = True
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 12.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(800, 450)
        Me.Controls.Add(Me.Button4)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.DataGridView1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGridView1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

    Friend WithEvents DataGridView1 As DataGridView
    Friend WithEvents Column1 As DataGridViewTextBoxColumn
    Friend WithEvents Column2 As DataGridViewTextBoxColumn
    Friend WithEvents Column3 As DataGridViewTextBoxColumn
    Friend WithEvents Column4 As DataGridViewTextBoxColumn
    Friend WithEvents Column5 As DataGridViewTextBoxColumn
    Friend WithEvents Button1 As Button
    Friend WithEvents Button2 As Button
    Friend WithEvents Button3 As Button
    Friend WithEvents Button4 As Button
End Class
```