2010年6月8日火曜日

PropertyGrid

このソフトウェアはデータベースに接続するので、もちろん「データベース接続設定」ダイアログが存在します。

DbConnectionStringBuilder のインスタンスを、PropertyGrid コントロールの SelectedObject プロパティに設定し、はい!完成っ!と、、暫く放置していました。

・・が、よく眺めてみると、Password (または pwd) は「***」とマスクが掛かっているのですが、ConnectionString が表示されていて、そこのパスワードは丸見え(x_x)

パスワードにマスクが掛かっている意味がない。。
設定ファイルを暗号化して保存している意味がない。。。

と、いうことで、PropertyGrid と格闘すること数時間、無事 ConnectionString を非表示にすることができましたので、以下、ソースコード。

PropertyGridの代わりに、以下のCustomPropertyGrid を使えばOKです。

// >> ここから -----------------------------------------------------------------
/// <summary>
/// カスタムプロパティグリッドです。
/// </summary>
public class CustomPropertyGrid : PropertyGrid
{
    /// <summary>
    /// プロパティタブを生成します。
    /// </summary>
    /// <param name="tabType">タブタイプ。</param>
    /// <returns>プロパティタブ。</returns>
    protected override PropertyTab CreatePropertyTab(Type tabType)
    {
        return new CustomPropertyTab();
    }
}

/// <summary>
/// カスタムプロパティタブです。
/// </summary>
public class CustomPropertyTab : PropertyTab
{
    /// <summary>
    /// プロパティのコレクションを取得します。
    /// </summary>
    /// <param name="component">対象のコンポーネント。</param>
    /// <param name="attributes">属性。</param>
    /// <returns>プロパティのコレクション。</returns>
    public override PropertyDescriptorCollection GetProperties(object component, Attribute[] attributes)
    {
        PropertyDescriptorCollection properties;
        if (attributes != null)
        {
            properties = TypeDescriptor.GetProperties(component, attributes);
        }
        else
        {
            properties = TypeDescriptor.GetProperties(component);
        }
        PropertyDescriptorCollection collection = new PropertyDescriptorCollection(null);
        foreach (PropertyDescriptor desc in properties)
        {
            // ConnectionString は追加しない
            if (desc.Name == "ConnectionString")
            {
                continue;
            }
            collection.Add(desc);
        }
        return collection;
    }

    /// <summary>
    /// タブ名を取得します。
    /// </summary>
    public override string TabName
    {
        get
        {
            return "CustomPropertyTab";
        }
    }

    /// <summary>
    /// ビットマップを取得します。
    /// </summary>
    public override Bitmap Bitmap
    {
        get
        {
            return new Bitmap(16, 16);
        }
    }
}
// << ここまで -----------------------------------------------------------------

0 件のコメント:

コメントを投稿