Drag and Drop syntax

quentinA
quentinA used Ask the Experts™
on
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)





         
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
AndyAinscowFreelance programmer / Consultant

Commented:
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.
Try looking at this demo file : Drag.zip
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

Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Also a simple example :  Drag N Drop Example Pic
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

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.

Author

Commented:
hi starlite551,

is there some reason why:

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

is preferrable to:

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

Commented:
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.
AndyAinscowFreelance programmer / Consultant

Commented:
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
AndyAinscowFreelance programmer / Consultant

Commented:
if (e == DragDropEffect.Move)

consider
e is (binary) 101 and move is binary 100 but copy is 001

Commented:
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.
AndyAinscowFreelance programmer / Consultant

Commented:
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.

Author

Commented:
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?
Freelance programmer / Consultant
Commented:
quentinA
You are pretty close.  As I have been saying all along the information is stored in individual bits inside an integral number.  You need to check the bits to determine what action is requested not the complete number.  Checking the complete number will only work if no other bits are set - and that can be a very dangerous assumption.  You might be lucky but sometime it will fail and the bug will be hard to track down.  You will probably find that a copy is a 'move' plus an extra bit that is set.

Your boss (I suspect) wouldn't like it if you supplied code along with a comment that you deliberately designed it so it would only work occasionally.

Author

Commented:
thanks everyone

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial