2

私はPanel1これPanel2とこれの中にありPanel3ます...だから次のように想像してください

Panel1->Panel2->Panel3->button1

では、次のようなパス文字列を取得するにはどうすればよいですか

string path=\Panel1\Panel2\Panel3\button1

button1のすべての親を取得したい場合。
そして、IExtenderProvider を実装するクラスを定義することでそれを行うことができるので、設計時にそれを行うことは可能ですか。

4

1 に答える 1

1

以下は、すべての親の名前を として取得する拡張メソッドですIEnumerable<string>

public static class Extensions
{
    public static IEnumerable<string> GetControlPath(this Control c)
    {
        yield return c.Name;

        if (c.Parent != null)
        {
            Control parent = c.Parent;

            while (parent != null)
            {
                yield return parent.Name;
                parent = parent.Parent;
            }                
        }
    }
}


そして、これを利用するプロジェクトに追加した UserControl のプロパティを次に示します。

public partial class CustomControl : UserControl
{
    public CustomControl()
    {
        InitializeComponent();
    }

    public string ControlPath
    {
        get
        {
            return string.Join(@"\", this.GetControlPath().Reverse());
        }
    }
}


ビルド後、ユーザー コントロールをツールボックスからフォームにドラッグします。他のコントロール内にネストしてください。3 つのパネルをネストし、例のように最も内側のパネルに配置しました。設計時のプロパティは次のようになります。

ユーザー コントロールのプロパティ

これは、 から派生する作成するすべてのクラスに適用できますControlIExtenderProviderここでは無関係のようです。

于 2013-05-10T02:31:55.933 に答える