Hi i'm pretty new to c++, but i've been programming in java for a while, i've tried to do a simple drag and drop program that when a file is dropped in the main screen it gets the file path and write it in a TextBox, but i doesn't work, when i drag a file from windows to the main window an stop icon appears and i can't proceed. I've set drop enable and all the rest.

// 
			// MainScreen
			// 
			this->AllowDrop = true;
			resources->ApplyResources(this, L"$this");
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->Controls->Add(this->btn_search);
			this->Controls->Add(this->txt_fileURL);
			this->Controls->Add(this->btn_delete);
			this->HelpButton = true;
			this->Name = L"MainScreen";
			this->Load += gcnew System::EventHandler(this, &MainScreen::MainScreen_Load);
			this->DragDrop += gcnew System::Windows::Forms::DragEventHandler(this, &MainScreen::MainScreen_DragDrop);
			this->DragEnter += gcnew System::Windows::Forms::DragEventHandler(this, &MainScreen::MainScreen_DragEnter);
			this->DragOver += gcnew System::Windows::Forms::DragEventHandler(this, &MainScreen::MainScreen_DragOver);
			this->ResumeLayout(false);
			this->PerformLayout();

What does the code look like in MainScreen_DragDrop, MainScreen_DragEnter, and MainScreen_DragOver?

What does the code look like in MainScreen_DragDrop, MainScreen_DragEnter, and MainScreen_DragOver?

I've done this right now, and the only method responding is :

private: System::Void MainScreen_DragOver(System::Object^  sender, System::Windows::Forms::DragEventArgs^  e) {
			 txt_fileURL->Text = "Dragged over";
		 }

The rest:

private: System::Void MainScreen_DragDrop(System::Object^  sender, System::Windows::Forms::DragEventArgs^  e) {
               txt_fileURL->Text = "Dragged and dropped";
		 }
private: System::Void MainScreen_DragEnter(System::Object^  sender, System::Windows::Forms::DragEventArgs^  e) {
			 txt_fileURL->Text = "Drag entered";
		 }
private: System::Void MainScreen_DragOver(System::Object^  sender, System::Windows::Forms::DragEventArgs^  e) {
			 txt_fileURL->Text = "Dragged over";
		 }

I was able to get the drag entered and drag over to work (but not at the same time). I found this article helpful http://www.codeproject.com/KB/mcpp/DirectoryList.aspx.
I got the following line of code from that article, but I don't know if it's the right idea if(e->Data->GetDataPresent(DataFormats::FileDrop)) //do whatever One thing you want to check is whether you have the "AllowDrop" property (select the form and go to the properties window on the right) set to True.

I got it working, the key part of the code is :

private: System::Void MainScreen_DragEnter(System::Object^  sender, System::Windows::Forms::DragEventArgs^  e) {
			 if(e->Data->GetDataPresent(DataFormats::FileDrop))
				e->Effect = DragDropEffects::All;
		     else
				e->Effect = DragDropEffects::None;
		 }

setting the effects to all, otherwise the "no" sign will keep showing...i'll post the part where i grab the path of the file and put it on the textbox in case anyone wants to know:

private: System::Void MainScreen_DragDrop(System::Object^  sender, System::Windows::Forms::DragEventArgs^  e) {
              array<String^> ^files = (array<String^>^) e->Data->GetData( DataFormats::FileDrop );
			  txt_fileURL->Text = files[0];
		 }

Thanks.

An interesting bug I encountered for drag and drop in forms in c++, if you didn't include

[STAThread]

The drag and drop callbacks wouldn't get call.

This article has been dead for over six months. Start a new discussion instead.