Avatar of quentinA
quentinAFlag for United Kingdom of Great Britain and Northern Ireland

asked on 

Drag and Drop syntax

hi,
I.m trying to understand Drag and Drop, and so studying someones code.
I have a question about the following code:

//////////////////////////////

     private void DragStarted(ItemsControl itemsControl)
        {        
       ........

            DataObject dObject = new DataObject(ItemType, _data);
           
            DragDropEffects e =  DragDrop.DoDragDrop(itemsControl, dObject, DragDropEffects.Copy | DragDropEffects.Move);
         
            if ((e & DragDropEffects.Move) != 0)
            {
                .........
            }

////////////////////////////////

can someone explain why:
 ((e & DragDropEffects.Move) != 0) is used

and not just

 (e  != 0)





         
C#.NET Programming

Avatar of undefined
Last Comment
quentinA
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

The & checks the BITS in the variable e.

eg.  If e is 3 (11 in binary) and the DrapDropEffects.Move is 4 (100 in binary) then the check used is false but e is not zero.
Avatar of starlite551
starlite551
Flag of India image

Try looking at this demo file : Drag.zip
Avatar of starlite551
starlite551
Flag of India image

These links also might help you :
http://msdn.microsoft.com/en-us/library/aa984430%28v=vs.71%29.aspx
http://codehill.com/2009/06/drag-and-drop-files-to-a-c-application/

 
// Form load event or a similar place
private void Form_Load(object sender, EventArgs e)
{
    // Enable drag and drop for this form
    // (this can also be applied to any controls)
    this.AllowDrop = true;
 
    // Add event handlers for the drag & drop functionality
    this.DragEnter += new DragEventHandler(Form_DragEnter);
    this.DragDrop += new DragEventHandler(Form_DragDrop);
}
 
// This event occurs when the user drags over the form with 
// the mouse during a drag drop operation 
void Form_DragEnter(object sender, DragEventArgs e)
{
    // Check if the Dataformat of the data can be accepted
    // (we only accept file drops from Explorer, etc.)
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        e.Effect = DragDropEffects.Copy; // Okay
    else
        e.Effect = DragDropEffects.None; // Unknown data, ignore it
 
}
 
// Occurs when the user releases the mouse over the drop target 
void Form_DragDrop(object sender, DragEventArgs e)
{
    // Extract the data from the DataObject-Container into a string list
    string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
 
    // Do something with the data...
 
    // For example add all files into a simple label control:
    foreach (string File in FileList)
        this.label.Text += File + "\n";
}

Open in new window

Avatar of starlite551
starlite551
Flag of India image

Also a simple example :  User generated image
using System;
using System.Windows.Forms;

public class TextBoxDragDropDemo : Form
{
    public TextBoxDragDropDemo()
    {
        InitializeComponent();
    }

    private void TextBox_MouseDown(object sender, MouseEventArgs e)
    {
        TextBox txt = (TextBox)sender;
        txt.SelectAll();
        txt.DoDragDrop(txt.Text, DragDropEffects.Copy);
    }

    private void TextBox_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.Text))
        {
            e.Effect = DragDropEffects.Copy;
        }
        else
        {
            e.Effect = DragDropEffects.None;
        }
    }

    private void TextBox_DragDrop(object sender, DragEventArgs e)
    {
        TextBox txt = (TextBox)sender;
        txt.Text = (string)e.Data.GetData(DataFormats.Text);
    }

    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new TextBoxDragDropDemo());
    }
    private System.Windows.Forms.TextBox TextBox2;
    private System.Windows.Forms.TextBox TextBox1;

    private void InitializeComponent()
    {
        this.TextBox2 = new System.Windows.Forms.TextBox();
        this.TextBox1 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();

        this.TextBox2.AllowDrop = true;
        this.TextBox2.Location = new System.Drawing.Point(28, 129);
        this.TextBox2.Multiline = true;
        this.TextBox2.Size = new System.Drawing.Size(196, 77);
        this.TextBox2.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop);
        this.TextBox2.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter);
        this.TextBox2.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown);

        this.TextBox1.AllowDrop = true;
        this.TextBox1.Location = new System.Drawing.Point(28, 36);
        this.TextBox1.Multiline = true;
        this.TextBox1.Size = new System.Drawing.Size(196, 77);
        this.TextBox1.DragDrop += new System.Windows.Forms.DragEventHandler(this.TextBox_DragDrop);
        this.TextBox1.DragEnter += new System.Windows.Forms.DragEventHandler(this.TextBox_DragEnter);
        this.TextBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.TextBox_MouseDown);

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 266);
        this.Controls.Add(this.TextBox2);
        this.Controls.Add(this.TextBox1);
        this.ResumeLayout(false);
        this.PerformLayout();

    }
}

