.NET 1.1 の時に何故かよくお世話になったコントロールで、
2.0 になってからもごくごく単純な使い方しかしてなかったわけですが、
う~む...よくよく見てみると機能がてんこもりですね~
# 3 時間近くも遊んでたらまたソースが汚く長ったらしくなってしまった...
さて、中さんのとこ で紹介されてた VirtualMode なのですが、
CheckBoxes = True にしても、チェックボックスが出てこないのですよ。
これはこういう仕様なんでしょうか...それとも何か間違ってるのかなぁ...?
■参考文献
ListView コントロール (Windows フォーム)
ListView クラス
ListViewItem クラス
■実行画像
こてこて色々試して遊んでみた ListView
VirtualMode で描画した ListView
Public Class ListViewTest
Private m_tabControl As TabControl
Private m_virtualListDataSrouce As DataTable
Private Enum SateImageKeys
blackwankumaIcon
wankumaIcon
End Enum
Private Enum ImageKeys
fujiko
nakasan
End Enum
Private Enum ListViewName As Integer
Normal
Virtual
End Enum
Private Sub ListViewTest_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.m_tabControl = New TabControl()
Me.m_tabControl.Dock = DockStyle.Fill
Me.m_tabControl.TabPages.Clear()
Me.Controls.Add(Me.m_tabControl)
Dim imagelst As ImageList = New ImageList()
imagelst.Images.Add(ImageKeys.fujiko.ToString(), My.Resources.ふじこちゃん)
imagelst.Images.Add(ImageKeys.nakasan.ToString(), My.Resources.中さん)
imagelst.ImageSize = New Size(50, 50)
'' CheckBoxes = True の時に、チェックボックスの代わりに表示する画像
'Dim stateImageList As ImageList = New ImageList()
'stateImageList.Images.Add(SateImageKeys.blackwankumaIcon.ToString(), My.Resources.blackwankumaIcon)
'stateImageList.Images.Add(SateImageKeys.wankumaIcon.ToString(), My.Resources.wankumaIcon)
Dim normalPage As TabPage = New TabPage
normalPage.Text = ListViewName.Normal.ToString()
Me.m_tabControl.TabPages.Add(normalPage)
Dim normalLstview As ListView = New ListView()
With normalLstview
.Name = ListViewName.Normal.ToString()
.SmallImageList = imagelst
.LargeImageList = imagelst
.Location = New Point(10, 30)
.Size = New Size(250, 200)
' フォーカスがなくても選択されているものを強調表示しないか否か(既定:True)
.HideSelection = False
'' 編集関連
.LabelEdit = True ' ユーザーによる編集を許可するか否か
AddHandler .BeforeLabelEdit, AddressOf ListView_BeforeLabelEdit
AddHandler .AfterLabelEdit, AddressOf ListView_AfterLabelEdit
' '' マウスポインタ関連
'' マウスポインタが置かれた時に項目またはサブ項目のテキストの外観が
'' ハイパーリンクになるかどうか(既定:False)
'.HotTracking = True
'' マウスポインタが置かれた時に自動で選択するか否か(既定:False)
'.HoverSelection = True
' '' チェックボックス関連
'' チェックボックスの表示
'.CheckBoxes = True
'' チェックボックスが オフの時、blackwankumaIcon
'' チェックボックスが オンの時、wankumaIcon が表示されるようになる
'.StateImageList = stateImageList
' View Detail の為の設定
.Columns.Add("名前", 100, HorizontalAlignment.Center)
.Columns.Add("namae", 100, HorizontalAlignment.Center)
.GridLines = True
.FullRowSelect = True
.AllowColumnReorder = True ' ユーザーによる列の並びかえを有効にする
' View Tile の為の設定
.TileSize = New Size(80, 80)
.BeginUpdate()
' Group の追加
.Groups.Clear()
Dim dogGroup As ListViewGroup = New ListViewGroup("犬")
Dim humanGroup As ListViewGroup = New ListViewGroup("人間")
.Groups.AddRange(New ListViewGroup() {dogGroup, humanGroup})
' Item の追加
With .Items
.Clear()
.Add(New ListViewItem(New String() {"ふじこ", "fujiko"}, ImageKeys.fujiko, dogGroup))
.Add(New ListViewItem(New String() {"えでん", "eden"}, ImageKeys.fujiko, dogGroup))
.Add(New ListViewItem(New String() {"オット", "otto"}, ImageKeys.fujiko, humanGroup))
.Add(New ListViewItem(New String() {"中さん", "nakasan"}, ImageKeys.nakasan, humanGroup))
End With
.EndUpdate()
End With
normalPage.Controls.Add(normalLstview)
Dim viewCombo As ComboBox = New ComboBox()
With viewCombo
.DropDownStyle = ComboBoxStyle.DropDownList
.Location = New Point(10, 10)
With .Items
.Add(View.Details)
.Add(View.LargeIcon)
.Add(View.List)
.Add(View.SmallIcon)
.Add(View.Tile)
End With
.SelectedItem = normalLstview.View
AddHandler .SelectedIndexChanged, AddressOf ChangeView
End With
normalPage.Controls.Add(viewCombo)
Dim enableGroupCheck As CheckBox = New CheckBox()
With enableGroupCheck
.Text = "グループ有効"
.Checked = True
.Location = New Point(150, 10)
AddHandler .CheckedChanged, AddressOf ChangeEnableGroup
End With
normalPage.Controls.Add(enableGroupCheck)
' コンテキストメニュー
Dim context As ContextMenuStrip = New ContextMenuStrip()
normalPage.ContextMenuStrip = context
Dim serchText As ToolStripTextBox = New ToolStripTextBox()
serchText.TextBox.ImeMode = Windows.Forms.ImeMode.Hiragana
context.Items.Add(serchText)
Dim searchButton As ToolStripButton = New ToolStripButton("検索")
context.Items.Add(searchButton)
AddHandler searchButton.Click, AddressOf Search
' VirtualMode で描画する
Dim virtualListPage As TabPage = New TabPage
virtualListPage.Text = ListViewName.Virtual.ToString()
Me.m_tabControl.TabPages.Add(virtualListPage)
' 表示するデータを作成
Me.m_virtualListDataSrouce = New DataTable("virtualTest")
With Me.m_virtualListDataSrouce
With .Columns
.Add("Field1", GetType(Integer))
.Add("Field2", GetType(String))
End With
End With
For index As Integer = 1 To 20000
Me.m_virtualListDataSrouce.Rows.Add(New Object() {index, "test" & index.ToString()})
Next
Dim virtualLstview As ListView = New ListView()
With virtualLstview
With .Columns
.Clear()
.Add("項目1", 100, HorizontalAlignment.Left)
.Add("項目2", 100, HorizontalAlignment.Left)
End With
.Dock = DockStyle.Fill
.View = View.Details
.GridLines = True
.MultiSelect = True
'.CheckBoxes = True ' 利かない?
.FullRowSelect = True
' VirtualMode に関する設定
.VirtualMode = True
.VirtualListSize = Me.m_virtualListDataSrouce.Rows.Count
AddHandler .RetrieveVirtualItem, AddressOf ListView_RetrieveVirtualItem
End With
virtualListPage.Controls.Add(virtualLstview)
' コンテキストメニュー
Dim contextForVirtual As ContextMenuStrip = New ContextMenuStrip()
virtualListPage.ContextMenuStrip = contextForVirtual
Dim selectedInfo As ToolStripMenuItem = New ToolStripMenuItem()
selectedInfo.Text = "選択されている情報"
contextForVirtual.Items.Add(selectedInfo)
AddHandler selectedInfo.Click, AddressOf SelectedOut
End Sub
Private Sub ChangeView(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lview As ListView = _
DirectCast(Me.m_tabControl.TabPages(ListViewName.Normal).Controls(ListViewName.Normal.ToString()), ListView)
lview.View = DirectCast(DirectCast(sender, ComboBox).SelectedItem, View)
If lview.View = View.Details OrElse _
lview.View = View.List Then
lview.TopItem.Selected = True ' 先頭を選択
End If
End Sub
Private Sub ChangeEnableGroup(ByVal sender As Object, ByVal e As System.EventArgs)
Dim chkBox As CheckBox = DirectCast(sender, CheckBox)
Dim lview As ListView = _
DirectCast(Me.m_tabControl.TabPages(ListViewName.Normal).Controls(ListViewName.Normal.ToString()), ListView)
lview.ShowGroups = chkBox.Checked
End Sub
' 検索
Private Sub Search(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As ToolStripButton = DirectCast(sender, ToolStripButton)
Dim context As ContextMenuStrip = DirectCast(btn.Owner, ContextMenuStrip)
Dim tBox As ToolStripTextBox = DirectCast(context.Items(0), ToolStripTextBox)
If tBox.Text = String.Empty Then Return
Dim lview As ListView = _
DirectCast(Me.m_tabControl.TabPages(ListViewName.Normal).Controls(ListViewName.Normal.ToString()), ListView)
' ListView 内を検索する
Dim foundItem As ListViewItem = _
lview.FindItemWithText(tBox.Text, _
True, _
0)
tBox.Text = String.Empty
If foundItem Is Nothing Then Return
' 見つかった Item を選択する
foundItem.Selected = True
' 必要であればスクロールして見つかった Item を表示する
lview.EnsureVisible(foundItem.Index)
End Sub
' 編集モードになったとき
Private Sub ListView_BeforeLabelEdit(ByVal sender As Object, ByVal e As LabelEditEventArgs)
Dim lview As ListView = _
DirectCast(Me.m_tabControl.TabPages(ListViewName.Normal).Controls(ListViewName.Normal.ToString()), ListView)
Dim editItem As ListViewItem = lview.Items(e.Item)
Console.WriteLine("◆BeforeLabelEdit◆")
Console.WriteLine("Item is " & editItem.Text)
Console.WriteLine("NewValue is " & e.Label)
If editItem.Text = "中さん" OrElse editItem.Text = "nakasan" Then
'' 中さんの場合編集をキャンセルする
e.CancelEdit = True
End If
End Sub
' 編集モードをぬけたとき
Private Sub ListView_AfterLabelEdit(ByVal sender As Object, ByVal e As LabelEditEventArgs)
Dim lview As ListView = _
DirectCast(Me.m_tabControl.TabPages(ListViewName.Normal).Controls(ListViewName.Normal.ToString()), ListView)
Dim editItem As ListViewItem = lview.Items(e.Item)
Console.WriteLine("◇AfterLabelEdit◇")
Console.WriteLine("Item is " & editItem.Text)
Console.WriteLine("NewValue is " & e.Label)
If e.Label = "baka" Then
' 編集した結果が baka の場合、編集をキャンセルする
e.CancelEdit = True
End If
End Sub
' 仮想モードで、ListViewItem を必要とする場合に発生
Private Sub ListView_RetrieveVirtualItem(ByVal sender As Object, ByVal e As RetrieveVirtualItemEventArgs)
Dim row As DataRow = Me.m_virtualListDataSrouce.Rows(e.ItemIndex)
e.Item = New ListViewItem(New String() {Convert.ToString(row(0)), Convert.ToString(row(1))})
End Sub
' 選択されている情報 を 出力ウィンドウに出力
Private Sub SelectedOut(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lview As ListView = _
DirectCast(Me.m_tabControl.TabPages(ListViewName.Virtual).Controls(ListViewName.Virtual.ToString()), ListView)
Dim selectedIndices As ListView.SelectedIndexCollection = _
lview.SelectedIndices
Dim selectedItems As ListView.SelectedListViewItemCollection = _
lview.SelectedItems()
'' チェックボックスに関する情報
'Dim checkedIndices As ListView.CheckedIndexCollection = _
'lview.CheckedIndices()
'Dim checkedItems As ListView.CheckedListViewItemCollection = _
'lview.CheckedItems()
Console.WriteLine("全項目数:" & lview.Items.Count())
Console.WriteLine("選択項目数:" & selectedIndices.Count())
'Console.WriteLine("チェックされてる項目数:" & checkedIndices.Count())
Console.WriteLine("SelectedIndices:")
For Each index As Integer In selectedIndices
Console.WriteLine(index.ToString())
Next
' Virtual Mode の時は SelectedItems コレクションの要素にアクセスできない
Console.WriteLine("SelectedItems:")
'For Each item As ListViewItem In selectedItems
' Dim allText As String = item.Text
' For Each subItem As ListViewItem.ListViewSubItem In item.SubItems
' allText &= " " & subItem.Text
' Next
' Console.WriteLine(allText)
'Next
' なのでこういう感じで取り出す
For Each index As Integer In selectedIndices
Dim item As ListViewItem = lview.Items(index)
Dim allText As String = item.Text
For Each subItem As ListViewItem.ListViewSubItem In item.SubItems
allText &= " " & subItem.Text
Next
Console.WriteLine(allText)
Next
'Console.WriteLine("CheckedIndices:")
'For Each index As Integer In checkedIndices
' Console.WriteLine(index.ToString())
'Next
'Console.WriteLine("CheckedItems:")
'For Each item As ListViewItem In checkedItems
' Dim allText As String = item.Text
' For Each subItem As ListViewItem.ListViewSubItem In item.SubItems
' allText &= " " & subItem.Text
' Next
' Console.WriteLine(allText)
'Next
End Sub
End Class