Jump to content
  • Advertisement
Sign in to follow this  
chrisliando

[.net] How to drag and drop text file from Windows Explorer to a textBox?

This topic is 3971 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to drag and drop text file from Windows Explorer to a textBox in my application.. But with my code below, there is nothing happened when I drag a text file to textBox in my application. Here is my code and also I cannot debug it..How to debug it? Thank you very much.. private: System::Void textBox2_DragDrop(System:Object^ sender, System::Windows::Forms:DragEventArgs^ e) { // if (e->Data->GetDataPresent(DataFormats::FileDrop)) { ArrayList ^filenames = gcnew ArrayList; filenames->Add(e->Data->GetData(DataFormats::FileDrop)); if (filenames != nullptr) { //TODO: enumerate all elements //TODO: Check file type textBox2->Text = System::IO::File::ReadAllText(filenames[0]->ToString()); } } // } private: System::Void textBox2_DragEnter(System:Object^ sender, System::Windows::Forms:DragEventArgs^ e) { if (e->Data->GetDataPresent(DataFormats::FileDrop)) { ArrayList ^filenames = gcnew ArrayList; filenames->Add(e->Data->GetData(DataFormats::FileDrop)); if (filenames != nullptr) { e->Effect = DragDropEffects::Copy; } else { e->Effect = DragDropEffects::None; } } else { e->Effect = DragDropEffects::None; } }

Share this post


Link to post
Share on other sites
Advertisement
Very easy in C#:


private void textBox1_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

// We're only concered with the first one.
string file = files[0];

FileStream fs = new FileStream(file, FileMode.Open);
StreamReader sr = new StreamReader(fs);

textBox1.Text = sr.ReadToEnd();

sr.Close();
fs.Close();

}

private void textBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}

Share this post


Link to post
Share on other sites
In C++ there is no type like this:
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

What should I change it? to ArrayList? or what?
what I know if array is like this:
array<String^>^ str = gcnew array<String^>;

is it the same with what you gave me?

Thank u..

Share this post


Link to post
Share on other sites
I've convert your code to C++ below. It was successfully compiled.

It should have worked I guess, the only thing that made it fail was maybe the value of variable file which get the value from line:

files[0]->ToString();

the files[0]->ToString() value was System.String[]

Here is my code in C++ : (I've tried to use ArrayList, array but the result is the same so that the next command which is FileStream and StreamReader cannot process it).


How to fix it?


Thank you very much..



Here is the code:


private: System::Void textBox1_DragDrop(System::Object^ sender, System::Windows::Forms::DragEventArgs^ e) {
ArrayList ^files = gcnew ArrayList;
files->Add(e->Data->GetData(DataFormats::FileDrop));
MessageBox::Show(files[0]->ToString());
System::IO::FileStream ^fs = gcnew FileStream(files[0]->ToString() , System::IO::FileMode::Open);
System::IO::StreamReader ^sr = gcnew StreamReader(fs);

textBox1->Text = sr->ReadToEnd();

sr->Close();
fs->Close();

} //end of textBox1_DragDrop

private: System::Void textBox1_DragEnter(System::Object^ sender, System::Windows::Forms::DragEventArgs^ e) {
e->Effect = DragDropEffects::Copy;
} //end of textBox1_DragEnter



Share this post


Link to post
Share on other sites
OMG, having to do crap like this makes me want to blow my brains out! [sad] C# is soooo much nicer.


private: System::Void textBox1_DragDrop(System::Object^ sender, System::Windows::Forms::DragEventArgs^ e)
{
array<String^>^ files = (array<String^>^)e->Data->GetData(DataFormats::FileDrop);

String ^file = files[0];

FileStream ^fs = gcnew FileStream(file,FileMode::Open);
StreamReader ^sr = gcnew StreamReader(fs);

this->textBox1->Text = sr->ReadToEnd();

sr->Close();
fs->Close();

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!