2016-09-07 4 views
0

タブページに2つのリストビューがあります。しかし、私は名前で、右コントロールを見つけるために、機能を探しています:タブページのListViewコントロールを見つける

私はこれは、指定されたコントロールおよびすべての彼の息子のコントロールで指定した名前のコントロールを検索します

foreach (Control c in form.Controls) // loop through form controls 
{ 
    if (c is TabControl) 
    { 
     TabControl f = (TabControl)c; 

     foreach (Control tab in f.Controls) 
     { 
      TabPage tabPage = (TabPage)tab; 

      foreach (Control control in tabPage.Controls) 
      { 
       MessageBox.Show(control.Name); 

       // code to go here 
      } 
     }  
    } 
} 

答えて

0

を持っています。

public Control findControlbyName(String name, Control parent){ 
    foreach (Control ctr in parent.Controls) 
    { 
     if (ctr.Name.Equals(name)) return ctr; 
     else return findControlbyName(name, ctr); 
    } 
    return null; 
} 

あなただけ実行する必要があります。

findControlbyName("NameOfTheListView",this); 
1

Controlsコレクションは、配列を返すFind機能があります。

Control[] ctrls = this.Controls.Find("listView1", true); 
if (ctrls.Length == 1) { 
    MessageBox.Show("Found " + ctrls[0].Name); 
} 
+0

を、それは、ネストされたcontroslのために再帰的に検索していますか? – Aimnox

+0

@Aimnoxそれは "真"な部分です:それはsearchAllChildrenです。 – LarsTech

関連する問題