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;
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.
The following code snippet adds a new page to the TabControl programmatically: TabPage newPage = new TabPage("New Page");
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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With