Drag'n'drop to a Windows form issue - c#

I have, what should be, a simple question on drag'n'drop. I have a fresh Win Form project where the form has set to allow drops using AllowDrop = true. Should also mention that I am running Windows 7 64-bit.
Just to be sure, I have subscribed to
this.DragDrop += new System.Windows.Forms.DragEventHandler(Form1_DragDrop);
as well.
But when I run the app and drag anything from my desktop or explorer, it indicates with the mouse pointer icon that I am not allowed to drop any file(s) to it after all.
I found a a similar question like this one (but Win Vista) where the issue was that Visual Studio was running with admin priveleges which windows explorer wasn't. But building the app and running the executable results in the same problem.
I have done this many times in the past and couldn't Google my way to solve this one. What am I missing?

By default the target drop effect of a drag-and-drop operation is not specified (DragDropEffects.None). Thus there is no drop event for your control in this case.
To allow Control to be a drag-and-drop operation's receiver for the specific data you should specify the concrete DardDropEffect as shown below (use the DragEnter or DragOver events):
void Form1_DragDrop(object sender, DragEventArgs e) {
object data = e.Data.GetData(DataFormats.FileDrop);
}
void Form1_DragEnter(object sender, DragEventArgs e) {
if(e.Data.GetDataPresent(DataFormats.FileDrop)) {
e.Effect = DragDropEffects.Copy;
}
}
Related MSDN article: Performing a Drag-and-Drop Operation in Windows Forms

You are using the wrong Event, use the DragEnter Event.
this.DragEnter += new System.Windows.Forms.DragEventHandler(Form1_DragDrop);

Related

Textbox_Enter event not does nothing

I want to be able to tell when the user cliked on a textbox and is in "edit" mode. Everywhere I look I see the textbox_Enter and textbox_Leave events being used with the instructions within that and it works fine. For me however it doesn't do anything. I tried elimination as many outside factors as possible, including creating a brand new project just for testing purposes and copied some code samples yet again nothing happens when I click on the textbox. I'm using Visual Studio 2017 on Windows 10 with Visual C# Application Windows Form (.NET Framework)
Also here's a sample of the code I try to use if it helps for whatever reason
private void textbox_Enter(object sender, ControlEventArgs e)
{
label.Text = "ok";
}
First of all the type of the e parameter is not correct: It must be EventArgs, not ControlEventArgs:
private void textbox_Enter(object sender, EventArgs e)
{
// Do something
}
Second, you need to register the event in the forms designer with the textbox control in the properties window:
You need to wire up this method to the textbox enter event. Select the control and then look at the events section in the properties tab.

WPF DragDrop: Window.ShowModal() hangs Windows Explorer

I have written code to allow dropping a file from Windows Explorer into a WPF application. In my drop event handler I launch a window which displays information about the dropped file. If I create this window using Window.ShowModally() then Windows Explorer will hang/freeze until the window in my app is closed. However, if I create the window using Window.Show() this problem does not occur. Unfortunately, I require this window to be shown modally.
Presumably the Windows Explorer thread is locked because it detects that one of its files is being used. Is there a way to inform Windows Explorer that it does not need to wait for the window in my application to close? I have tried setting the DragDropEventArgs.Handled to true but this does not resolve the problem.
I no longer require the DragDrop once I have extracted the data from it so if there is a way to cancel or end the DragDrop in my Drop event handler then this would be an acceptable solution.
You cannot block in any of your drag+drop event handlers, that will hang the D+D plumbing and a dead source window is the expected outcome.
There's a simple workaround, just ask the dispatcher to run your code later, after the event completes. Elegantly done with its BeginInvoke() method. Roughly:
private void Window_Drop(object sender, DragEventArgs e) {
// Do something with dropped object
//...
this.Dispatcher.BeginInvoke(new Action(() => {
var dlg = new DialogWindow();
dlg.Owner = this;
var result = dlg.ShowDialog();
// etc..
}));
}

