用标签打开文件

时间:2013-08-23 20:23:01

标签: c# visual-studio

这是我打开新文件的代码:

private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
    OpenFileDialog openFD = new OpenFileDialog();
    string Chosen_File = "";

    openFD.InitialDirectory = "C:";
    openFD.Title = "Open a Text File";
    openFD.FileName = "";

    openFD.Filter = "Text Files|*.txt|Word Documents|*.doc";

    if (openFD.ShowDialog() != DialogResult.Cancel)
    {

        Chosen_File = openFD.FileName;
        GetRichTextBox().LoadFile(Chosen_File, RichTextBoxStreamType.PlainText);

    }

我也在使用标签。我打开标签时只能打开文件。如何设置它以打开包含该文件的新选项卡?它仅在我打开选项卡然后打开文件时才有效。

1 个答案:

答案 0 :(得分:1)

在加载文件之前,您必须在TabPage添加新的TabControl(代码中的方式):

TabPage tab = new TabPage(){Text = System.IO.Path.GetFileName(Chosen_File)};
tabControl1.TabPages.Add(tab);
tabControl1.SelectedTab = tab;
RichTextBox rich = new RichTextBox{Parent = tab, Dock = DockStyle.Fill};
rich.LoadFile(Chosen_File, RichTextBoxStreamType.PlainText);