CÔNG TY CỔ PHẦN BLUESOFTS

CÔNG TY CỔ PHẦN BLUESOFTS

Lập trình VBA tạo BSTreeView hiển thị icon

Tạo TreeView hiển thị icon/ ảnh củng Task Pane trong Excel VBA, sử dụng các controls: BSTreeView, BSImageLIst, BSTaskPane.

Support: Unicode, platforms 32, 64-bit.

Step 1: Import controls of BSAC. Click on Userform, see window "Toolbox", right click on tab "Controls" -> "Import Page", select file "ImportToToolbox.pag" ((Download).

(Do Step 1 only if you do not see BSAC controls on Toolbox window)

Step 2: Drag controls to userform: BSTreeView (name: BSTreeView1), BSImageList(name: BSImageList1), BSButton,...

Step 3: Add icons to BSImageList control.

+ Click on BSImageList1 on Userform, see window "Properties"->Custom..., click on button [...] on the right.

+ In window "Property Pages" click button "Add" to add icons to BSImageList control.

(Index of icon use in property ImageIndex of controls) 

Step 4: Coding: Create Task Pane by BSTaskPanes, BSTaskPane

'Author: Nguyen Duy Tuan - duytuan@bluesofts.net
'Website: http://bluesofts.net - http://atoolspro.com
'Support online: https://www.facebook.com/groups/hocexcel/
'View learn BSTreeView: https://www.youtube.com/watch?v=dIczazD5bxM
Option Explicit 
Dim TP As BSTaskPane 
Private Sub UserForm_Initialize() 
   'Create Task Pane by BSTaskPanes
   Dim TPs As New BSTaskPanes 
   Set TP = TPs.Add(ConvertStr("Menu lÖnh", AcTcvn3ToUnicode), Me, False) 
   TP.AllowHide = False  'Prevent user from click [X] on task pane
   Set TPs = Nothing 
   'AddIcons ' - No need in BSAC 3.0. Icons/Image added in design-time
   DoCreateBSTreeView 
   'DoCreateBSTreeView_Easy ' For lean easy
End Sub 

Private Sub UserForm_Terminate() 
   'BSImageList1.ListImages.Clear 'Use this line if you run method "AddIcons"
   Set TP = Nothing  'Free Task Pane
End Sub 

Private Sub UserForm_Resize() 
   On Error Resume Next  'Important!
   BSTreeview1.Height = Height - 26 
   BSTreeview1.Width = Width 
End Sub 

Step 5: Coding: Create TreeView from data sheet

+ Data for treeview:

- Key: index of node

- Text: text display for node

- Key Parent: is index of parent node.

- ImageIndex: Index of icon in BSImageList

- Link Sheet: Sheet name will open when click on node.

Private Sub DoCreateBSTreeView() 
   Dim I&, Node As BSTreeNode, sh As Worksheet 
   Dim Key As String, ParentKey As String, Text As String, idx& 
   Set sh = ThisWorkbook.Sheets("MENU") 
   BSTreeview1.ReadOnly = True 
   BSTreeview1.hImageList = frmResource.BSImageList1.hImageList  'Link icons/images to BSTreeVew
   BSTreeview1.Items.Clear 
   BSTreeview1.Font.Size = 12 
   BSTreeview1.CheckBoxes = True 
   BSTreeview1.Items.BeginUpdate  'For faster. You must run EndUpdate later
   For I = 4 To sh.Range("A1000").End(xlUp).Row  'Dong cuoi du lieu
      'Nhan gia tri cho bien
      Key = sh.Cells(I, 1).Value  'A
      Text = sh.Cells(I, 2)  'B
      ParentKey = sh.Cells(I, 3).Value  'C
      idx = sh.Cells(I, 4).Value  'D
      'Tao cac node cho BSTreeView
      If ParentKey = "" Then  'Parent in root
         Set Node = BSTreeview1.Items.Add(, Key, Text, idx) 
         Node.Bold = True 
      Else  'Child
         Set Node = BSTreeview1.Items.Add(ParentKey, Key, Text, idx) 
      End If 
   Next I 
   BSTreeview1.Items.EndUpdate 
   BSTreeview1.FullExpand 
   BSTreeview1.Items(0).MakeVisible 
End Sub 

Video instruction:

Download Source code

Author: Nguyễn Duy Tuân