2013-03-20 66 views
1

的控件創建具有RichtTextBox新MDIChild:獲取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 &乾杯 亞歷

回答

0

您可以檢查控制是RichTextBox使用is操作:

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

+0

謝謝約翰,這對我很有用! – DaFunkyAlex 2013-03-20 14:46:20

+0

不客氣! – 2013-03-20 14:47:19

+0

'if(control.GetType()== typeof(System.Windows.Forms.RichTextBox)'與'if(control是RichTextBox)'是一樣的,因爲我發現了^^ – DaFunkyAlex 2013-03-20 14:57:43