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.