please help, drag and drop file to listbox

yyonaim

Newcomer
Joined
Apr 19, 2003
Messages
4
i just installed vb.net,

i want to drag a file from windows explorer or the desktop, to a list box on my program. then adding that file name to the listbox, obviously.

PLEASE HELP A NEWBIE OUT

thank you, peace
 
Hi there...
First, set the allowdrop property of the listbox = true
then in the listbox's dragover and dragdrop events:
C#:
private void listBox1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
	//already validated the format in the dragover event, no need to check again...		
	//the data type will be a string array, because you could drop multipal files
	string[] file = (System.String[])e.Data.GetData(DataFormats.FileDrop,true);
	//add the files
	foreach(string s in file)
	{
		listBox1.Items.Add(s);
	}
}

private void listBox1_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
	//inform the user that he can drop the data in the listbox, if file/s are dropped
	if(e.Data.GetDataPresent(DataFormats.FileDrop))
		e.Effect = DragDropEffects.Copy; 
}
this should do it...
Cheers!
 
Back
Top