Linked Questions

Popular Questions

DragDrop a UserControl to a different form

Asked by At

Basically, the situation is this: FormA has a UserControl (which contains 5 labels and a picbox) and is named "cardShowing".

private void cardShowing_MouseDown(object sender, MouseEventArgs e)
    DoDragDrop(cardShowing, DragDropEffects.Copy);

FormB is the target of the drag

private void HandForm_DragDrop(object sender, DragEventArgs e)
    e.Effect = DragDropEffects.Copy;
    CardControl card = (CardControl)e.Data.GetData(typeof(CardControl));
    card.BorderStyle = BorderStyle.FixedSingle;
    card.Location = new Point(cardControl3.Location.X + 187, cardControl1.Location.Y);
    //cardControl1 and 3 are other controls already present on FormB
    this.Controls.Add(card);//add the card to the controls colection

At the end, the result is FormA losing its control and FormB gains it (although effect is 'copy'), but the properties are turned to default - BorderStyle is originally FixedSingle, and even though I make sure this property is set to FixedSingle, it turns to 'None'. Same goes for the 'Location' property.

Now, what I'd like to do is not cut the control from its original form, and have its actual properties be transferred.

Edit I have found the problem being the line:

CardControl card = (CardControl)e.Data.GetData(typeof(CardControl));

and that is because it is always null. I have tried this with different classes that I wrote, not user controls, and it still gives null. Does anyone have a clue why this happens?

Related Questions