The development environment is based on Vsto:visual studio 2010,VB. Net,excel 2007, a document-level customization program. The requirement is to dock the System.Windows.Forms.TreeView control on the left side of the sheet, implementing a resource-browser-like effect, and the tree node uses a custom icon to support the check box.
By first preparing the tree node icons, you can save a lot of hassle by using Visual Studio 2010 's icon (in the installation directory \common7\vs2010imagelibrary). I selected 4 16x16-sized icons to be copied to the resources directory under the VSTO project. Add an icon to the project by project->xxx properties->resources->addresource->add Existing file, The code can be referenced in My.Resources.aaa (AAA is the icon name).
Next, the initialization code for the icon and the TreeView control is completed in the sheet initialization function, and you need to add code to the Sheet_change function if the contents of the TreeView control need to be refreshed with sheet data changes.
PublicClassSheet1
Public WithEventsM_tree asNewTreeview
PublicM_imagelist asNewImageList
PrivateSubSheet1_Startup ()HandlesMe. Startup
M_imagelist.colordepth = Colordepth.depth32bit
'The first parameter is ImageKey, similar to the role of the image name
M_IMAGELIST.IMAGES.ADD ("CFG", My.Resources.book_notebook)
M_IMAGELIST.IMAGES.ADD ("Algo", my.resources._075b_upfolder_16x16_72)
M_IMAGELIST.IMAGES.ADD ("Scen", My.Resources.PlayHS)
M_IMAGELIST.IMAGES.ADD ("Warning", my.resources._109_allannotations_warning_16x16_72)
GLOBALS.THISWORKBOOK.ACTIONSPANE.CONTROLS.ADD (M_tree)
Application.CommandBars ("Task Pane"). Position = Microsoft.Office.Core.MsoBarPosition.msoBarLeft
M_tree. ImageList = M_imagelist
M_tree. checkboxes =True
'The tree's dock is automatically resized and needs to be set after add to pane, otherwise it won't take effect
M_tree. Dock = DockStyle.Fill
withM_tree
. BeginUpdate ()
. Nodes.clear ()
. Nodes.Add (NewTreeNode (Me. Name))
. Nodes (0). ImageKey ="CFG"
. Nodes (0). SelectedImageKey ="CFG"
DimIdx asInteger= . Nodes.Add (NewTreeNode ("Node1"))
. Nodes (IDX). ImageKey ="Algo"
. Nodes (IDX). SelectedImageKey ="Algo"
DimIdx2 asInteger= . Nodes (IDX). Nodes.Add (NewTreeNode ("case1_5m"))
. Nodes (IDX). Nodes (IDX2). ImageKey ="Scen"
. Nodes (IDX). Nodes (IDX2). SelectedImageKey ="Scen"
. EndUpdate ()
End with
End Sub
PrivateSubSheet1_change (Target asMicrosoft.Office.Interop.Excel.Range)HandlesMe. Change
'Do Tree Refresh
End Sub
End Class
:
Add a TreeView control to the left of a worksheet