如何从 C# 中的 TabPage 获取控件? [英] How to get control(s) from TabPage in C#?

查看:69
本文介绍了如何从 C# 中的 TabPage 获取控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个 TabPages,每个都包含富文本框.如何在选定的选项卡上访问richtechbox?

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.

添加:

这是如何添加富文本框控件的标签页

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;

推荐答案

如果这是 WinForms,那就是:

If this is WinForms, it would just be:

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

如果 rtb 是 RichTextBox 控件的名称.

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

这篇关于如何从 C# 中的 TabPage 获取控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