Nevron Chart
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » Multi richtextbox with multi tab
       
Author Reply
Tanmay Sarkar
posted 194 posts
since May 28, 2010 
from India

Multi richtextbox with multi tab

  Posted on: 31 Aug 2010       
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!
Tanmay Sarkar
Roy S
posted  94 posts
since  May 01, 2010 
from  Belgium

 Re: Multi richtextbox with multi tab
  Posted on: 31 Aug 2010        0  
The richtextbox becomes the only control of the tab so I think you can get it like this:
tabControl1.SelectedTab.Controls[0]
Tanmay Sarkar
posted  194 posts
since  May 28, 2010 
from  India

 Re: Multi richtextbox with multi tab
  Posted on: 31 Aug 2010        0  
If i wrote "hello world" in rich text box &use the code

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!
Tanmay Sarkar
theLizard
posted  578 posts
since  Oct 18, 2009 
from 

 Re: Multi richtextbox with multi tab
  Posted on: 31 Aug 2010        0  
in    private void TabControl_Click(object sender, EventArgs e)

      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);


theLizard
http://www.c-sharplizards.com
Tanmay Sarkar
posted  194 posts
since  May 28, 2010 
from  India

 Re: Multi richtextbox with multi tab
  Posted on: 31 Aug 2010        0  
Thank you sir, for your long support.

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,

Tanmay Sarkar
theLizard
posted  578 posts
since  Oct 18, 2009 
from 

 Re: Multi richtextbox with multi tab
  Posted on: 31 Aug 2010   Accepted Answer     0  
As I said, you need to manage things.

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);
        }


theLizard
http://www.c-sharplizards.com
Tanmay Sarkar
posted  194 posts
since  May 28, 2010 
from  India

 Re: Multi richtextbox with multi tab
  Posted on: 31 Aug 2010        0  
thank you sir, it solve now! :)
Tanmay Sarkar
       
DevExpress Free UI Controls
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
Discover the top 5 tips for understanding .NET Interop
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved