- PR -

カスタムコントロールに関して

1
投稿者投稿内容
YAMURA
会議室デビュー日: 2002/12/13
投稿数: 3
投稿日時: 2004-08-04 12:10
矢村です。

カスタムコントロールを作成した際の
開発環境時のプロパティウィンドウのカテゴリー内での並び順のシンプルな指定方法です。
({プロパティグリッド,PropertyGrid}内の並び順)


カスタムコントロールのプロパティの並び順の指定に関して
簡単な資料が発見できなかったので、他の開発者のご参考になれば。



方法:下記のメソッドをオーバーライドする。

//お決まり
public override bool GetPropertiesSupported(ITypeDescriptorContext context)
{
return true;
}

//ソートオーダー
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attrs)
{
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(value, attrs);
//並び替えたいプロパティの並び順
string[] SortOrder = {"First","Second","Third","Fourth"};
return pdc.Sort(SortOrder);
}


[ メッセージ編集済み 編集者: YAMURA 編集日時 2004-08-04 12:12 ]
YAMURA
会議室デビュー日: 2002/12/13
投稿数: 3
投稿日時: 2004-08-04 15:13
プロパティグリッドの情報指定にフォルダ選択ダイアログボックスを利用する方法です。

1. プロパティの属性に下記を設定

[Editor(typeof(FolderSelectEditor), typeof(UITypeEditor))]


2.独自のプロパティエディタを作成

/// <summary>
/// デザインモード用フォルダパス選択
/// </summary>
public class FolderSelectEditor : System.Drawing.Design.UITypeEditor {
private IWindowsFormsEditorService editService = null;

//選択用のダイアログ表示
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (context != null && context.Instance != null && provider != null)
{
editService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (editService != null)
{
//フォルダ選択ダイアログを作成
//同様にファイル選択、オリジナルのフォームを指定可能
FolderBrowserDialog fbd = new FolderBrowserDialog();
if(fbd.ShowDialog() == DialogResult.OK)
{
value = fbd.SelectedPath;
}

}
}
return value;
}

//PropetyGridの選択用ボタンを表示 [...]
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
if (context != null && context.Instance != null)
{
return UITypeEditorEditStyle.Modal;
}
return base.GetEditStyle(context);
}
}


※フォルダ選択ダイアログは、.NET Framework1.1以降
※プロパティ属性に設定された「FolderSelectEditor」は、独自のプロパティエディタのクラス名


絶対パスなので開発環境では利用価値は少ないのですが、
プロパティグリッドコントロールを使用したアプリケーションでは応用次第で利用価値大です。




[ メッセージ編集済み 編集者: YAMURA 編集日時 2004-08-04 18:10 ]
1

スキルアップ/キャリアアップ(JOB@IT)