'/// Copyright(c) 2016 pakkin. All Rights Reserved.
'/// [改訂履歴]
'/// 2016.05.20 作成
Imports System.Reflection
Public Class Form1
Private RowChangeKeys() As Keys = New Keys() {Keys.Down, Keys.Up, Keys.PageUp, Keys.PageDown, Keys.Home, Keys.End}
Private beforeRow As ListViewItem ' 変更前行
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'ListViewのダブルバッファリングを有効化(ちらつき防止)
Dim RedrawFlags As BindingFlags = BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.SetProperty
Me.ListView1.GetType().InvokeMember("DoubleBuffered", RedrawFlags, Nothing, Me.ListView1, New Object() {True})
'ListViewのプロパティ設定
Me.ListView1.View = View.Details
Me.ListView1.MultiSelect = True
Me.ListView1.Columns.Add("Sample", 300)
'ListViewのダミー行セット
Dim DummyItems As Dictionary(Of Integer, ListViewItem) = Enumerable.Range(0, 100).ToDictionary(Function(n) n, Function(n) New ListViewItem() With {.Text = "Dummy_" & n.ToString("00")})
Me.ListView1.Items.AddRange(DummyItems.Values.ToArray())
Me.UnderlineChange(Me.ListView1.Items(0))
End Sub
'フォーカス行のアンダーライン変更処理
Private Sub UnderlineChange(ByRef afterRow As ListViewItem)
If Me.beforeRow IsNot Nothing Then
If afterRow IsNot Nothing AndAlso afterRow.Index <> Me.beforeRow.Index Then
Me.beforeRow.Font = New Font(Me.beforeRow.Font.Name, Me.beforeRow.Font.Size, FontStyle.Regular)
End If
End If
If afterRow IsNot Nothing Then
If Me.beforeRow Is Nothing OrElse afterRow.Index <> Me.beforeRow.Index Then
afterRow.Font = New Font(afterRow.Font.Name, afterRow.Font.Size, FontStyle.Underline)
End If
Me.beforeRow = afterRow
End If
End Sub
Private Sub ListView1_MouseDown(sender As Object, e As MouseEventArgs) Handles ListView1.MouseDown
'左クリック押下時の行のアンダーライン変更
If e.Button = MouseButtons.Left Then
Dim Posi As Point = Me.ListView1.PointToClient(Cursor.Position)
Me.UnderlineChange(Me.ListView1.GetItemAt(Posi.X, Posi.Y))
End If
End Sub
Private Sub ListView1_KeyUp(sender As Object, e As KeyEventArgs) Handles ListView1.KeyUp
'PAGEDOWN/PAGEUP/UP/DOWNキー押上時、アンダーライン変更
'(押下時だとFocusedItemが旧行を指すため、押上時に処理)
If Me.RowChangeKeys.Contains(e.KeyCode) Then
Me.UnderlineChange(Me.ListView1.FocusedItem)
End If
End Sub
End Class
Copyright(c) 2014-2022 pakkin. All Rights Reserved.