PowerBuilder的TreeView控件clicked事件代码

 PowerBuilder的TreeView控件clicked事件代码,经过多位修改完善,近乎完美,没有显摆的意思,自己留存,各位老师留情。

long parent_handle,brother_handle
long i
TreeViewItem tvi_parent,tvi_cur,tvi_brother

IF not ib_stateclick  then Return  //如果不是在状态上点击,就返回
IF handle < 2 then  Return  //如果点击的是不能选择的item

//获得当前item
this.GetItem(handle,tvi_cur)

IF ( tvi_cur.StatePictureIndex = 2  ) then  //当前item 的StatePictureIndex = 2表示被选中,1未选中
	parent_handle = this.FindItem(ParentTreeItem!,handle)  //得到父项的句柄
	this.GetItem(parent_handle, tvi_parent)
	
	IF tvi_parent.level > 1 and tvi_parent.StatePictureIndex <>  2 THEN  //实现第一次选择子项时自动选中它的父项
		tvi_parent.StatePictureIndex = 2
		this.setRedraw(False)
		this.SetItem(parent_handle, tvi_parent)
		this.setRedraw(True)
	END IF
	
	IF KeyDown(KeyControl!) THEN
		//如果按Ctrl键 + clicked 选择所有子项
		parent_handle = this.FindItem(ChildTreeItem!,handle)
		IF parent_handle <>  -1 THEN
			 this.GetItem(parent_handle, tvi_parent)
			 tvi_parent.StatePictureIndex = 2
		    	 this.setRedraw(False)
			 this.SetItem(parent_handle, tvi_parent)
			 brother_handle = parent_handle
			 do while (brother_handle <> -1)
 				    brother_handle = this.FindItem(NextTreeItem!,brother_handle)
					this.GetItem(brother_handle, tvi_brother)
					if (tvi_brother.StatePictureIndex = 2)  then continue //如果该子item是被选中的,则跳过
					tvi_brother.StatePictureIndex = 2
					this.SetItem(brother_handle, tvi_brother)
			loop
			 this.setRedraw(True)			 	
		END IF	 
	END IF	
Else  //当前Item未选中
	tvi_cur.StatePictureIndex = 1
	IF KeyDown(KeyControl!) THEN
		//如果按Ctrl键 + clicked 取消所有子项
		parent_handle = this.FindItem(ChildTreeItem!,handle)
		IF parent_handle <>  -1 THEN
			 this.GetItem(parent_handle, tvi_parent)
			 tvi_parent.StatePictureIndex = 1
		    	 this.setRedraw(False)
			 this.SetItem(parent_handle, tvi_parent)
			 brother_handle = parent_handle
			 do while (brother_handle <> -1)
 				    brother_handle = this.FindItem(NextTreeItem!,brother_handle)
					this.GetItem(brother_handle, tvi_brother)
					if (tvi_brother.StatePictureIndex = 1)  then continue //如果该子item是被取消的,则跳过
					tvi_brother.StatePictureIndex = 1
					this.SetItem(brother_handle, tvi_brother)
			loop
			 this.setRedraw(True)			 	
		END IF	 
	END IF	

	//2020.12.6增加, 如果取消父项的选中,则对应子项全部取消
	IF this.FindItem(ChildTreeItem!,handle) <> -1  Then  //判断是否有子项,如果有则执行
		parent_handle = this.FindItem(ChildTreeItem!,handle)
		IF parent_handle <>  -1 THEN
			this.GetItem(parent_handle, tvi_parent)
			 tvi_parent.StatePictureIndex = 1
		    	 this.setRedraw(False)
			 this.SetItem(parent_handle, tvi_parent)
			 brother_handle = parent_handle
			 do while (brother_handle <> -1)
 				    brother_handle = this.FindItem(NextTreeItem!,brother_handle)
					this.GetItem(brother_handle, tvi_brother)
					if (tvi_brother.StatePictureIndex = 1)  then continue //如果该子item是被选中的,则跳过
					tvi_brother.StatePictureIndex = 1
					this.SetItem(brother_handle, tvi_brother)
			loop
			this.setRedraw(True)			 	
		End IF
	End IF	
	
	//10.23加,实现当取消的子item是最后一个的时候,连它的相应父item也取消
	parent_handle = this.FindItem(ParentTreeItem!,handle)
	this.GetItem(parent_handle, tvi_parent)
	//测试handle差值
	i = handle - parent_handle
	
       CHOOSE CASE (handle - parent_handle) 
			case 1   //如果点击的是某个父item的第一个子项,则依次往下找就可以了
				brother_handle = handle
				brother_handle = this.FindItem(NextTreeItem!,brother_handle)
				do while (brother_handle <> -1)
					this.GetItem(brother_handle, tvi_brother)
					if (tvi_brother.StatePictureIndex = 2)  then return  //如果有item是被选中的,则退出
					brother_handle = this.FindItem(NextTreeItem!,brother_handle)
				loop
				//如果所有的子item都没有被选中,那么父item也不选
			     tvi_parent.StatePictureIndex = 1
		   		this.setRedraw(False)
				this.SetItem(parent_handle, tvi_parent)
				this.setRedraw(True)			
			case is > 1 如果点击的不是某个父item的第一个子项,则需要往上往下都去找
				
				//先往下找
				brother_handle=handle
				brother_handle=this.FindItem(NextTreeItem!,brother_handle)
				do while (brother_handle <> -1)
					this.GetItem(brother_handle, tvi_brother)
					if (tvi_brother.StatePictureIndex = 2) then return  //如果有item是被选中的,则退出
					brother_handle=this.FindItem(NextTreeItem!,brother_handle)
				loop
				//再往上找
				brother_handle=handle
				brother_handle=this.FindItem(PreviousTreeItem!,brother_handle)
				do while (brother_handle <> -1)
					this.GetItem(brother_handle, tvi_brother)
					if (tvi_brother.StatePictureIndex = 2) then return  //如果有item是被选中的,则退出
					brother_handle=this.FindItem(PreviousTreeItem!,brother_handle)
				loop
				//如果所有的子item都没有被选中,那么父item也不选
			     tvi_parent.StatePictureIndex = 1
		   		this.setRedraw(False)
			     this.SetItem(parent_handle, tvi_parent)
		   		this.setRedraw(True)
	END CHOOSE
		//10.23加
end if

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页