继承创建自定义的TreeView
2024-07-21 02:24:20
供稿:网友
customtreeviewform类,好东西大家分享,好好研究一下哦!
public class customtreeviewform
inherits system.windows.forms.form
#region " windows form designer generated code "
public sub new()
mybase.new()
'this call is required by the windows form designer.
initializecomponent()
'add any initialization after the initializecomponent() call
initializeform()
end sub
'form overrides dispose to clean up the component list.
protected overloads overrides sub dispose(byval disposing as boolean)
if disposing then
if not (components is nothing) then
components.dispose()
end if
end if
mybase.dispose(disposing)
end sub
'required by the windows form designer
private components as system.componentmodel.icontainer
'note: the following procedure is required by the windows form designer
'it can be modified using the windows form designer.
'do not modify it using the code editor.
friend withevents tvwcategories as system.windows.forms.treeview
friend withevents txtselectedid as system.windows.forms.textbox
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
me.tvwcategories = new system.windows.forms.treeview()
me.txtselectedid = new system.windows.forms.textbox()
me.suspendlayout()
'
'tvwcategories
'
me.tvwcategories.imageindex = -1
me.tvwcategories.location = new system.drawing.point(7, 2)
me.tvwcategories.name = "tvwcategories"
me.tvwcategories.selectedimageindex = -1
me.tvwcategories.size = new system.drawing.size(277, 240)
me.tvwcategories.tabindex = 0
'
'txtselectedid
'
me.txtselectedid.location = new system.drawing.point(6, 246)
me.txtselectedid.name = "txtselectedid"
me.txtselectedid.size = new system.drawing.size(278, 20)
me.txtselectedid.tabindex = 1
me.txtselectedid.text = "txtselectedid"
'
'customtreeviewform
'
me.autoscalebasesize = new system.drawing.size(5, 13)
me.clientsize = new system.drawing.size(292, 273)
me.controls.addrange(new system.windows.forms.control() {me.txtselectedid, me.tvwcategories})
me.name = "customtreeviewform"
me.text = "custom treeview"
me.resumelayout(false)
end sub
#end region
private sub initializeform()
dim objcategories as new categories()
dim i as integer
dim x as integer
for i = 1 to 5
with objcategories.add(new category())
.categoryid = "cat" & i
.displayname = "category " & i
for x = 1 to 3
with .accounttypes.add(new accounttype())
.accounttypeid = "a" & x
.displayname = "accounttype " & x
end with
with .parttypes.add(new parttype())
.parttypeid = "p" & x
.displayname = "parttype " & x
end with
next x
end with
next i
with me.tvwcategories
dim objcategory as category
dim objaccounttype as accounttype
dim objparttype as parttype
dim objcategorytreenode as categorytreenode
for each objcategory in objcategories
objcategorytreenode = new categorytreenode(objcategory)
.nodes.add(objcategorytreenode)
with objcategorytreenode
for each objaccounttype in objcategory.accounttypes
.nodes.add(new accounttypetreenode(objaccounttype))
next
for each objparttype in objcategory.parttypes
.nodes.add(new parttypetreenode(objparttype))
next
end with
next
end with
end sub
private sub tvwcategories_afterselect(byval sender as system.object, byval e as system.windows.forms.treevieweventargs) handles tvwcategories.afterselect
dim objnode as treenode
objnode = tvwcategories.selectednode
select case objnode.gettype.tostring
case "customtreeview.categorytreenode"
dim objcategorytreenode as categorytreenode
objcategorytreenode = ctype(objnode, categorytreenode)
txtselectedid.text = objcategorytreenode.category.categoryid.tostring
case "customtreeview.accounttypetreenode"
dim objaccounttypetreenode as accounttypetreenode
objaccounttypetreenode = ctype(objnode, accounttypetreenode)
txtselectedid.text = objaccounttypetreenode.accounttype.accounttypeid.tostring
case "customtreeview.parttypetreenode"
dim objparttypetreenode as parttypetreenode
objparttypetreenode = ctype(objnode, parttypetreenode)
txtselectedid.text = objparttypetreenode.parttype.parttypeid.tostring
end select
end sub
end class
public class accounttype
public accounttypeid as string
public displayname as string
end class
public class parttype
public parttypeid as string
public displayname as string
end class
public class category
public categoryid as string
public displayname as string
public accounttypes as new accounttypes()
public parttypes as new parttypes()
end class
public class categories
inherits collectionbase
public function add(byval value as category) as category
me.innerlist.add(value)
add = value
end function
end class
public class accounttypes
inherits collectionbase
public function add(byval value as accounttype) as accounttype
me.innerlist.add(value)
add = value
end function
end class
public class parttypes
inherits collectionbase
public function add(byval value as parttype) as parttype
me.innerlist.add(value)
add = value
end function
end class
public class accounttypetreenode
inherits treenode
public accounttype as accounttype
sub new(byval value as accounttype)
mybase.new()
accounttype = value
me.text = accounttype.displayname
end sub
end class
public class parttypetreenode
inherits treenode
public parttype as parttype
sub new(byval value as parttype)
mybase.new()
parttype = value
me.text = parttype.displayname
end sub
end class
public class categorytreenode
inherits treenode
public category as category
sub new(byval value as category)
mybase.new()
category = value
me.text = category.displayname
end sub
end class