Hi all,
Firts of all I am complete new to C# but I am trying to build a simple
texteditor.
So on my mainform I have a tabcontrol and each of the tabs contains a
RichTextBox. Now when the users selects another tab I want to get a
reference to the RichTextBox located on that tab.
Ive tried this:
private RichTextBox CurrentDocument = null;
.
.
.
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
CurrentDocument = tabControl1.TabPages[0].Controls[0];
}
but it doesn't work. Can someone help me out here ....
Thanks a lot in advance,
Bart
Fredo - 02 Mar 2008 18:09 GMT
Try this:
First, create an interface class:
public interface IMyTabPage
{
RichTextBox RichTextBoxControl
{
get;
}
}
Then, implement that interface on your tab pages and have them return the
RichTextBox for the page.
Then in your form code, simply do the following:
RichTextBox currRTB = (tabControl1.SelectedTab as
IMyTabPage).RichTextBoxControl;
> Hi all,
>
[quoted text clipped - 21 lines]
>
> Bart
Alfred Myers [C# MVP] - 02 Mar 2008 21:53 GMT
Hi Bart,
Given that tabControl1.TabPages[0].Controls[0] is of type RichTextBox the
code below should do the job.
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
CurrentDocument = (RichTextBox)tabControl1.TabPages[0].Controls[0];
}
You should note that Controls[0] returns an object of type Control and for
that reason it has to be converted to a RichTextBox.
Of course, that will only work if Control[0] is indead of type RichTextBox.
HTH
Alfred Myers
C# MVP
> Hi all,
>
[quoted text clipped - 21 lines]
>
> Bart