dbicList Send comments on this topic.
Developing with dbicList - Drag and Drop

Glossary Item Box

 

In addition to the standard drag and drop functionality found in most controls, the dbicList control extends support for drag and drop by providing a custom DragOut eventthat fires when a drag operation leaves the control and a custom DropList event that fires when a drop action occurs on the control which passes the nodeItem on which the drop event took place.  Using the control's collection-based items architecture and multi-select capability, the control allows for complex drag and drop operations.

 

To implement dragging of objects from the dbicList control set the AllowDrop property of the target object(s) to True for any control that will accept drop actions from the dbicList.  Start a drag by calling the host form’s DoDragDrop method in the dbicList DragOut event (an event that fires when a drag operation leaves the dbicList control).  Pass the method the dbicList object as the drag data and specify the DragDropEffects.Move effect to illustrate to the user an object from the dbicList is being dragged.  NOTE: For simple dragging of nodes from a dbicList control the drag data could be set to the nodeItem being dragged, however to handle more complex drag operations for multi-selected nodes setting the drag data to the source dbicList provides a handle back to the dbicList’s selectedNodes collection allowing for the dragging of multiple nodes simultaneously.

 

Example: To initiate the drag operation …

 

VB.NET

 

Private Sub dbicList_DragOut(ByVal sender As Object, ByVal e As Dbi.WinControl.List.DragOutEventArgs) Handles dbicList.DragOut

'This event fires when a drag operation leaves the dbicList control

'Start the drag/drop operation.  NOTE: A reference to the list control being dragged is placed in the

'data property

 

Me.DoDragDrop(Me.dbicList, DragDropEffects.Move)

 

End Sub

 

C#

 

private void dbicList_DragOut(object sender, Dbi.WinControl.List.DragOutEventArgs e)

{

// This event fires when a drag operation leaves the dbicList control

// Start the drag/drop operation.  NOTE: A reference to the list control being dragged is placed in

// the data property

           

this.DoDragDrop(this.dbicList, DragDropEffects.Move);

}

 

To respond to a drag operation code the remainder of the action in the target object(s).  For the purpose of this example the target of the drag/drop is another instance of the dbicList control (dbicListTarget). The first event to fire is DragDrop event.  In the DragDrop event set the tag property of the target to contain the data being dragged (in this case the source dbicList control).  The DragDrop event automatically fires the dbicList control’s DropList event.  This event is similar to the DragDrop event with the extended ability to identify the nodeItem on which the drop event took place.  This allows the developer the option of inserting the dropped item(s) at the point where the drop took place.  In the example below, the selected nodeItemsin the source list are inserted into the target list at the nodeItem on which the drop occurs, and then the selected nodeItems in the source list are removed.

 

Example: To respond to the drop event …

 

VB.NET

 

Private Sub dbicListTarget_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) _

Handles dbicListTarget.DragDrop

'This event fires when an object is dropped on the list

'NOTE: Test the data type using the GetDataPresent method on the Data being dragged.

 

   If e.Data.GetDataPresent("Dbi.WinControl.List.dbiList", True) Then

 

       'The data being dragged is a dbicList control. 

       'Place the drag data in the tag property of the dbicList control to make it

       'available to the DropList event.

       Me.dbicListTarget.Tag = e.Data.GetData("Dbi.WinControl.List.dbiList")

 

   End If

 

End Sub

 

Private Sub dbicListTarget_DropList(ByVal sender As System.Object, ByVal e As _

Dbi.WinControl.DropListEventArgs) Handles dbicList2.DropList

'This event is fired internally by the DragDrop event.  NOTE: The e value passed in provides an

'item parameter that identifies the dbiNodeItem on which the drop event occurred.

 

Dim ListSource As Dbi.WinControl.List.dbicList

Dim curNode As Dbi.WinControl.dbiNodeItem

Dim countR As Integer

Dim selectedItemsCount As Integer

 

'NOTE: The source list object for the drag drop operation was stored in the tag of the

'target list control when the drag/drop took place

ListSource = Me.dbicListTarget.Tag

 

'Store the selected items count in a variable as the value will change as items are deleted

