Drag and Drop / What object type?

M

MFRASER

How can I tell what object type is dropped onto my control? Here is a
snippet of code.

private void lvwTasks_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)

{

//I want to know if e.Data is of type MyClass1 or MyClass2

Core.MyClass1 aTask = new MyClass1 ();

aTask = (Core.VariableMapping.MapXMLTask)e.Data.GetData(aTask.GetType());



Core.MyClass2 aTask2 = new MyClass2 ();

aTask2 = (Core.VariableMapping.MapXMLTask)e.Data.GetData(aTask2 .GetType());

}
 
A

APG

MFRASER said:
How can I tell what object type is dropped onto my control? Here is a
snippet of code.

private void lvwTasks_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)

{

//I want to know if e.Data is of type MyClass1 or MyClass2

Core.MyClass1 aTask = new MyClass1 ();

aTask = (Core.VariableMapping.MapXMLTask)e.Data.GetData(aTask.GetType());



Core.MyClass2 aTask2 = new MyClass2 ();

aTask2 = (Core.VariableMapping.MapXMLTask)e.Data.GetData(aTask2 .GetType());

}
Hi,

You check for null after the e.Data.GetData(). If it is null, the
dropped item is not of that type.

Like:

private void tabPage1_DragDrop(object sender,
System.Windows.Forms.DragEventArgs e)
{
Panel pnl =e.Data.GetData(typeof(Panel)) as Panel;
if(pnl == null)
{
//Dropped item is not a panel
}
else
{
//Dropped item is a panel
//Do appropriate action
//Return
}
PictureBox picBox =e.Data.GetData(typeof(PictureBox)) as PictureBox;
if(picBox == null)
{
//Dropped item is not a picture box
//Do appropriate action here
}
}

HTH,
APG
 
F

Fraser Michael

Thank you. I ended up using the DataPresent Method to test the type of
the data.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top