Single/Multi Select TreeView In PowerBuilder
TreeView Object:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 | forward global type u_tv from treeview end type type os_point from structure within u_tv end type type os_tvhittestinfo from structure within u_tv end type end forward type os_point from structure long l_x long l_y end type type os_tvhittestinfo from structure os_point str_pt long l_flags long l_hitem end type global type u_tv from treeview integer width = 503 integer height = 368 integer taborder = 10 integer textsize = -8 integer weight = 400 fontcharset fontcharset = ansi! fontpitch fontpitch = variable! fontfamily fontfamily = swiss! string facename = "Tahoma" long textcolor = 33554432 borderstyle borderstyle = stylelowered! string picturename[] = {"DatabaseProfile!"} integer picturewidth = 16 integer pictureheight = 16 long picturemaskcolor = 12632256 string statepicturename[] = {"state0.bmp","state1.bmp","radio0.bmp","radio1.bmp"} integer statepicturewidth = 16 integer statepictureheight = 16 long statepicturemaskcolor = 16777215 event mousemove pbm_mousemove end type global u_tv u_tv type prototypes Function Long HitMsg(Long hWindow, UInt Msg, Long wParam, Ref os_tvhittestinfo lParam) Library 'user32.dll' Alias for "SendMessageA;Ansi" end prototypes type variables Public: Constant UInt TVM_HITTEST = 4369 Constant UInt TVHT_ONITEMSTATEICON = 64 Protected: Boolean ib_stateclick Long il_handle end variables forward prototypes public function boolean of_isselected (long al_handle) public function integer of_deselectall () public function integer of_deselectall (long al_root) public function integer of_selectitem (long al_handle, boolean ab_switch) public function integer of_getselecteditems (long al_root, ref any aa_data[]) public function integer of_getselecteditems (ref any aa_data[]) public function integer of_selectall (long al_root) public function integer of_selectall () public function integer of_getselecteditems (long al_root, ref long al_handles[]) public function integer of_getselecteditems (ref long al_handles[]) public function integer of_getselecteditems (long al_root, ref string as_strings[]) public function integer of_getselecteditems (ref string as_strings[]) end prototypes event mousemove;os_tvhittestinfo lstr_tvhittest lstr_tvhittest.str_pt.l_x = UnitsToPixels(xpos, XUnitsToPixels!) lstr_tvhittest.str_pt.l_y = UnitsToPixels(ypos, YUnitsToPixels!) HitMsg(il_handle, TVM_HITTEST, 0, lstr_tvhittest) If lstr_tvhittest.l_hItem <> 0 Then ib_stateclick = (lstr_tvhittest.l_flags = TVHT_ONITEMSTATEICON) End If end event public function boolean of_isselected (long al_handle);TreeViewItem ltvi_node Long ll_rc ll_rc = This.GetItem(al_handle, ltvi_node) If ll_rc > 0 Then Return ltvi_node.StatePictureIndex = 2 Or ltvi_node.StatePictureIndex = 4 End If Return False end function public function integer of_deselectall ();Integer li_rc = 1 Long ll_root Long ll_count ll_root = FindItem(RootTreeItem!, 0) If ll_root > 0 Then This.Of_DeSelectAll(ll_root) ll_root = This.FindItem(NextTreeItem!, ll_root) Do While ll_root > 0 This.Of_DeSelectall(ll_root) ll_root = This.FindItem(NextTreeItem!, ll_root) Loop Else li_rc = 0 End If Return li_rc end function public function integer of_deselectall (long al_root);TreeViewItem ltvi_node Integer li_rc Long ll_root Long ll_count If al_root > 0 Then ll_root = al_root Do ll_root = FindItem(ChildTreeItem!, ll_root) Do This.GetItem(ll_root, ltvi_node) If ltvi_node.StatePictureIndex = 2 Then ltvi_node.StatePictureIndex = 1 This.SetItem(ll_root, ltvi_node) End If ll_root = This.FindItem(NextTreeItem!, ll_root) Loop While ll_root > 0 Loop While ll_root > 0 End If Return li_rc end function public function integer of_selectitem (long al_handle, boolean ab_switch);TreeViewItem ltvi_node Integer li_rc Long ll_handle If al_handle > 0 Then This.GetItem(al_handle, ltvi_node) If ab_switch Then Choose Case ltvi_node.StatePictureIndex Case 1 ltvi_node.StatePictureIndex = 2 // Check Box This.SetItem(al_handle, ltvi_node) li_rc = 1 Case 3 ltvi_node.StatePictureIndex = 4 // Check Radio This.SetItem(al_handle, ltvi_node) // Uncheck all sibling radio buttons. ll_handle = al_handle Do While ll_handle <> -1 ll_handle = This.FindItem(PreviousTreeItem!, ll_handle) If ll_handle > 0 Then This.GetItem(ll_handle, ltvi_node) If ltvi_node.StatePictureIndex = 4 Then ltvi_node.StatePictureIndex = 3 This.SetItem(ll_handle, ltvi_node) End If End If Loop ll_handle = al_handle Do While ll_handle <> -1 ll_handle = This.FindItem(NextTreeItem!, ll_handle) If ll_handle > 0 Then This.GetItem(ll_handle, ltvi_node) If ltvi_node.StatePictureIndex = 4 Then ltvi_node.StatePictureIndex = 3 This.SetItem(ll_handle, ltvi_node) End If End If Loop li_rc = 1 End Choose Else Choose Case ltvi_node.StatePictureIndex Case 2 ltvi_node.StatePictureIndex = 1 // Uncheck Box This.SetItem(al_handle, ltvi_node) li_rc = 1 Case 4 // Cannot uncheck radio -- Only through checking another item. li_rc = 0 End Choose End If End If Return li_rc end function public function integer of_getselecteditems (long al_root, ref any aa_data[]);TreeViewItem ltvi_node Integer li_rc Long ll_root Long ll_count If al_root > 0 Then ll_root = al_root Do ll_root = This.FindItem(ChildTreeItem!, ll_root) Do This.GetItem(ll_root, ltvi_node) If ltvi_node.StatePictureIndex = 2 Or ltvi_node.StatePictureIndex = 4 Then ll_count = UpperBound( aa_data[] ) + 1 aa_data[ll_count] = ltvi_node.Data End If ll_root = This.FindItem(NextTreeItem!, ll_root) Loop While ll_root > 0 Loop While ll_root > 0 End If Return li_rc end function public function integer of_getselecteditems (ref any aa_data[]);TreeViewItem ltvi_node Integer li_rc = 1 Long ll_root Long ll_count ll_root = This.FindItem(RootTreeItem!, 0) If ll_root > 0 Then This.of_GetSelectedItems(ll_root, aa_data) ll_root = This.FindItem(NextTreeItem!, ll_root) Do While ll_root > 0 This.Of_GetSelectedItems(ll_root, aa_data) ll_root = This.FindItem(NextTreeItem!, ll_root) Loop Else li_rc = 0 End If Return li_rc end function public function integer of_selectall (long al_root);TreeViewItem ltvi_node Integer li_rc Long ll_root Long ll_count If al_root > 0 Then ll_root = al_root Do ll_root = This.FindItem(ChildTreeItem!, ll_root) Do This.Of_SelectItem(ll_root, True) ll_root = This.FindItem(NextTreeItem!, ll_root) Loop While ll_root > 0 Loop While ll_root > 0 End If Return li_rc end function public function integer of_selectall ();TreeViewItem ltvi_node Integer li_rc = 1 Long ll_root Long ll_count ll_root = This.FindItem(RootTreeItem!, 0) If ll_root > 0 Then This.Of_SelectAll(ll_root) ll_root = This.FindItem(NextTreeItem!, ll_root) Do While ll_root > 0 This.Of_SelectAll(ll_root) ll_root = This.FindItem(NextTreeItem!, ll_root) Loop Else li_rc = 0 End If Return li_rc end function public function integer of_getselecteditems (long al_root, ref long al_handles[]);TreeViewItem ltvi_node Integer li_rc Long ll_root Long ll_count If al_root > 0 Then ll_root = al_root Do ll_root = This.FindItem(ChildTreeItem!, ll_root) Do This.GetItem(ll_root, ltvi_node) If ltvi_node.StatePictureIndex = 2 Or ltvi_node.StatePictureIndex = 4 Then ll_count = UpperBound(al_handles[]) + 1 al_handles[ll_count] = ll_root End If ll_root = This.FindItem(NextTreeItem!, ll_root) Loop While ll_root > 0 Loop While ll_root > 0 End If Return li_rc end function public function integer of_getselecteditems (ref long al_handles[]);TreeViewItem ltvi_node Integer li_rc = 1 Long ll_root Long ll_count ll_root = This.FindItem(RootTreeItem!, 0) If ll_root > 0 Then This.Of_GetSelectedItems(ll_root, al_handles) ll_root = This.FindItem(NextTreeItem!, ll_root) Do While ll_root > 0 This.Of_GetSelectedItems(ll_root, al_handles) ll_root = This.FindItem(NextTreeItem!, ll_root) Loop Else li_rc = 0 End If Return li_rc end function public function integer of_getselecteditems (long al_root, ref string as_strings[]);TreeViewItem ltvi_node Integer li_rc Long ll_root Long ll_count If al_root > 0 Then ll_root = al_root Do ll_root = This.FindItem(ChildTreeItem!, ll_root) Do This.GetItem(ll_root, ltvi_node) If ltvi_node.StatePictureIndex = 2 Or ltvi_node.StatePictureIndex = 4 Then ll_count = UpperBound(as_strings[]) + 1 as_strings[ll_count] = ltvi_node.Label End If ll_root = This.FindItem(NextTreeItem!, ll_root) Loop While ll_root > 0 Loop While ll_root > 0 End If Return li_rc end function public function integer of_getselecteditems (ref string as_strings[]);TreeViewItem ltvi_node Integer li_rc = 1 Long ll_root Long ll_count ll_root = This.FindItem(RootTreeItem!, 0) If ll_root > 0 Then This.Of_GetSelectedItems(ll_root, as_strings) ll_root = This.FindItem(NextTreeItem!, ll_root) Do While ll_root > 0 This.Of_GetSelectedItems(ll_root, as_strings) ll_root = This.FindItem(NextTreeItem!, ll_root) Loop Else li_rc = 0 End If Return li_rc end function event clicked;Long ll_rc If ib_stateclick Then ll_rc = This.Of_SelectItem(Handle, Not This.Of_IsSelected(Handle)) End If Return ll_rc end event event constructor;il_handle = Handle(This) end event event selectionchanging;If newhandle > 0 Then If ib_stateclick Then Return 1 End If end event on u_tv.create end on on u_tv.destroy end on |
Example Window
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | forward global type w_main from window end type type tv_multi from u_tv within w_main end type end forward global type w_main from window integer width = 1349 integer height = 1596 boolean titlebar = true string title = "Multi Select TreeView" boolean controlmenu = true boolean minbox = true boolean maxbox = true boolean resizable = true long backcolor = 67108864 string icon = "AppIcon!" boolean center = true tv_multi tv_multi end type global w_main w_main on w_main.create this.tv_multi=create tv_multi this.Control[]={this.tv_multi} end on on w_main.destroy destroy(this.tv_multi) end on event open;TreeViewItem ltvi_root TreeViewItem ltvi_node Integer li_index Integer li_items = 5 Long ll_lev0 Long ll_lev1 Long ll_lev2 String ls_list String ls_root = 'Non-mutually exclusive items' ltvi_root.PictureIndex = 1 ltvi_root.SelectedPictureIndex = 1 ltvi_node.PictureIndex = 2 ltvi_node.SelectedPictureIndex = 2 ltvi_node.StatePictureIndex = 1 // Root ltvi_root.Label = ls_root ll_lev1 = tv_multi.InsertItemLast(0, ltvi_root) // Nodes For li_index = 1 To li_items ltvi_node.Label = "Item " + String(li_index) ll_lev2 = tv_multi.InsertItemLast(ll_lev1, ltvi_node) Next ll_lev0 = tv_multi.FindItem(RootTreeItem!, 0) tv_multi.ExpandAll(ll_lev0) // Radio buttons. String ls_items[3] String ls_blank[3] // root ltvi_root.PictureIndex = 4 ltvi_root.SelectedPictureIndex = 4 ltvi_root.Label = 'Mutually exclusive items' ll_lev1 = tv_multi.InsertItemLast(0, ltvi_root) // nodes ls_items = ls_blank ls_items[1] = 'Item 1' ls_items[2] = 'Item 2' ls_items[3] = 'Item 3' li_items = UpperBound(ls_items) For li_index = 1 To li_items - 1 ltvi_node.Label = ls_items[li_index] ltvi_node.PictureIndex = 5 ltvi_node.SelectedPictureIndex = 5 ltvi_node.StatePictureIndex = 3 ll_lev2 = tv_multi.InsertItemSort(ll_lev1, ltvi_node) Next ltvi_node.Label = ls_items[3] ltvi_node.PictureIndex = 6 ltvi_node.SelectedPictureIndex = 6 ll_lev2 = tv_multi.InsertItemSort(ll_lev1, ltvi_node) tv_multi.Of_SelectItem(ll_lev2 - 2, True) ll_lev0 = tv_multi.FindItem(RootTreeItem!, 0) tv_multi.ExpandAll(ll_lev0) ll_lev0 = tv_multi.FindItem(NextTreeItem!, ll_lev0) tv_multi.ExpandAll(ll_lev0) end event type tv_multi from u_tv within w_main integer x = 37 integer y = 32 integer width = 1207 integer height = 1312 string picturename[] = {"Library!","Window!","RunWindow!","Library!","ViewPainter!","WebPBWizard!"} end type |
Find Projects On Github click here
Good Luck!