[英]how to open the content of a folder in to different rich text boxes?
我正在嘗試打開文件對話框並將文件夾中的文件打開到不同的富文本框中? 但我不確定還需要添加什么? 你能不能幫助一個新的血液。
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
richTextBox1.Text = File.ReadAllText(openFileDialog1.FileName);
tabPage1.Text = openFileDialog1.SafeFileName;
}
如果要允許用戶選擇一個文件夾,然后在不同的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
默認情況下只打開一個文件。 嘗試將其MultiSelect
屬性更改為true
。 這樣的事情會做:
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
}
老答案:
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.