Nothing shows when I tried to fill Combobox with .csv filenames - c#

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";

Related

How to drag and drop one file at the time?

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);
}

C# Button not firing more than once

When I click the button the first time, it works, but after that, it doesn't do anything. I have tried to debug this and I have looked it up but I can't find an answer. I am probably just not noticing something obvious.
private void button5_Click(object sender, EventArgs e)
{
string[] files;
files = Directory.GetFiles("Tasks");
foreach (string file in files)
{
string[] lines;
StreamReader reader = new StreamReader(file);
lines = File.ReadAllLines(file);
tasks.Add(lines[0]);
reader.Close();
}
listBox1.DataSource = tasks;
}
You're problem is not with the button. It's with the listbox. It's not updating properly because it's not detecting a change in your datasource.
Try setting it to null before updating so it knows it's changing:
listBox1.DataSource = null;
listBox1.DataSource = tasks;
Alternative Method:
You can also use a BindingList instead of a regular list.
See here:
How to refresh DataSource of a ListBox in C# WinForms

Hide/modify string in listView

I am making a program in which users can modify remote files. I put the selected files (depending on some predefined criteria) in a listView, but I display only the file names, not full filepaths.
The problem I get however, is that when a user would double-click on an item, it should open another window to modify that item.
private void listView1_DoubleClick(object sender, EventArgs e)
{
account = File.ReadAllLines("\\\\myremoteserver\\ftp\\"+listView1.SelectedItems[0].Text+".txt");
Form3 passForm = new Form3();
passForm.ShowDialog();
}
private void Form2_Load(object sender, EventArgs e)
{
string[] files = Directory.GetFiles("\\\\myremotserver\\ftp\\","*.txt", System.IO.SearchOption.AllDirectories);
foreach (string s in files)
{
listView1.Items.Add(Path.GetFileNameWithoutExtension(s));
}
}
The problem is, that the files are all in different subfolders, so if I leave the code as is, it will not display the correct content of the file. For example, the file is called test1.txt, it is placed in myremoteserver\ftp\testfolder\test1.txt, but with my program, it will try to find the file in myremoteserver\ftp\test1.txt.
What I am asking is, if it is possible to modify the listView in such a way, that the full file path is always saved, but only the file names are displayed? I do not want the user to see the complete file path of the files, just the file names.
Use the Tag property of the ListViewItem
So to create items...
foreach (string s in files)
{
ListViewItem lvi = new ListViewItem(Path.GetFileNameWithoutExtension(s));
lvi.Tag = s;
listView1.Items.Add(lvi);
}
Then in event handler...
account = File.ReadAllLines("\\\\myremoteserver\\ftp\\"+listView1.SelectedItems[0].Tag +".txt);

Opening outside files in C# WinForms

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);
}
}

Populate files into a listbox from a folder in C# windows forms

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.

Categories