Open in new window

Avatar of starlite551
starlite551
Flag of India image

Actually  ((e & DragDropEffects.Move) != 0) is used instead of just (e!=0) because look at the previous line of code :
DragDropEffects e =  DragDrop.DoDragDrop(itemsControl, dObject, DragDropEffects.Copy | DragDropEffects.Move);

e is of the Type DragDropEffects now the types possible can be Copy, Move etc.. So in the line above it was actually OR operation.. used so only either of the two Copy or Move would be applied while Starting the Drag Operation..

Whereas  if you add the line ((e & DragDropEffects.Move) != 0) It implies that during Drag Start Operation both Copy as well as Move Operations Must be considered.
Avatar of quentinA
quentinA
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

hi starlite551,

is there some reason why:

if ((e & DragDropEffects.Move) != 0) { ... }

is preferrable to:

if (e == DragDropEffect.Move){ ... }

Avatar of systan
systan
Flag of Philippines image

e is a DragDropEffects variable used to assign DragDrop.DoDragDrop an object/items from ItemControl

So, if mouse Drag, then DropEffect is move, NOT copy.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Nope - as my first comment says things are stored bitwise.  You need to check the bits with a mask to find if it is a copy OR a move
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

if (e == DragDropEffect.Move)

consider
e is (binary) 101 and move is binary 100 but copy is 001
Avatar of systan
systan
Flag of Philippines image

e is a DragDropEffects variable used to assign DragDrop.DoDragDrop an object/items from ItemControl
So, if mouse Drag, then DropEffect is move, NOT Copy.

DragDropEffects e =  DragDrop.DoDragDrop(itemsControl, dObject, DragDropEffects.Copy | DragDropEffects.Move);
it means the item is Ready for Drag and Drop,   Drop either to Copy the item or Move


 ((e & DragDropEffects.Move) != 0) is used
Here's a pseudo code,
if ( (itemReady_was_mouseDrag) AND (the procedure is to Move)  NOT EQUAL to problem )
it means if item is Ready for mouse drag AND the procedure is to Move then Move the Item, NOT Copy.

and not just
 (e  != 0)
Because, there is no point on that,  it does not know if the item when Drop either to Move or Copy, So you must specify the Drag Effect.

I hope you understand that clearly.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

If things are stored bitwise then you MUST check the individual bits otherwise somethnig will go wrong sometime.

Consider:
Three friends, A, B and C.  All love eating apples but A also likes other fruit.
A meets B and C and says he has just been shopping but there was only one of each sort of fruit left in the shop.  If the others can say  what he has then he will let them have that fruit.
B (who only understands equals but not bits) says if the contents of your bag is an apple then give it me.
A says no luck.
C (who understands bits) says if your bag contains an apple then give it to me.
A gives him an apple.
B objects - I asked the same.
A says, no you didn't and takes a banana out of the bag.  You said if it was ONLY an apple in the bag.
Avatar of quentinA
quentinA
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I think what you are saying is:

that because of the Bitwise OR in -

DragDropEffects e =  DragDrop.DoDragDrop(itemsControl, dObject, DragDropEffects.Copy | DragDropEffects.Move);

The bit value of what is allowed by the source is: 0011.

And that the masking check using the Bitwise AND is saying " has a MOVE happened, and is one of the allowed effects on the source a MOVE?"

if ((e & DragDropEffects.Move) != 0)  [produces 0010]

Is that correct?
Or am i completely off the ballpark?
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of quentinA
quentinA
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

thanks everyone
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo