PC Review


Reply
Thread Tools Rate Thread

DragDrop/DragEnter event

 
 
Tony Johansson
Guest
Posts: n/a
 
      20th Apr 2009
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


 
Reply With Quote
 
 
 
 
Tony Johansson
Guest
Posts: n/a
 
      21st Apr 2009
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


"Peter Duniho" <(E-Mail Removed)> skrev i meddelandet
newsp.us****(E-Mail Removed)...
> On Mon, 20 Apr 2009 15:23:01 -0700, Tony Johansson
> <(E-Mail Removed)> wrote:
>
>> 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.

>
> The docs say:
>
> -- DragEnter: "Occurs when an object is dragged into the control's
> bounds."
> -- DragDrop: "Occurs when a drag-and-drop operation is completed."
>
> You should handle both events to provide proper user feedback. Hopefully
> from the documentation it is obvious which one signals your code to
> actually complete the operation.
>
> Pete



 
Reply With Quote
 
Rob Whiteside
Guest
Posts: n/a
 
      21st Apr 2009
On Apr 20, 4:19*pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
> 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
>
> "Peter Duniho" <NpOeStPe...@nnowslpianmk.com> skrev i meddelandetnewsp.us****(E-Mail Removed)...
>
> > On Mon, 20 Apr 2009 15:23:01 -0700, Tony Johansson
> > <johansson.anders...@telia.com> wrote:

>
> >> 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.

>
> > The docs say:

>
> > * * -- DragEnter: "Occurs when an object is dragged into the control's
> > bounds."
> > * * -- DragDrop: "Occurs when a drag-and-drop operation is completed."

>
> > You should handle both events to provide proper user feedback. *Hopefully
> > from the documentation it is obvious which one signals your code to
> > actually complete the operation.

>
> > Pete


Make sure you've set the "AllowDrop" property on your control to "true"
 
Reply With Quote
 
Jeff Johnson
Guest
Posts: n/a
 
      21st Apr 2009
"Rob Whiteside" <(E-Mail Removed)> wrote in message
news:d8868a7b-0f5c-439d-9726-(E-Mail Removed)...

>> 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.


> 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);
}
}


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DragDrop event Jeff Williams Microsoft C# .NET 1 28th Mar 2007 03:35 PM
Where does DragDrop event come from??? Dan Microsoft C# .NET 3 30th Jul 2005 12:53 AM
Calling Web Service in DragDrop event causes event to be fired multipletimes Vincent Mouton Microsoft Dot NET Framework Forms 0 18th Mar 2005 07:28 PM
dragenter event propagate up Lance Johnson Microsoft Dot NET Framework Forms 4 25th Mar 2004 12:18 PM
DragDrop event Michael Hutchinson Microsoft Dot NET Framework Forms 2 5th Feb 2004 06:31 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:04 PM.