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

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

The reason your approach is not working is because your are trying to find it by using the control's name property.

Looking at your code you are not setting the Name property. If you can generate a known name ahead of time you can use that when looking for the control.

You can also try this:

var rtb = tabControl.SelectedTab.Controls.Cast<Control>()
                                .FirstOrDefault(x => x is RichTextBox);

Tags:

C#

Winforms