Active Query Builder support area

How to drag an object from the database schema tree to external control?

Last modified:


listBox1.AllowDrop = true;
listBox1.DragDrop += new System.Windows.Forms.DragEventHandler(listBox1_DragDrop);
listBox1.DragOver += new System.Windows.Forms.DragEventHandler(listBox1_DragOver);

private MetadataDragObject GetMetadataDragObject(DragEventArgs e)
{
    var dataObject = (DataObject)e.Data;
    string[] formats = dataObject.GetFormats();
    var metadataDragObjectFormatIndex = Array.IndexOf(formats, typeof(MetadataDragObject).ToString());
    var dragObject = e.Data.GetData(formats[metadataDragObjectFormatIndex]);

    return dragObject as MetadataDragObject;
}

private void listBox1_DragOver(object sender, DragEventArgs e)
{
    var metadataDragObject = GetMetadataDragObject(e);
    if (metadataDragObject != null)
    {
        e.Effect = DragDropEffects.Link;
    }
}

private void listBox1_DragDrop(object sender, DragEventArgs e)
{
    var metadataDragObject = GetMetadataDragObject(e);
    if (metadataDragObject != null)
    {
        foreach (var metadataItem in metadataDragObject.MetadataDragged)
        {
            listBox1.Items.Add(metadataItem.NameFull);
        }
    }
}
private void listBox1_DragOver(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(DataSourceFieldList)))
    {
        e.Effect = DragDropEffects.Link;
    }
}

private void listBox1_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(typeof(DataSourceFieldList)))
    {
        e.Effect = DragDropEffects.Link;

	DataSourceFieldList fieldListControl = (DataSourceFieldList) e.Data.GetData(typeof(DataSourceFieldList));

        if (fieldListControl.SelectedItem != null)
        {
            if (((TextAndObject) fieldListControl.SelectedItem).obj != null)
            {
                MetadataField field = (MetadataField) ((TextAndObject) fieldListControl.SelectedItem).obj;

                int x = listBox1.Items.Add(field.NameStr + " [" + field.FieldTypeName + "]");

                listBox1.SelectedIndex = x;
            }
        }
    }
}
procedure TForm2.Memo1DragDrop(Sender, Source: TObject; X, Y: Integer);
var n:TacTreeNode;
        mo:TacMetadataObject;
begin
    Assert(Source=qb.MetadataTree);
    Assert(qb.MetadataTree.Selected<>nil);

    n:=qb.MetadataTree.Selected;
    mo:=TacMetadataObject(n.Data);
    Memo1.Lines.Add(mo.FullNameStr);
end;


procedure TForm2.Memo1DragOver(Sender, Source: TObject; X, Y: Integer;
    State: TDragState; var Accept: Boolean);
begin
    Accept:=false;
    if (Source=qb.MetadataTree)and
        (qb.MetadataTree.Selected<>nil)
    then Accept:=true;
end;
jTextArea1.setDropTarget(new DropTarget(this, new DropTargetAdapter()
{
    @Override
    public void dragOver(DropTargetDragEvent event)
    {
        if (!event.getTransferable().isDataFlavorSupported(DataFlavor.stringFlavor))
        {
            event.rejectDrag();
        }
    }

    @Override
    public void drop(DropTargetDropEvent event)
    {
        event.acceptDrop(DnDConstants.ACTION_LINK);

        Transferable transferable = event.getTransferable();

        if (transferable.isDataFlavorSupported(DataFlavor.stringFlavor))
        {
            try
            {
                String data = (String) transferable.getTransferData(DataFlavor.stringFlavor);
                jTextArea1.setText(data);
            }
            catch (UnsupportedFlavorException ex)
            {
                // handle exception
            }
            catch (IOException ex)
            {
                // handle exception
            }
        }
    }
}));

Is this article helpful for you?