selectedItemsCount = ListSource.SelectedItems.Count

 

'NOTE: By iterating through the selected items collection from the bottom to the top

'the items can be deleted as they are processed.

For countR = selectedItemsCount - 1 To 0 Step -1

   Dim newRow As New Dbi.WinControl.dbiNodeItem

 

'Set the curNode to the dbiNodeItem in the SelectedItems collection in the dbicList

'stored in the tag property of the target dbicList control.

curNode = ListSource.SelectedItems(countR)

 

'Set the text property of the new nodeItem to the text property of the nodeItem

'stored in the SelectedItems collection in the dbicList stored in the tag property dbicList

'of the target control.

'NOTE: The dbicList is stored in the tag in the DragDrop event of the target dbicList control.

newRow.Text = curNode.Text

'Insert the new node above the dbiNodeItem the list where the drop event occurred (e.item)

Me.dbicListTarget.Items.Insert(Me.dbicList2.Items.IndexOf(e.Item), newRow)

'Remove the original dbiNodeItem from the source dbicList control

Me.dbicList.Items.Remove(ListSource.SelectedItems(countR))

 

Next countR

 

Me.dbicListTarget.Tag = Nothing

 

End Sub

 

 

C#

 

private void dbicListTarget_DragDrop(object sender, DragEventArgs e)

{

// This event fires when an object is dropped on the list

// NOTE: Test the data type using the GetDataPresent method on the Data being dragged.

 

bool dragObjectIsMyClassList = (e.Data.GetDataPresent(typeof(Dbi.WinControl.List.dbiList)) == true);

bool dragObjectIsMyClassNode = (e.Data.GetDataPresent(typeof(Dbi.WinControl.dbiNodeItem)) == true);

bool dragObjectIsMyClassSys32 = (e.Data.GetDataPresent(typeof(System.Int32)) == true);

if (dragObjectIsMyClassList)

{

       // Place the drag data in the tag property of the dbicList control to make it

       // available to the DropList event.

 

this.dbicListTarget.Tag = e.Data.GetData("Dbi.WinControl.List.dbiList");

}

 

private void dbicListTarget_DropList(object sender, Dbi.WinControl.DropListEventArgs e)

{

// This event is fired internally by the DragDrop event.  NOTE: The e value passed in provides an

// item parameter that identifies the dbiNodeItem on which the drop event occurred.

 

Dbi.WinControl.List.dbicList ListSource;

Dbi.WinControl.dbiNodeItem curNode;

int countR;

int selectedItemsCount;

 

// NOTE: The source list object for the drag drop operation was stored in the tag of the

// target list control when the drag/drop took place

ListSource = (Dbi.WinControl.List.dbicList)this.dbicListTarget.Tag;

 

// Store the selected items count in a variable as the value will change as items are deleted

selectedItemsCount = ListSource.SelectedItems.Count;

 

// NOTE: By iterating through the selected items collection from the bottom to the top

// the items can be deleted as they are processed.

for (countR = selectedItemsCount - 1; countR >= 0; countR--)

{

Dbi.WinControl.dbiNodeItem newRow = new Dbi.WinControl.dbiNodeItem();

 

// Set the curNode to the dbiNodeItem in the SelectedItems collection in the dbicList

// stored in the tag property of the target dbicList control.

curNode = (Dbi.WinControl.dbiNodeItem)ListSource.SelectedItems[countR];

 

// Set the text property of the new nodeItem to the text property of the nodeItem

// stored in the SelectedItems collection in the dbicList stored in the tag property dbicList of

// the target control.

// NOTE: The dbicList is stored in the tag in the DragDrop event of the target dbicList control.

newRow.Text = curNode.Text;

 

// Insert the new node above the dbiNodeItem the list where the drop event occurred (e.item)

this.dbicListTarget.Items.Insert(this.dbicListTarget.Items.IndexOf(e.Item), newRow);

 

// Remove the original dbiNodeItem from the source dbicList control

this.dbicList.Items.Remove((Dbi.WinControl.dbiNodeItem)ListSource.SelectedItems[countR]);

   }

 

   this.dbicListTarget.Tag = null;

 

}