Reputation: 67
I have a select folder function here that looks for .txt files and .lua files in the user's selected folder, it takes the names of those files, and puts them in a list box.
private void selectFolderToolStripMenuItem_Click(object sender, EventArgs e)
{
using (var fbd = new FolderBrowserDialog())
{
DialogResult result = fbd.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
{
string[] files = Directory.GetFiles(fbd.SelectedPath);
string[] txtfiles = Directory.GetFiles(fbd.SelectedPath, "*.txt");
string[] luafiles = Directory.GetFiles(fbd.SelectedPath, "*.lua");
System.Windows.Forms.MessageBox.Show("OK! Here are the number of ususable files i've found. TxtFiles: " + txtfiles.Length.ToString() + " LuaFiles: " + luafiles.Length.ToString() + "");
var wqert = Directory.EnumerateFiles(fbd.SelectedPath, "*.*", SearchOption.AllDirectories)
.Where(s => s.EndsWith(".txt") || s.EndsWith(".lua"));
for (int i = 0; i < files.Length; i++);
{
foreach (var luafile in luafiles)
{
File.ReadAllText(luafile.ToString());
listBox1.Items.Add(Path.GetFileName(luafile.ToString()));
}
foreach (var txtfile in txtfiles)
{
File.ReadAllText(txtfile.ToString());
listBox1.Items.Add(Path.GetFileName(txtfile.ToString()));
}
}
}
}
}
I wish to make an event where when I double-click one of the items on the list box, it takes whatever "stuff" that is inside the files and puts it into a richtextbox.
Upvotes: 0
Views: 73
Reputation: 3271
There are likely a number of ways in which you can complete this, I tend to use this method:
First, Create a class to hold the filename and its content
public class FileObject
{
public String FileName { get; set; }
public String FileContent { get; set; }
}
Second, when you loop through the file names, you will need to create a new instance of the FileObject
class and set its properties:
foreach (var luafile in luafiles)
{
FileObject f = new FileObject();
f.FileName = Path.GetFileName(luafile.ToString());
f.Content = File.ReadAllText(luafile.ToString());
listBox1.Items.Add(f);
}
foreach (var txtfile in txtfiles)
{
FileObject f = new FileObject();
f.FileName = Path.GetFileName(txtfile.ToString());
f.Content = File.ReadAllText(txtfile.ToString());
listBox1.Items.Add(f);
}
Third, as you are now populating the ListBox
control with objects instead of strings it will be necessary to set its DataBinding properties:
listBox1.ValueMember = "FileName";
listBox1.DisplayMember = "FileName";
Finally, you will need to handle the ListBox DoubleClick event in order to carry out your desired action of populating the RichTextBox
private void listBox1_DoubleClick(object sender, EventArgs e)
{
FileObject fileObject = (FileObject)listBox1.SelectedItem;
// Populate the RichTextBox here - I output to the Console as a demo
Console.WriteLine(fileObject.FileContent);
}
Note: If there are a lot of files and/or large content then this method will consume a lot of memory as each file will be loaded in to memory and hang around
Upvotes: 1