I get a problem, I dynamicaly build a tab with richtextbox. &
want to save it. but i can't save that. Can you help me please?
my code is as follows,
int counter=0;
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage tp = new TabPage();
this.tabControl1.Controls.Add(tp);
RichTextBox rt = new RichTextBox();
rt.Dock = System.Windows.Forms.DockStyle.Fill;
counter++;
tp.Name = "New Doc "+Convert.ToString(counter);
tp.Text = "New Doc " + Convert.ToString(counter);
tp.Controls.Add(rt);
}
I think we can get the selected tab like, tabControl1.SelectedTab
but how get the richtextbox to save the content.
Thank you!
Loading

theLizardPosted Aug 31, 2010, 10:40 PM
You cannot declare the rich text box inside the method it must be done globally and you must have an array of text boxes.
RichTextBox[] theBoxes = new RichTextBox[0];
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage tp = new TabPage();
this.tabControl1.Controls.Add(tp);
tp.Name = "re" + IntToStr(this.tabControl1.TabCount);
Array.Resize(ref, theBoxes, theBoxes.Length+1)
theBoxes[theBoxes.Length -1] = new RichTextBox();
theBoxes[theBoxes.Length -1].Name = tp.Name;
then to access the text box
Control[] controls = this.tabControl1.Controls.Find(tp.Name,false);
then you can do
controls[0].SaveToFile ....
I have not tested thie so you will need to work it out ...
RichTextBox rt = new RichTextBox();
rt.Size = new System.Drawing.Size(453, 325);
rt.Dock = System.Windows.Forms.DockStyle.Fill;
tp.Text = "New Doc";
tp.ImageIndex = 0;
tp.Controls.Add(rt);
}
Tanmay SarkarPosted Aug 31, 2010, 11:44 PM
Tanmay SarkarPosted Aug 31, 2010, 10:19 PM
when i dynamically build it, code is
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage tp = new TabPage();
this.tabControl1.Controls.Add(tp);
RichTextBox rt = new RichTextBox();
rt.Size = new System.Drawing.Size(453, 325);
rt.Dock = System.Windows.Forms.DockStyle.Fill;
tp.Text = "New Doc";
tp.ImageIndex = 0;
tp.Controls.Add(rt);
}
And as you mention to save the richtextbox, the code is
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage tp=(TabPage)tabControl1.TabPages[tabControl1.SelectedIndex];
Control[] controls = tp.Controls.Find("rt",false);
RichTextBox rt1 = (RichTextBox)controls[0];
rt1.AppendText("Hello");
rt1.SaveFile(@"C:\123.rtf", RichTextBoxStreamType.PlainText);
}
in control[] line, you wrote,
Control[] controls = tp.Controls.Find("the name of the RichTextBox", false);
as when i built it i use rt so i write,
Control[] controls = tp.Controls.Find("rt",false);
but it gets an exception,
System.IndexOutOfRangeException: Index was outside the bounds of the array.
help me please.
with regards,
theLizardPosted Aug 31, 2010, 7:23 PM
tp = (TabPage)TabControl.TabPages[TabControl.SelectedIndex];
Control[] controls = tp.Controls.Find("the name of the RichTextBox", false);
RichTextBox rt = (RichTextBox)controls[0];
rt.AppendText("Hello");
rt.SaveFile(@"the path and file name", RichTextBoxStreamType.PlainText);
to open files
rt.LoadFile(@"the path and file name", RichTextBoxStreamType.PlainText);
This is just an example,
in the click event you would get the tab text which might be something like the "c:\folder\the file.txt"
currentFile = tp.Text;
then when you want to save the file
rt.SaveFile(currentFile, RichTextBoxStreamType.PlainText);
in the click event of the RichTextBox you can set which is the current text box in the similar way
global declaration
RichTextBox currentTextBox = null;
private void richTextBox1_Click(object sender, EventArgs e)
{
currentTextBox = (RichTextBox)sender;
}
then
currentTextBox .SaveFile(currentFile, RichTextBoxStreamType.PlainText);
Tanmay SarkarPosted Aug 31, 2010, 3:18 PM
tabControl1.SelectedTab.Controls[0]
then the data will be store as
"System.Windows.Forms.RichTextBox, Text: hello world"
not "hello world"
can you help me please!
Thank you!
Roy SPosted Aug 31, 2010, 11:59 AM
tabControl1.SelectedTab.Controls[0]