Tim Van Wassenhove

Passionate geek, interested in Technology. Proud father of two

19 Dec 2006

Drag and Drop Microsoft Office Outlook Contacts on your WinForm

Earlier today i saw someone that wanted to know how to drag and drop Microsoft Office Outlook Contacts on his winform (and get the data of the contact). Here are the few lines of code that do what he asked for

// in the constructor of the form (or in the InitializeComponent method if you set it via the Designer)
this.AllowDrop = true;

// handle the DragOver event
private void Form1_DragOver(object sender, DragEventArgs e)
{
	e.Effect = DragDropEffects.All;
}

// handle the DragDrop event
private void Form1_DragDrop(object sender, DragEventArgs e)
{
	string text = (string)e.Data.GetData("Text", true);
	this.label1.Text = text;

	// for more finegrained access to the data
	//string[] lines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
}

screenshot of outlook contact that was dragged and dropped on the form