DragDrop/DragEnter event

T

Tony Johansson

Hello!

If I want to drag a file into a TextBox and the contents of the file is to
be displayed in the TextBox which event is most appropriate between
DragEnter or DragDrop.

//Tony
 
T

Tony Johansson

Hello!

This DragEnter event works fine but I can't get the DragDrop event to occur.
I have one eventhandler method for the DragDrop event and
one eventhandler method for the DragEnter event.

When I drag a file from the DeskTop into the TextBox the DragEnter event is
fired so my DragEnter eventhandler
method is called which works fine.
I have read the docs but I can't understand when the DragDrop event is fired
because my eventhandler for DragDrop event is never called.

//Tony
 
R

Rob Whiteside

Hello!

This DragEnter event works fine but I can't get the DragDrop event to occur.
I have one eventhandler method for the DragDrop event and
one eventhandler method for the DragEnter event.

When I drag a file from the DeskTop into the TextBox the DragEnter event is
fired so my DragEnter eventhandler
method is called which works fine.
I have read the docs but I can't understand when the DragDrop event is fired
because my eventhandler for DragDrop event is never called.

//Tony

Make sure you've set the "AllowDrop" property on your control to "true"
 
J

Jeff Johnson

Make sure you've set the "AllowDrop" property on your control to "true"

I doubt that's the issue. The AllowDrop property is badly named. It really
should be AllowDragDropEvents. MSDN describes it thusly: "true if
drag-and-drop operations are allowed in the control; otherwise, false. The
default is false."

The fact that he's getting the DragEnter event suggests that he has set this
value to true already.

Tony, search your entire project for the name of your DragDrop handler. In
other words, if the method is named textbox1_DragDrop then search for that,
using Entire Project as the scope. Make sure that in the .Designer.cs class
(I assume you're using VS 2005 or later) that there is a line like this:

this.textbox1.DragDrop += new
System.Windows.Forms.DragEventHandler(textbox1_DragDrop);

(Of course, you could use the Properties window and check to see if this
text box has a value for the DragDrop event as well, but checking the code
rules out any possibility of IDE screwiness.)

I wrote a really simple program a while back that lets you drag and drop
files onto it and it extracts their file names. The DragEnter and DragDrop
methods looked like this (I put them in the form so that the file could be
dropped anywhere, but this could easily have been applied to just the text
box):

private void MainForm_DragEnter(object sender, DragEventArgs e)
{
foreach (string f in e.Data.GetFormats(false))
{
if (f == DataFormats.FileDrop)
{
e.Effect = DragDropEffects.Copy;
return;
}
}

e.Effect = DragDropEffects.None;
}

private void MainForm_DragDrop(object sender, DragEventArgs e)
{
StringBuilder listBuilder = new StringBuilder();

foreach (string f in e.Data.GetFormats())
{
if (f == DataFormats.FileDrop)
{
foreach (string file in
(string[])e.Data.GetData(DataFormats.FileDrop, false))
{
string fileName = file;

if (!includePathCheckBox.Checked)
{
fileName = Path.GetFileName(fileName);
}

// If the user drops a drive and Include Path is not checked,
there
// will be no file name, so don't add it
if (fileName.Length > 0)
{
listBuilder.AppendLine(fileName);
}
}
}
}

string list = listBuilder.ToString();

if (list.Length > 0)
{
if (clearListCheckBox.Checked)
{
fileListTextBox.Text = list;
}
else
{
fileListTextBox.Text += list;
}
}
else
{
MessageBox.Show("No files were found in the dropped items.", "No
Files",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
 

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