I have a WinForms application where I allow users to drag and drop images onto a panel. For now, they need to drop two files at once to add them both. If they drop one and then they want to add another, it just overwrites the first file.
I want to allow them to drop one file at once, and add as many as they want without overwriting them.
private void Panel1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
string[] files;
private void Panel1_DragDrop(object sender, DragEventArgs e)
{
files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
Console.WriteLine(files.Length);
}
}
The reason it doesn't work is that you're throwing away whatever is in files every time DragDrop is raised. Instead of using an array, you should use List<string> (or HashSet<string> to ignore duplicates).
Here's an example:
List<string> files = new List<string>();
private void Panel1_DragDrop(object sender, DragEventArgs e)
{
files.AddRange((string[])e.Data.GetData(DataFormats.FileDrop));
Console.WriteLine(files.Count);
}
Or with HashSet:
HashSet<string> files = new HashSet<string>();
private void Panel1_DragDrop(object sender, DragEventArgs e)
{
files.UnionWith((string[])e.Data.GetData(DataFormats.FileDrop));
Console.WriteLine(files.Count);
}
Related
I failed to fill combobox with csv filenames. I created the combobox by dragging from toolbox in Microsoft Visual Studio. I set the name of combobox to ChooseSampleSheet.
The following is my code:
private void ChooseSampleSheet_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo d = new DirectoryInfo(#"C:\Users\UniFlow\Desktop\Europa-master\user interface\Europa design Y\Experiemnt_Gui");//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.csv"); //Getting Text files
ChooseSampleSheet.DataSource = Files;
ChooseSampleSheet.DisplayMember = "Name";
}
Also, I tried the following code:
private string path = (#"C:\Users\UniFlow\Desktop\Europa-master\user interface\Europa design Y\Experiemnt_Gui");
private void ChooseSampleSheet_SelectedIndexChanged(object sender, EventArgs e)
{
List<String> Configurations = Directory.EnumerateDirectories(path, "*.exe")
.Select(p => Path.GetFileName(p))
.ToList();
ChooseSampleSheet.DataSource = Configurations;
}
But Neither of them works. Nothing shows in my combobox. I expected to see csv file names. So that I can click to open selected file afterwards (not show in in my code).
People suggested me to change the event. The following is my update.
private void form4_load(object sender, EventArgs e)
{
DirectoryInfo d = new DirectoryInfo(#"C:\Users\UniFlow\Desktop\Europa-master\user interface\Europa design Y\Experiemnt_Gui");//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.csv"); //Getting Text files
ChooseSampleSheet.DataSource = Files;
ChooseSampleSheet.DisplayMember = "Name";
}
private void ChooseSampleSheet_SelectedIndexChanged(object sender, EventArgs e)
{
}
However, nothing show in the combobox still.
I do not see anything wrong in your code however I think your code is at the wrong place.
SelectedIndexChanged will get execute when you select something from the drop down. Since your drop down has not been filled with values , you can't fire that event.
Put the same code in form_load and you will see the values there.
DirectoryInfo d = new DirectoryInfo(#"C:\Users\UniFlow\Desktop\Europa-master\user interface\Europa design Y\Experiemnt_Gui");//Assuming Test is your Folder
FileInfo[] Files = d.GetFiles("*.csv"); //Getting Text files
ChooseSampleSheet.DataSource = Files;
ChooseSampleSheet.DisplayMember = "Name";
So basically what I'm trying to accomplish is being able to select a file from a displayed list and open that file. Right now I have it set up in a CheckBoxList that displays the .docx, .mov, and .txt files that exist in the selected folder. The problem is I can't get it to open the file. I've seen most people suggesting-
Process.Start(filename);
But the problem with that is that it requires a specific file name and I'm trying to pull that name from a variable. Any ideas?
Here's my current code -
private void Form1_Load(object sender, EventArgs e)
{
const string path = #"C:\Users\Haxelle\Documents\Journal";
List<string> extensions = new List<string> { "DOCX", "MOV", "TXT" };
string[] files = GetFilesWithExtensions(path, extensions);
ckbEntry.Items.AddRange(files);
}
private string[] GetFilesWithExtensions(string path, List<string> extensions)
{
string[] allFilesInFolder = Directory.GetFiles(path);
return allFilesInFolder.Where(f => extensions.Contains(f.ToUpper().Split('.').Last())).ToArray();
}
private void btnOpen_Click(object sender, EventArgs e)
{
CheckedListBox.CheckedItemCollection selectedFiles = ckbEntry.CheckedItems;
}
Trying to open file in btnOpen_Click
It seems like all you are missing is iterating over the selected files names and opening them. Since the CheckedItemCollection.Item is typed as object, you will need to cast the items, which can be done using LINQ's Cast function.
private void btnOpen_Click(object sender, EventArgs e)
{
CheckedListBox.CheckedItemCollection selectedFiles = ckbEntry.CheckedItems;
foreach (var filename in selectedFiles.Cast<string>()) {
Process.Start(filename);
}
}
I found this code for dragging and dropping files into a GUI but it just seems to display the filepath. the csv file contains Hex Values, how do I display and use these?
private void Form1_Load(object sender, EventArgs e)
{
this.AllowDrop = true;
this.DragEnter += new DragEventHandler(Form_DragEnter);
this.DragDrop += new DragEventHandler(Form_DragDrop);
void Form_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
void Form_DragDrop(object sender, DragEventArgs e)
{
string[] FileList = (string[])e.Data.GetData(DataFormats.FileDrop, false);
foreach (string File in FileList)
this.Listbox1.Items.Add(File);
}
}
You have to write the file opening and parsing code yourself. The clipboard data for a file drop is simply the filename itself, not the contents of a file.
If you think about this logically, you will realise why (i.e. think of what dragging and then dropping a file would entail each time you did it).
I would like to know how to Drag and Drop a folder and get its name. I already know how to do it with a file, but I'm not really sure how to modify it to be able to drag folders as well. Here's the code of the event that is triggered when a file is dropped:
private void checkedListBox_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
// NB: I'm only interested in the first file, even if there were
// multiple files dropped
string fileName = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
}
}
You could test if the path is a folder and in your DragEnter handler, conditionally change the Effect:
void Target_DragEnter(object sender, DragEventArgs e)
{
DragDropEffects effects = DragDropEffects.None;
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var path = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
if (Directory.Exists(path))
effects = DragDropEffects.Copy;
}
e.Effect = effects;
}
I'm a newbie in C# and I have 2 Listboxes l-->istBox1 and listBox2 and I want to load files from folder into these listboxes.
I tried like this :
listBox1:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\TestLoadFiles");
FileInfo[] Files = dinfo.GetFiles("*.rtdl");
foreach (FileInfo file in Files)
{
listbox1.Items.Add(file.Name);
}
}
listBox2:
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\TestLoadFiles");
FileInfo[] Files = dinfo.GetFiles("*.dlz");
foreach (FileInfo file in Files)
{
listbox2.Items.Add(file.Name);
}
}
when i run the form, the files from the folder is not displaying???
Instead of listBox1_SelectedIndexChanged, update the listbox against some button click, otherwise your code looks fine. Initially you probably don't have any item in your listbox and that's why SelectedIndexChanged doesn't get fired when you click on it.
Edit: (Since the question has been edited, I will update my answer)
To pouplate your listboxes with Files, you should do that, in some event other than SelectedIndexChanged. Because at the start of your application your listboxes are empty and SelectedIndexChanged event gets fired when there are items in the listbox and user click on it. You may create the following function
private void PopulateListBox(ListBox lsb, string Folder, string FileType)
{
DirectoryInfo dinfo = new DirectoryInfo(Folder);
FileInfo[] Files = dinfo.GetFiles(FileType);
foreach (FileInfo file in Files)
{
lsb.Items.Add(file.Name);
}
}
Now you may call this function with your listbox in some event against a button click or form load. e.g.
private void Form1_Load(object sender, EventArgs e)
{
PopulateListBox(listbox1, #"C:\TestLoadFiles", "*.rtld");
PopulateListBox(listbox2, #"C:\TestLoadFiles", "*.other");
}
This might work ;)
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\TestLoadFiles");
FileInfo[] Files = dinfo.GetFiles("*.rtdl");
foreach (FileInfo file in Files)
{
listbox2.Items.Add(file.Name);
}
}
Wrong event i suppose. Move that code to the constructor of your form/control or attach it to an event of another control. Repopulating the listBox on SelectedIndexChanged when the initial state of the listbox is empty does not make sense.