簡體   English   中英

如何在Winforms中使用遞歸方法C#

[英]How to use Recursive Method in winforms c#

我需要在表單中找到控件。 在asp.net中,我使用了遞歸方法。現在在winforms中怎么做

public static Control FindControlRecursive(Control root, string id)
{
    if (root.ID == id)
        return root;

    return root.Controls.Cast<Control>()
       .Select(c => FindControlRecursive(c, id))
       .FirstOrDefault(c => c != null);
}

任何想法..謝謝...

每個控件都有一個Controls屬性,實際上是ControlCollection 該集合本身有一個使用2個參數的Find()方法。 第一個參數是您要查找的控件的名稱,第二個參數指示是否將所有子級都包括在搜索中。

作為示例:

Control[] allButton1 = this.Controls.Find("button1", true);
// for your example
Control[] foundControls = this.Controls.Find(root.Name,true);

您可以在勝利形式中使用root.Name代替root.ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM