在RichTextBox而不是Tab名称中打开

时间:2016-04-16 18:57:13

标签: c# winforms tabcontrol

    // add a module tab
    private void add_mod_Click(object sender, EventArgs e)
    {

        int TabCount = 0;

        int? index = searchIndex(mod_add_textbox.Text);
        if (index == null)
        {
            RichTextBox new_rich = new RichTextBox();
            new_rich.Dock = DockStyle.Fill;
            TabPage NewPage = new TabPage();
            TabCount += 1;
            string DocumentText = mod_add_textbox.Text;
            NewPage.Name = DocumentText;
            NewPage.Text = DocumentText;
            NewPage.Controls.Add(new_rich);
            mod_tab.TabPages.Add(NewPage);

        }

        else
        {
            mod_tab.SelectedIndex = Convert.ToInt32(index);
        }


    }     


    private async void btn_file_note_Click(object sender, EventArgs e)
    {
        using(OpenFileDialog ofd = new OpenFileDialog() { Filter="Text Documents|*.txt", ValidateNames = true, Multiselect = false })
        {
            if(ofd.ShowDialog() == DialogResult.OK)
            {
                using (StreamReader sr = new StreamReader(ofd.FileName))
                {
                    mod_tab.SelectedTab.Text = await sr.ReadToEndAsync();
                }
            }
        }
    }

我遇到的问题是,当我尝试打开文档时,它会将其打开到选项卡名称而不是选项卡内的富文本框。我已经更改了" mod_tab.SelectedTab"部分到选项卡中的富文本框的名称,但我想要它,所以用户选择它在那里打开的任何选项卡。有什么建议?谢谢。

1 个答案:

答案 0 :(得分:1)

您已将值分配给所选标签的boolean isGroup=false; while(line = br.readline() != null){ if(line.equals("Amount"){ isGroup=true; } if(line.equals("Period to which") && isGroup) isGroup=false; if(isGroup){ //read line and check whether it is null or not String amount = line; } } 属性。相反,您应该将值分配给Text的{​​{3}}属性,或使用RichTextBox RichTextBox方法来加载内容。例如:

this.richTextBox1.Text = ....

此外,当您在代码中动态创建选项卡和RichTextBox时,您可以通过以下方式找到它:

//It means: Find  all RichTextBox control which are children of mod_tab.SelectedTab
//And return first of them.

var rtb = this.mod_tab.SelectedTab.Controls.OfType<RichTextBox>().FirstOrDefault();
rtb.Text = ...

也是这样:

//It means get the first child control of mod_tab.SelectedTab
//And convert it to RichTextBox.

var rtb = this.mod_tab.SelectedTab.Controls[0] as RichTextBox;
rtb.Text = ...