简体   繁体   English

如何在不同的富文本框中打开文件夹的内容?

[英]how to open the content of a folder in to different rich text boxes?

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 如果要允许用户选择一个文件夹,然后在不同的richtextbox中打开该文件夹中存在的前5个文件,则不需要OpenFileDialog ,而是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. OpenFileDialog默认情况下只打开一个文件。 Try to change its MultiSelect property to true . 尝试将其MultiSelect属性更改为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....?
}           

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM