ajm113

Ok, I am trying to make a scripting editor in C# and I want my editor so you can create a new tab and have a "new" rich text area on it. I don't want my richtext that you already made move to a new tab. So if someone can tell me so if I can disable the richtext from moving and just create a new one on the new tab please tell me!

Code:
Code Snippet

private void newTab(object sender, EventArgs e)
{
if (rtb != null)
{
tabPage1 = new TabPage("Untitled");
tabPage1.Controls.Add(rtb);
tabPage1.AutoScroll = true;
rtb.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabs.TabPages.Add(tabPage1);
rtb.Multiline = true;
rtb.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;
rtb.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
tabs.SelectedTab = tabPage1;
}
}





Re: Windows Forms General How do I have richtext dublicate in a new tab?

beshaghy

Make a new RichTextBox control. If you add the original one to the new tab it will be the same as the richTextBox on the original tab. Controls are objects and as such they are reference types, adding the same object to multiple tabs, means you are adding references to the one object to multiple places. In short don't check if rtb is null, instead make a new instance of the RichTextBox.

http://msdn2.microsoft.com/en-us/library/490f96s2(VS.71).aspx - Understanding what a reference is.

Change your code to this:

Code Snippet

private void newTab(object sender, EventArgs e)

{

// Create a new instance instead of using the reference to rtb

RichTextBox newRtb = new RichTextBox();

tabPage1 = new TabPage("Untitled");

tabPage1.Controls.Add(newRtb);

tabPage1.AutoScroll = true;

newRtb.Dock = System.Windows.Forms.DockStyle.Fill;

this.tabs.TabPages.Add(tabPage1);

newRtb.Multiline = true;

newRtb.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.ForcedBoth;

newRtb.Font = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));

tabs.SelectedTab = tabPage1;

}





Re: Windows Forms General How do I have richtext dublicate in a new tab?

ajm113

Thanks a lot man! This works perfect! I can't wait to get other stuff going on here! You saved me some time on doing this other wise I would be googling for stuff like this for hours!