Goal: drag-and-drop from one button to another button and have access to the following info from the original button:

- Image
- BackColor
- Tag

Problem: I can only implement one. If I try to implement all three I get null pointers.

private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            string buttonTag = button1.Tag.ToString();
            button1.DoDragDrop(buttonTag, DragDropEffects.Copy);
            //button1.DoDragDrop(button1.Image, DragDropEffects.Copy);
        }

        private void button2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            e.Effect = DragDropEffects.None;
            //button2.Image = (Bitmap)e.Data.GetData(typeof(Bitmap));
            button2.Tag = (String)(e.Data.GetData(typeof(String)));
        }

        private void button2_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {            
            e.Effect = DragDropEffects.Copy;
        }

The above code correctly lets me copy the Tag info. Easy check: button2.Text = button2.Tag.ToString() works correctly.

However, if I uncomment the command to copy the image in addition to the Tag, then I get a null pointer for the Tag, specifically triggered when I try to invoke ToString().

On the other hand, if I implement only the Image copy and instead try to access the Tag info through something like:

private void button2_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            e.Effect = DragDropEffects.None;
            button2.Image = (Bitmap)e.Data.GetData(typeof(Bitmap));
            (Button) sendButton = (Button)sender
            button2.Tag = sendButton.Tag;
            button2.Text = button2.Tag.ToString();
        }

Then I get a null pointer, i.e., I do not have access to the Tag data from sender. The last line button2.Text = button2.Tag.ToString() is just a test to see if the data was passed correctly.

Similar code for button1.BackColor -> button2.BackColor seem to be completely ignored as well.

Well, that's because you can only send one "piece" of data through when you start the drag. May be a bit of overkill, but can you do this in "button1_MouseDown":

button1.DoDragDrop(button1, DragDropEffects.Copy);

Then, in "button2_DragDrop":

Button oButton = (Button)(e.Data.GetData(typeof(Button)));

Now, you have a reference to button1, and all of the props within it.

Thank you very much Mike, your suggestion was very helpful and has solved my problem.

Even though there is the extra overhead with all of the button's properties being available than just the ones strictly of immediate interest this approach gets the job done and provides some flexibility should I need access to the other properties in the future.

But this leaves me wondering why the same information is not accessible by typecasting "object sender" as Button? For example, you can typecast sender and retrieve the original button's name, but other properties seem to be off-limits.

This question has already been answered. Start a new discussion instead.