//create new tab page and add out Rich Text Box To It
public void NewTabObject()
{
TabPage t = new TabPage();
t.Name = "Text_Page:" + tabs.ToString();
t.Text = "Text_Page:" + tabs.ToString();
tb_control_main.Controls.Add(t);
RichTextBox r = new RichTextBox();
r.Size = new Size(884,464);
r.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
r.Name = t.Name + "r";
r.Font = new Font(FontFamily.GenericSansSerif,12,FontStyle.Regular);
t.Controls.Add(r);
tabs++;
}
public void CloseTabObject()
{
if (tb_control_main.TabCount > 1)
{
tb_control_main.SelectedTab.Dispose();
}
else { MessageBox.Show("You Have Only One Tab Open Currently. You Must Have At Least Two Tabs Open Before Closing One. I.E. You Can't Have Zero Tabs", "You Can't Have Zero Tabs", MessageBoxButtons.OK, MessageBoxIcon.Warning); }
}
These are the methods for creating new tabs::it works fine then you have the global variable in the beginning.
public uint tabs = 2;
public RichTextBox rtb;
The tabs int is just used for naming each new tab and incrementally it like new tab 2 3 etc.,
private void frm_main_Load(object sender, EventArgs e)
{
this.rtb = this.rch_1;
fntfuncsdll.FontFuncs.UpdateLbl(this.rtb, this.tlstrp_topmain_lbl_justification_font);
//load settings
LoadSettings();
rtb.Selectionchanged += new EventHandler(rtb_Selectionchanged);
}
This assigns the rtb to the main RTB before or if the user creates new tabs which obviously have a RTB that looks the same. The fntfuncsdll.FontFuncs.UpdateLbl(this.rtb, this.tlstrp_topmain_lbl_justification_font); is what updates the label to the currently used font etc. and this changes automatically with the event when the user moves around in the RTB. The actual function I'm using for this is
public static void UpdateLbl(RichTextBox r, ToolStripLabel l)
{
if (r.SelectedText.Length < 2)
{
l.Text = r.SelectionFont.OriginalFontName.ToString() + "|" + r.SelectionFont.Size.ToString() + "pt |" + r.SelectionFont.Style.ToString();
}
}
}
Selection length is needed since if more is highlighted in the RTB it will cause a object instance exception.
The way to re-assign this variable to a new textbox if done when the user actually clicks the new tab which they have to do then I use this
private void tb_control_main_Selecting(object sender, TabControlCancelEventArgs e)
{
//update variable
foreach (RichTextBox r in e.TabPage.Controls)
{
this.rtb = r;
}
//update event
rtb.Selectionchanged -= new EventHandler(rtb_Selectionchanged);
rtb.Selectionchanged += new EventHandler(rtb_Selectionchanged);
}
This is the most important code. Essentially since there is only 1 RTB in the new tab page when a user creates once I know that to assign to rtb but the the old event issued at the form load is now void so I figured you have to first -= that event and then += creating the event over again which now works with the newly selected tab and RTB. If the user changes tabs this automatically occurs again.
Is there anything bad about this method? Can it lead memory leaks etc.? Advice?

New Topic/Question
Reply



MultiQuote





|