I have a textbox on a winform in Visual Studio. I want to drag&drop files on it. This is what I have done:
public LangMerge()
{ InitializeComponent();
this.AllowDrop = true;
tbxFilepath.AllowDrop = true;
tbxFilepath.DragDrop += new DragEventHandler(tbxFilepath_DragDrop);
tbxFilepath.DragEnter += new DragEventHandler(tbxFilepath_DragEnter);
}
void tbxFilepath_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files) tbxFilepath.Text=(file);
}
void tbxFilepath_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}
Neither the drag effect nor the receiving of the file does not work. No error messages or warnings. What could be the problem?
UPDATE: I inserted breakpoints into the event handler methods and the code doesn't get into them in the first place, no matter what I do with the cursor.
You have set AllowDrop=true; and handled DragDrop() and DragEnter() events, however for winforms application you need to also handle TextBox.DragOver() event :
Something like this :
private void textBoxFile_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
Related
I'm having trouble loading a file into a RichText control.
First, there are no events in the designer to accomodate drag/drop, so I put these handlers in the form initialization.
Second, the DragEnter() event does not seem to identify text files properly, so dropping is disabled in the RichText (it works fine for a ListBox).
Third, if I get the RT to load the file, it displays an icon/filename inside the RT text area. I cant find any references to this behavior, and can't turn it off.
This code does NOT work:
public Form1()
{
InitializeComponent();
RT.AllowDrop = true;
RT.DragEnter += new System.Windows.Forms.DragEventHandler(RT_DragEnter);
RT.DragDrop += new System.Windows.Forms.DragEventHandler(RT_DragDrop);
}
private void RT_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) {
// when a text file is dragged to this control, GetDataPresent() returns FALSE.
// this seems to be regardless of the file extension. File is ASCII text only.
// this code DOES work if the underlying control is a ListBox.
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Move;
else
e.Effect = DragDropEffects.None;
}
private void RT_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop, false);
if (null != filenames){
// when the file is loaded, the RT also displays an icon for the file,
// with the filename, inside the text area ... ?
RT.LoadFile(filenames[0],RichTextBoxStreamType.PlainText);
}
}
In order to get the RT to load a dropped file properly, I use a timer to load the RT outside of the
DragDrop() event:
public string FileToLoad;
private void RT_DragEnter(object sender, System.Windows.Forms.DragEventArgs e) {
// allow anything:
//if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Move;
//else
//e.Effect = DragDropEffects.None;
}
private void RT_DragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
string[] filenames = (string[])e.Data.GetData(DataFormats.FileDrop, false);
if (null != filenames){
FileToLoad = filenames[0];
timer1.Enabled = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Enabled = false;
if (FileToLoad.Length > 0)
{
RT.LoadFile(FileToLoad, RichTextBoxStreamType.PlainText);
FileToLoad = "";
}
}
The latter has the RichText loading the file properly, but this seems like a long way around the block to get LoadFile() to work as expected. Am I doing something wrong?
I have one windows application in c#, i want to add drag & drop facility in this application, after adding this facility it takes multiple files, so I want to take only one file at a time, how to do this?
read the code below and try applying it to your situation
public Form1()
{
InitializeComponent();
this.AllowDrop = true;
this.DragEnter += Form1_DragEnter;
this.DragDrop += Form1_DragDrop;
}
void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
void Form1_DragDrop(object sender, DragEventArgs e)
{
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Length == 1)
{
// do what you want
}
else
{
// show error
}
}
How could I load files to a form by drag-and-drop?
Which event will appear?
Which component should I use?
And how to determine name of file and others properties after drag-and-dropping it to a form?
Thank you!
Code
private void panel1_DragEnter(object sender, DragEventsArgs e){
if (e.Data.GetDataPresent(DataFormats.Text)){
e.Effect = DragDropEffects.Move;
MessageBox.Show(e.Data.GetData(DataFormats.Text).toString());
}
if (e.Data.GetDataPresent(DataFormats.FileDrop)){
}
}
ok, this works.
How about files? How can I get filename and extension?
and this is only a dragEnter action.
This code will loop through and print the full names (including extensions) of all the files dragged into your window:
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string filePath in files)
{
Console.WriteLine(filePath);
}
}
Check the below link for more info
http://doitdotnet.wordpress.com/2011/12/18/drag-and-drop-files-into-winforms/
private void Form2_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop));
foreach (string fileLoc in filePaths)
// Code to read the contents of the text file
if (File.Exists(fileLoc))
using (TextReader tr = new StreamReader(fileLoc))
{
MessageBox.Show(tr.ReadToEnd());
}
}
}
You need work with 2 below Events, of course while your Control/Form AllowDrop property's is true.
private void Home_DragOver(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Link;
else
e.Effect = DragDropEffects.None;
}
private void Home_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
//YourOpenMethod(files);
}
}
Enjoy...
I can't seem to make this work using this:
private void listView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}
private void listView1_DragDrop(object sender, DragEventArgs e)
{
string[] directoryName = (string[])e.Data.GetData(DataFormats.FileDrop);
string[] files = Directory.GetFiles(directoryName[0]);
foreach (string file in files)
{
if (Path.GetExtension(file) == ".mp3")
{
listView1.Items.Add(file);
}
}
}
The mouse cursor shows a NOT sign and I can't drop the folder in my program.
Have you set the AllowDrop property of your ListView to True?
Is your DragEnter event ever being hit?
How do i drag files or folders into a textbox? i want to put the foldername in that very textbox. C# .NET
i wrote this code based in this link
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.AllowDrop = true;
textBox1.DragEnter += new DragEventHandler(textBox1_DragEnter);
textBox1.DragDrop += new DragEventHandler(textBox1_DragDrop);
}
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effects = DragDropEffects.Copy;
else
e.Effects = DragDropEffects.None;
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
string s="";
foreach (string File in FileList)
s = s+ " "+ File ;
textBox1.Text = s;
}
}
Set AllowDrop to true on your TextBox, and write the following code for the DragDrop and DragEnter events :
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
textBox1.Lines = fileNames;
}
}
CodeProject has a really nice example of doing this, including how to enable drag and drop both ways (from Explorer to your app, and from your app to Explorer).
Control has various events for dealing with drag/drop - you'll probably only need to look at the DragDrop event for what you want.
If you get the Error Messages below, This applied to me when using Visual Studio 2015, try
e.Effect instead of e.Effects
Severity Code Description Project File Line Suppression State
Error CS1061 'DragEventArgs' does not contain a definition for 'Effects' and no extension method 'Effects' accepting a first argument of type 'DragEventArgs' could be found (are you missing a using directive or an assembly reference?)