2

我用这个创建一个新标签:

private void addTabs(RichTextBox richTextBox,string name)
    {
        //Add Tabs
        try
        {
            if (tabControl1.TabPages.Count <= 8)
            {
                TabPage tabPage = new TabPage();
                tabPage.Controls.Add(richTextBox);
                if (name == null)
                {
                    tabPage.Text = "Page: " + (tabControl1.TabCount + 1).ToString();
                    tabPage.Tag = "Page: " + (tabControl1.TabCount + 1).ToString();
                }
                else
                {
                    tabPage.Text = name;
                    tabPage.Tag = name;
                }
                richTextBox.Dock = DockStyle.Fill;

                tabControl1.TabPages.Add(tabPage);
            }
            else
                MessageBox.Show("You have reached the max threshold for tabs.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            Console.WriteLine(e.Message);
        }
    }

这将创建一个新选项卡并为其提供一个富文本框。我知道如何使用对话框保存文件,但如何访问richtextbox?

4

1 回答 1

4

您正在通过参数提供richtTextBox。您可以从调用此函数的位置访问它。

或者通过 TabControl 调用您的 TabPage:

// The first control would be your richtextbox if that's the only control in
// the tabpage.
var richTextBox = (RichtTextBox)tabControl1.TabPages[index].Controls[0];
于 2013-10-21T21:44:44.173 回答