Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get control(s) from TabPage in C#?

Tags:

c#

winforms

I have few TabPages and each contains rich text box. How can I access richtechbox on a selected tab?

TabPage selectedTab = tabControl.SelectedTab;
RichTextBox selectedRtb = selectedTab.Controls.Find("rtb", true).First() as RichTextBox;

This is what I have tried but no luck.

Added:

This is how tabpage is added with richtextbox control

TabPage newTab = new TabPage(name);
RichTextBox rtb = new RichTextBox();
rtb.Dock = DockStyle.Fill;
rtb.BorderStyle = BorderStyle.None;
rtb.Text = file.Data;
newTab.Controls.Add(rtb);
tabControl.TabPages.Add(newTab);
tabControl.SelectedTab = newTab;
like image 472
jM2.me Avatar asked Feb 19 '12 17:02

jM2.me


People also ask

How to use Tab control c#?

To create a TabControl control at design-time, you simply drag and drop a TabControl control from Toolbox onto a Form in Visual Studio. After you drag and drop a TabControl on a Form, the TabControl1 is added to the Form and looks like Figure 1. A TabControl is just a container and has no value without tab pages.

Which of the following is the correct way to add a new page to the tab control programmatically?

The following code snippet adds a new page to the TabControl programmatically: TabPage newPage = new TabPage("New Page");


1 Answers

If this is WinForms, it would just be:

if (selectedTab.Controls.ContainsKey("rtb"))
  RichTextBox selectedRtb = (RichTextBox)selectedTab.Controls["rtb"];

if rtb is the name of the RichTextBox control.

When creating your control, add the name to it:

RichTextBox rtb = new RichTextBox();
rtb.Name = "rtb";
like image 200
LarsTech Avatar answered Sep 20 '22 12:09

LarsTech