在 RichTextBox 而不是选项卡名称中打开
Opening in RichTextBox rather than Tab name
// 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" 部分更改为选项卡中富文本框的名称,但我想要它,因此无论用户选择哪个选项卡,它都会在其中打开。有什么建议么?谢谢你。
您已将值分配给所选选项卡的 Text
属性。相反,您应该将值分配给 RichTextBox
的 Text
property of RichTextBox
or use Load
方法以加载内容。例如:
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 = ...
// 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" 部分更改为选项卡中富文本框的名称,但我想要它,因此无论用户选择哪个选项卡,它都会在其中打开。有什么建议么?谢谢你。
您已将值分配给所选选项卡的 Text
属性。相反,您应该将值分配给 RichTextBox
的 Text
property of RichTextBox
or use Load
方法以加载内容。例如:
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 = ...