我创建了一个具有RichtTextBox的新MDIChild:
Form myForm = new Form();
myForm.MdiParent = this;
RichTextBox rtb = new RichTextBox();
myForm.Controls.Add(rtb);
myForm.Show();因为可能会打开一些其他没有RTB的MDIChilds,所以我想检查一下ActiveChild中是否有RichtTextBox。我不知道该怎么做。类似于try-catch (?)中的内容:
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
发布于 2013-03-20 22:43:26
您可以使用is运算符检查控件是否为RichTextBox:
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
bool found = false;
foreach (Control control in this.ActiveMdiChild.Controls)
{
if (control is RichTextBox)
{
found = true;
break;
}
}
if (found)
{
}如果将其放入返回RichTextBox控件的方法中,则可以让该方法检查子窗体是否有RichTextBox,如果有,则返回它,否则返回null。
https://stackoverflow.com/questions/15526529
复制相似问题