I'm trying to open a file dialog and open the files inside a folder into different rich text boxes? but i'm not sure what else I would need to add? could you please help out a new blood.
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
richTextBox1.Text = File.ReadAllText(openFileDialog1.FileName);
tabPage1.Text = openFileDialog1.SafeFileName;
}
If you want to allow your user to select a folder and then open the first 5 files present in that folder each one in a different richtextbox then you don't need the OpenFileDialog
, but the FolderBrowserDialog
// First prepare two list with the richtextboxes and the tabpages
List<RichTextBox> myBoxes = new List<RichTextBox>()
{ richTextBox1, richTextBox2, richTextBox3, richTextBox4, richTextBox5 };
List<TabPage> myPages = new List<TabPage>()
{ tabPage1, tabPage2, tabPage3, tabPage4, tabPage5};
// Now open the folderbrowser dialog
// (see link above for some of its properties)
FolderBrowserDialog fbd = new FolderBrowserDialog();
if(fbd.ShowDialog() == DialogResult.OK)
{
int i = 0;
foreach(string file in Directory.GetFiles(fbd.SelectedPath))
{
myBoxes[i].Text = File.ReadAllText(file);
myPages[i].Text = Path.GetFileName(file);
i++;
// Added a warning if the folder contains more than 5 files
if(i >= 5)
{
MessageBox.Show("Too many files in folder, only 5 loaded");
break;
}
}
}
OpenFileDialog
is to open but one file by default. Try to change its MultiSelect
property to true
. Something like this will do:
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
for (int i = 0; i < openFileDialog1.FileNames.Length; ++i) {
RichTextBox rtb = Controls.Cast<Control>().Single(x => x.Name == "richTextBox" + (i + 1).ToString()) as RichTextBox;
rtb.Text = File.ReadAllText(openFileDialog1.FileNames[i]);
}
tabPage1.Text = openFileDialog1.SafeFileName; //again, I wonder what you want to do with this. If needed be, consider to update this dynamically too
}
Old answer:
openFileDialog1.Multiselect = true; //important: set this to true
richTextBox1.Text = ""; //and you may want to reset this every time
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
foreach(var filename in openFileDialog1.FileNames) //get file names here
richTextBox1.Text += File.ReadAllText(filename); //you may want to add enter per file
tabPage1.Text = openFileDialog1.SafeFileName; //but I wonder what you want to do with this....?
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.