Minimize Event ( C# Win 8.1 app)

I'm developing an App for windows 8.1 and i need to execute a method to pause some tasks or play a sound when the app is minimized in the sidebar.
I tried:
Application.Current.Suspending += new SuspendingEventHandler(Method_Name);
private void Method_Name(object sender, object e)
{
Other_Method();
}
But this event takes a few seconds (5~10) to occur after I minimize the app.
What event occurs when the app is dragged to the sidebar? What process sends the event?
Thanks.
Check out this post for the answer. It's WindowSizeChanged and check the value of ApplicationView.Value.
[EDIT]
Apparently for 8.1 things have changed a bit. The ApplicationView stuff is deprecated (that was quick) but this still takes place in SizeChanged for the window. Check out this for more details.
After long searching I found something that is not exactly what i wanted, but it works.
This event occurs everytime that visibility of the page changes (Is started, is maximized or minimized for exemple), then you must do some conditions using the if operator.
Window.Current.VisibilityChanged += new WindowVisibilityChangedEventHandler(One_Method);
private void One_Method(object sender, Windows.UI.Core.VisibilityChangedEventArgs e)
{
if(Some_Condition)
{
//TODO: Code that you want execute.
}
}
I'll keep the answer in open for the case of someone knows something more efficient.

C# WinForms DragEnter never fires

I'm puzzled with this. I attempted implementing drag and drop on a DataGridView. Failing to see any events fired, I tried a simple form, with a text box.
I would like to be able to drag files or folders from Windows Explorer.
I'm missing something because these events never fire. I did read about DragEvents, Windows 7 and UIPI but I still couldn't get around this.
I'm out of ideas and I welcome your suggestions.
public Form1()
{
InitializeComponent();
this.AllowDrop = true;
textBox1.AllowDrop = true;
textBox1.DragEnter += new DragEventHandler(textBox1_DragEnter);
textBox1.DragDrop += new DragEventHandler(textBox1_DragDrop);
textBox1.DragOver += new DragEventHandler(textBox1_DragOver);
}
void textBox1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
void textBox1_DragDrop(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
void textBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
It seems that this should work. I have a clean install on WP7 64 - with all updates, I don't have virus or malware protection running, or anything (to my knowledge) which could prevent these events firing.
I had the same issue. it was only because I was debugging from a "run as administrator" session. I think that since VISTA there is a security that prevents from dropping to a privileged application.
I found that while I was running my Forms application in debug mode from Visual Studio, it didn't work. Only when I ran it outside of VS does it work perfectly. Presumably this is also something to do with security on Windows 7 (and possibly later versions).

Is it possible to change mouse cursor when handling drag (from DragOver event)? [duplicate]

This question already has answers here:
Can I choose a custom image for C# Windows Application Drag Drop functions?
(2 answers)
Closed 9 years ago.
We need to display a feedback to the user when he drags-in items into our application.
Our client prefers this feedback to be in form of a custom cursor.
This is already implemented for drag-out, using a custom cursor that is set in GiveFeedback event handler (raised by DoDragDrop when dragging items out of our app). The GiveFeedbackEventArgs allows us to specify UseDefaultCursors property - setting this to false allows us to override the cursor.
However, the DragOver event handler argument, which is the equivalent of GiveFeedback, does not have UseDefaultCursors property and changing the cursor from there does not have any effect.
Sample (this has no effect):
private void browser_DragOver(object sender, DragEventArgs e) {
Cursor.Current = Cursors.WaitCursor;
}
The drag operation originates from outside our app. (for in-app drag, it works using the GiveFeedback event.
How to change the cursor when receiving a drag? Is this even possible/feasible?
void Form1_GiveFeedback(object sender, GiveFeedbackEventArgs e) {
e.UseDefaultCursors = false;
}
Yes, you must implement a COM interfaces (IDragSourceHelper and IDropTargetHelper).
Take a look HERE.
Would this suffice?
This code will change the mouse pointer by adding a [+] sign next to it.
private void Form_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
We've addressed a whole range of drag-drop issues here:
Drag and Drop between Instances of the same Windows Forms Application
Hopefully this will point you in the right direction.

Categories

Resources