|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionIn this article, we will learn how to drag an item from a The Module IdeaThe module populates the
Fig. (a) Initial drag-drop screen After the drag and drop activity, you will have something like what is shown in the following screenshot:
Fig. (b) Final drag-drop screen Customers enlisted in the adjoining In this module, the names of the different controls are:
Drag-Drop OperationIn the drag operation of an item from the ListBox_MouseDownThis event occurs when the mouse pointer is over the control and a mouse button is pressed. We use this event to capture the item which is being dragged from the lstCust.Items(lstCust.IndexFromPoint(e.X, e.Y)).ToString()
where DoDragDrop Method: Begins a drag-and-drop operation.
Public Function DoDragDrop( _
ByVal data As Object, _
ByVal allowedEffects As DragDropEffects _
) As DragDropEffects
Parameters
In our module, we have used the following line of code: DoDragDrop(strItem, DragDropEffects.All)
In the drop operation of the item from the trvCntry_DragEnterThis event occurs when an object is dragged into the control's bounds. We have written the following lines of code in this event: If e.Data.GetDataPresent(DataFormats.Text) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
trvCntry_DragDropThis event occurs when a drag-and-drop operation is completed. We have written the following lines of code in this event. We store the dragged item in a dummy node to be dropped on the strNodeDragged = CType(e.Data.GetData(strDummy.GetType), String)
DragNode = New TreeNode(strNodeDragged)
Next, we find the target item in the position.X = e.X
position.Y = e.Y
position = trvCntry.PointToClient(position)
DropNode = Me.trvCntry.GetNodeAt(position)
We validate the target If IsNothing(DropNode) Or DropNode.Text = "Country" Then
Exit Sub
End If
To ensure that items should only be dragged on folders, we assign ‘N’ to the items and ‘Y’ to the folder node, by editing the If IsNothing(DragNode.Tag) Then
DragNode.Tag = "N"
End If
Now, our next task is to check whether the dragged item is already in the '*************************************************************
' Function Name : ExistInTreeView
' Purpose : This Function Removes The DragNode
' from the tree from its Previous Place
' Input : DragNode,Node
' Output : Integer
' Author : Ujwal Watgule
' Date : 6th May 2006
' Modification History
' --------------------------------------------------------
'*************************************************************
Private Function ExistInTreeView(ByVal node As TreeNode, _
ByVal nddrag As TreeNode) As Integer
Dim anode As TreeNode
Try
anode = New TreeNode()
For Each anode In node.Nodes
If Trim(anode.Text) = Trim(nddrag.Text) Then
trvCntry.Nodes.Remove(anode)
Return sThree
End If
If anode.Nodes.Count > 0 Then
ExistInTreeView(anode, nddrag)
End If
If Trim(anode.Text) = Trim(nddrag.Text) Then
trvCntry.Nodes.Remove(anode)
Return sThree
End If
Next
Catch ex As Exception
Throw ex
End Try
End Function
As shown below, we loop through every node of the mTrNode = New TreeNode()
For Each mTrNode In trvCntry.Nodes
ExistInTreeView(mTrNode, DragNode)
Next
Now, we are ready to attach the dragged item to the targeted node of the If the targeted node is a folder node, then the dragged item can be inserted with the help of the following line of code. But if the targeted node is not a folder node, then the dragged item must be inserted in the parent of the targeted node. As shown in the following lines of code, we have set the If DropNode.Tag = "Y" Then
DropNode.Nodes.Insert(DropNode.Index + 1, DragNode)
DragNode.Tag = "N"
DragNode.ImageIndex = 5
DragNode.SelectedImageIndex = 5
DropNode.Expand()
Else
DropNode.Parent.Nodes.Insert(DropNode.Index + 1, DragNode)
DragNode.Tag = "N"
DragNode.ImageIndex = 5
DragNode.SelectedImageIndex = 5
DropNode.Expand()
End If
We have also set the Now, the item is successfully dropped under the targeted folder. The remaining task is to reflect the correct item path in the adjoining New Folder ButtonWe can add new node under the Country folder with the help of a New Folder button. We have written the following lines of code in the New Folder button 'Make sure the folder is being created under folder node only
If trvCntry.SelectedNode.Tag = "Y" Then
SubFolder = New TreeNode()
'If node selected is the last node
If IsNothing(trvCntry.SelectedNode.LastNode) Then
trvCntry.SelectedNode.Nodes.Insert(
trvCntry.SelectedNode.Index + 1, SubFolder)
trvCntry.SelectedNode.Expand()
Else
trvCntry.SelectedNode.Nodes.Insert(
trvCntry.SelectedNode.LastNode.Index + 1, SubFolder)
trvCntry.SelectedNode.Expand()
End If
SubFolder.Tag = "Y"
SubFolder.ImageIndex = 0
SubFolder.Text = "NewFolder"
trvCntry.SelectedNode = SubFolder
trvCntry.LabelEdit = True
trvCntry.SelectedNode.BeginEdit()
SubFolder.NodeFont = mFnt
End If
Delete Folder ButtonThe Delete Folder button allows us to delete a folder, and thereby any other items present under that folder to get refreshed (i.e., their color changes from black to blue) in the If trvCntry.SelectedNode.Text = "Country" Then
Exit Sub
End If
If trvCntry.SelectedNode.Tag = "Y" Then
DeleteItems(trvCntry.SelectedNode)
trvCntry.SelectedNode.Remove()
End If
Now, to refresh the items under the folder to be deleted, we call the method '*****************************************************
' Function Name : DeleteItems
' Purpose : This Subroutine Deletes
' The item From the Treeview
' Input : Treenode
' Output : Nothing
' Author : Ujwal Watgule
' Date : 11th May 2006
'*****************************************************
Private Sub DeleteItems(ByVal mNode As TreeNode)
Dim aNode As TreeNode
Try
For Each aNode In mNode.Nodes
DeleteItems(aNode)
RefreshList(aNode.Text)
Next
Catch ex As Exception
Throw ex
End Try
End Sub
The '*********************************************************
' Function Name : RefreshList
' Purpose : This Subroutine Refreshes The Listbox
' Input : Items Name
' Output : Nothing
' Author : Ujwal Watgule
' Date : 11th May 2006
'*********************************************************
Private Sub RefreshList(ByVal mItem As String)
Dim strRefreshListItem As String
Dim strChkItem As String
Dim intCntr, icntr As Integer
Try
For intCntr = 0 To lstCust.Items.Count - 1
strRefreshListItem = lstCust.Items(intCntr)
For icntr = 0 To strRefreshListItem.Length - 1
If mItem = strRefreshListItem.Substring(0, icntr) Then
strChkItem = mItem
lstCust.Items.RemoveAt(intCntr)
lstCust.Items.Insert(intCntr, (strChkItem))
lstCust.Refresh()
Exit For
End If
Next
Next
Catch ex As Exception
Throw ex
End Try
End Sub
ConclusionIn this article, we have learned about the events of
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||