Чекбоксы в ListView - VB
Формулировка задачи:
Как добавить чекбоксы в ListView, например как в торренте?
Тоесть папка рядом + и чекбокс, нажимаешь + открывается древо и у каждого элемента есть чекбокс.
Дальнейшую логику как делать сам пойму, мне хотяб 1 пример с добавлением в родителя и дочерние элементы)
Решение задачи: «Чекбоксы в ListView»
textual
Листинг программы
Option Explicit Private Enum ChkState CS_UNCHECKED = 1 CS_CHECKED = 2 CS_GRAYED = 3 End Enum Private Sub Form_Load() tvwTree.Nodes.Add , , "main1", "Main 1", CS_UNCHECKED tvwTree.Nodes.Add , , "main2", "Main 2", CS_UNCHECKED tvwTree.Nodes.Add , , "main3", "Main 3", CS_UNCHECKED tvwTree.Nodes.Add "main1", tvwChild, "main1ch1", "Child1", CS_UNCHECKED tvwTree.Nodes.Add "main1", tvwChild, "main1ch2", "Child2", CS_UNCHECKED tvwTree.Nodes.Add "main1", tvwChild, "main1ch3", "Child3", CS_UNCHECKED tvwTree.Nodes.Add "main1ch1", tvwChild, "main1ch1ch1", "Child1", CS_UNCHECKED tvwTree.Nodes.Add "main1ch1", tvwChild, "main1ch2ch1", "Child2", CS_UNCHECKED tvwTree.Nodes.Add "main1ch1ch1", tvwChild, "main1ch1ch1ch1", "Child1", CS_UNCHECKED tvwTree.Nodes.Add "main1ch1ch1", tvwChild, "main1ch2ch1ch1", "Child2", CS_UNCHECKED tvwTree.Nodes.Add "main1ch2ch1ch1", tvwChild, "main1ch1ch1ch1ch1", "Child1", CS_UNCHECKED tvwTree.Nodes.Add "main1ch2ch1ch1", tvwChild, "main1ch2ch1ch1ch1", "Child2", CS_UNCHECKED tvwTree.Nodes.Add "main2", tvwChild, "main2ch2", "Child2", CS_UNCHECKED tvwTree.Nodes.Add "main2", tvwChild, "main2ch3", "Child3", CS_UNCHECKED End Sub Private Sub tvwTree_MouseDown(Button As Integer, Shift As Integer, X As Single, y As Single) Dim Sel As Node, Pt As Node Dim Lvl As Long Dim nX As Long, dx As Long Set Sel = tvwTree.HitTest(X, y) If Sel Is Nothing Then Exit Sub Set Pt = Sel X = ScaleX(X, vbTwips, vbPixels) - 3 Do Until Pt.Parent Is Nothing Set Pt = Pt.Parent Lvl = Lvl + 1 Loop nX = Lvl * tvwTree.Indentation dx = X - nX If dx <= iglIcon.ImageWidth Then If Sel.Image = CS_UNCHECKED Or Sel.Image = CS_GRAYED Then Sel.Image = CS_CHECKED MarkAllChild Sel, CS_CHECKED Else Sel.Image = CS_UNCHECKED MarkAllChild Sel, CS_UNCHECKED End If MarkAllParents Sel End If End Sub Private Sub MarkAllParents(Child As Node) Dim Prnt As Node Dim Chld As Node Dim Ct As Long Dim IsGr As Boolean Set Prnt = Child.Parent If Not Prnt Is Nothing Then Set Chld = Child.FirstSibling Do Until Chld Is Nothing If Chld.Image = CS_CHECKED Then Ct = Ct + 1 ElseIf Chld.Image = CS_GRAYED Then Ct = Ct + 1 IsGr = True Exit Do End If Set Chld = Chld.Next Loop If Ct = 0 Then Prnt.Image = CS_UNCHECKED ElseIf IsGr Or Ct < Prnt.Children Then Prnt.Image = CS_GRAYED ElseIf Ct = Prnt.Children Then Prnt.Image = CS_CHECKED End If MarkAllParents Prnt End If End Sub Private Sub MarkAllChild(Parent As Node, Value As ChkState) Dim Chld As Node Set Chld = Parent.Child Do Until Chld Is Nothing Chld.Image = Value If Chld.Children Then MarkAllChild Chld, Value Set Chld = Chld.Next DoEvents Loop End Sub
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д