首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取MDIChild的控件

获取MDIChild的控件
EN

Stack Overflow用户
提问于 2013-03-20 22:35:21
回答 1查看 592关注 0票数 1

我创建了一个具有RichtTextBox的新MDIChild:

代码语言:javascript
复制
Form myForm = new Form();
myForm.MdiParent = this;
RichTextBox rtb = new RichTextBox();
myForm.Controls.Add(rtb);
myForm.Show();

因为可能会打开一些其他没有RTB的MDIChilds,所以我想检查一下ActiveChild中是否有RichtTextBox。我不知道该怎么做。类似于try-catch (?)中的内容:

代码语言:javascript
复制
foreach (Control control in this.ActiveMdiChild.Controls)
{
    // check if the control is a checkbox
    // make the richttextbox as an object so I can do strange things with it ^^
}

你能帮帮我吗?

Thx & Cheers Alex

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-20 22:43:26

您可以使用is运算符检查控件是否为RichTextBox

代码语言:javascript
复制
foreach (Control control in this.ActiveMdiChild.Controls)
{
    if (control is RichTextBox)
    {
        RichTextBox rtfChild = (RichTextBox)control;
        // From here on you can use rtfChild as any other RichTextBox control.
    }
}

当然,您也可以将它用于任何其他类型的控件。

检查子窗体是否具有RichTextBox

代码语言:javascript
复制
bool found = false;
foreach (Control control in this.ActiveMdiChild.Controls)
{
    if (control is RichTextBox)
    {
        found = true;
        break;
    }
}

if (found)
{
}

如果将其放入返回RichTextBox控件的方法中,则可以让该方法检查子窗体是否有RichTextBox,如果有,则返回它,否则返回null

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15526529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档