How to reference a richtextbox ?

B

Bart

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
 
F

Fredo

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

Alfred Myers [C# MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top