Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

How to reference a richtextbox ?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bart - 02 Mar 2008 17:45 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.