DoboWiki
Top
> .NETプログラミング研究/32 をテンプレートにして作成
.NETプログラミング研究/32 をテンプレートにして作成
開始行:
#title(.NETプログラミング研究 第32号)
#navi(.NETプログラミング研究)
#contents
*.NETプログラミング研究 第32号 [#we2fc89d]
**.NET Tips [#n3b218dd]
**PropertyGridコントロールの使い方 [#z10dcb4d]
#column(注意){{
この記事の最新版は「[[PropertyGridコントロールの使い方>ht...
}}
***「PropertyGridコントロール」とは? [#m4f5002a]
.NET Frameworkには、PropertyGridコントロールというコンポ...
PropertyGridコントロールは、ただオブジェクトを指定するだ...
補足:PropertyGridコントロールの使い方に関しては、MSDNに...
-[[.NET Framework の PropertyGrid コントロールの高度な活...
-[[Visual Studio .NET プロパティ ブラウザによるコンポーネ...
***とりあえず使ってみよう [#oe4c6c66]
PropertyGridクラスは、System.Windows.Forms名前空間、Syste...
「Windowsフォーム」タブにPropertyGridコントロールを追加す...
それでは早速PropertyGridコントロールを使ってみましょう。...
まずはPropertyGridコントロールのすばらしさを実感していた...
#code(vbnet){{
PropertyGrid1.SelectedObject = Me
}}
#code(csharp){{
PropertyGrid1.SelectedObject = this;
}}
ビルドし実行すると、PropertyGridコントロールにForm1のプロ...
PropertyGrid.SelectedObjectプロパティを設定しただけで、こ...
***PropertyGridコントロールの基本的なプロパティ [#d3f885a7]
PropertyGridコントロールで使用される基本的なプロパティ、...
PropertyGridコントロールのツールバーを表示しないようにす...
プロパティの並べ方は、PropertySortプロパティで指定します...
選択されている項目は、SelectedGridItemプロパティにより取...
***自作したクラスに使ってみよう [#r24d76c4]
次に、クラスを自作し、そのプロパティをPropertyGridコント...
まずは、次のようなクラスを作成します。
#code(vbnet){{
Public Class TestClass
Public Enum TestEnum
One
Two
Tree
End Enum
Private _integerValue As Integer = 0
Private _stringValue As String = "こんにちは"
Private _booleanValue As Boolean = False
Private _enumValue As TestEnum = TestEnum.One
Private _colorValue As System.Drawing.Color = _
System.Drawing.Color.Red
Public Property IntegerValue() As Integer
Get
Return _integerValue
End Get
Set(ByVal Value As Integer)
_integerValue = Value
End Set
End Property
Public Property StringValue() As String
Get
Return _stringValue
End Get
Set(ByVal Value As String)
_stringValue = Value
End Set
End Property
Public Property BooleanValue() As Boolean
Get
Return _booleanValue
End Get
Set(ByVal Value As Boolean)
_booleanValue = Value
End Set
End Property
Public Property EnumValue() As TestEnum
Get
Return _enumValue
End Get
Set(ByVal Value As TestEnum)
_enumValue = Value
End Set
End Property
Public Property ColorValue() As System.Drawing.Color
Get
Return _colorValue
End Get
Set(ByVal Value As System.Drawing.Color)
_colorValue = Value
End Set
End Property
End Class
}}
#code(csharp){{
public class TestClass
{
public enum TestEnum
{
One,
Two,
Tree
}
private int _integerValue = 0;
private string _stringValue = "こんにちは";
private bool _booleanValue = false;
private TestEnum _enumValue = TestEnum.One;
private System.Drawing.Color _colorValue =
System.Drawing.Color.Red;
public int IntegerValue
{
get {return _integerValue;}
set {_integerValue = value;}
}
public string StringValue
{
get {return _stringValue;}
set {_stringValue = value;}
}
public bool BooleanValue
{
get {return _booleanValue;}
set {_booleanValue = value;}
}
public TestEnum EnumValue
{
get {return _enumValue;}
set {_enumValue = value;}
}
public System.Drawing.Color ColorValue
{
get {return _colorValue;}
set {_colorValue = value;}
}
}
}}
このTestClassオブジェクトを次のようにしてPropertyGrid.Sel...
#code(vbnet){{
Dim cls As New TestClass
PropertyGrid1.SelectedObject = cls
}}
#code(csharp){{
TestClass cls = new TestClass();
PropertyGrid1.SelectedObject = cls;
}}
何もしなくてもPropertyGridコントロールでは、プロパティの...
それ以外では、Color型のプロパティではリストにより色の選択...
***プロパティの表示方法を変更する [#p77a5e06]
このようにPropertyGridコントロールは実に簡単に使えますの...
ここから以下は、特にプロパティの表示方法に関するテクニッ...
using System.ComponentModel;
VB.NETでは
Imports System.ComponentModel
が宣言されているものとします。
***プロパティのデフォルト値を指定する [#v5f2741c]
VS.NETの場合、プロパティがデフォルト値(規定値)でないと...
次の例では、IntegerValueプロパティのデフォルト値を0にして...
#code(vbnet){{
<DefaultValue(0)> _
Public Property IntegerValue() As Integer
Get
Return _integerValue
End Get
Set(ByVal Value As Integer)
_integerValue = Value
End Set
End Property
}}
#code(csharp){{
[DefaultValue(0)]
public int IntegerValue
{
get {return _integerValue;}
set {_integerValue = value;}
}
}}
ところで、Color型、Font型、Size型などのデフォルト値は上記...
-[[PropertyDescriptor.ShouldSerializeValue メソッド>http:...
次の例では、ColorValueプロパティのデフォルト値をColor.Red...
#code(vbnet){{
Private _colorValue As System.Drawing.Color = _
System.Drawing.Color.Red
Public Property ColorValue() As System.Drawing.Color
Get
Return _colorValue
End Get
Set(ByVal Value As System.Drawing.Color)
_colorValue = Value
End Set
End Property
Private Function ShouldSerializeColorValue() As Boolean
Return Not ColorValue.Equals(System.Drawing.Color.Red)
End Function
}}
#code(csharp){{
private System.Drawing.Color _colorValue =
System.Drawing.Color.Red;
public System.Drawing.Color ColorValue
{
get {return _colorValue;}
set {_colorValue = value;}
}
private bool ShouldSerializeColorValue()
{
return ColorValue != System.Drawing.Color.Red;
}
}}
***クラスのデフォルトプロパティを指定する [#x2b4780d]
PropertyGridコントロールで一番初めに選択されるプロパティ...
次の例では、"TestClass"クラスのデフォルトプロパティを"Str...
#code(vbnet){{
<DefaultProperty("StringValue")> _
Public Class TestClass
'(省略)
End Class
}}
#code(csharp){{
[DefaultProperty("StringValue")]
public class TestClass
{
//(省略)
}
}}
***プロパティの説明を表示する [#ld1f6ff8]
PropertyGridコントロールの説明ペインに、選択されているプ...
次の例では、StringValueプロパティの説明を設定しています。
#code(vbnet){{
<Description("ここにStringValueの説明を書きます。")> _
Public Property StringValue() As String
Get
Return _stringValue
End Get
Set(ByVal Value As String)
_stringValue = Value
End Set
End Property
}}
#code(csharp){{
[Description("ここにStringValueの説明を書きます。")]
public string StringValue
{
get {return _stringValue;}
set {_stringValue = value;}
}
}}
***プロパティの項目を指定する [#h0d542cd]
PropertyGridコントロールではプロパティを項目(カテゴリ)別...
次の例では、ColorValueプロパティの項目を"表示"にしていま...
#code(vbnet){{
<Category("表示")> _
Public Property ColorValue() As System.Drawing.Color
Get
Return _colorValue
End Get
Set(ByVal Value As System.Drawing.Color)
_colorValue = Value
End Set
End Property
}}
#code(csharp){{
[Category("表示")]
public System.Drawing.Color ColorValue
{
get {return _colorValue;}
set {_colorValue = value;}
}
}}
***プロパティを表示しない [#cc46e292]
PropertyGridコントロールに表示したくないプロパティには、F...
次の例では、BooleanValueプロパティをPropertyGridコントロ...
#code(vbnet){{
<Browsable(False)> _
Public Property BooleanValue() As Boolean
Get
Return _booleanValue
End Get
Set(ByVal Value As Boolean)
_booleanValue = Value
End Set
End Property
}}
#code(csharp){{
[Browsable(false)]
public bool BooleanValue
{
get {return _booleanValue;}
set {_booleanValue = value;}
}
}}
***プロパティの値が編集できないようにする [#a57e5c9d]
プロパティの値をユーザーが編集できないようにするには、Tru...
次の例では、IntegerValueプロパティをPropertyGridコントロ...
#code(vbnet){{
<ReadOnlyAttribute(True)> _
Public Property IntegerValue() As Integer
Get
Return _integerValue
End Get
Set(ByVal Value As Integer)
_integerValue = Value
End Set
End Property
}}
#code(csharp){{
[ReadOnly(true)]
public int IntegerValue
{
get {return _integerValue;}
set {_integerValue = value;}
}
}}
***「ファイルを開く」ダイアログを表示してファイルを選択で...
右側にボタンを表示し、このボタンをクリックすることにより...
次の例では、StringValueプロパティにボタンを表示し、「ファ...
#code(vbnet){{
<Editor(GetType(System.Windows.Forms.Design.FileNameEdito...
GetType(System.Drawing.Design.UITypeEditor))> _
Public Property StringValue() As String
Get
Return _stringValue
End Get
Set(ByVal Value As String)
_stringValue = Value
End Set
End Property
}}
#code(csharp){{
[Editor(typeof(System.Windows.Forms.Design.FileNameEditor),
typeof(System.Drawing.Design.UITypeEditor))]
public string StringValue
{
get {return _stringValue;}
set {_stringValue = value;}
}
}}
***プロパティを展開できるようにする [#i7883981]
例えば次のようなSize型のプロパティは、PropertyGridコント...
#code(vbnet){{
Public Class TestClass
Private _size As New Size(10, 10)
Public Property Size() As Size
Get
Return _size
End Get
Set(ByVal Value As Size)
_size = value
End Set
End Property
End Class
}}
#code(csharp){{
public class TestClass
{
private Size _size = new Size(10, 10);
public Size Size
{
get {return _size;}
set {_size = value;}
}
}
}}
しかし次のような自作のクラスの場合は、展開ができず、値を...
#code(vbnet){{
Public Class CustomClass
Private _number As Integer = 0
Private _message As String = "hello"
Public Property Number() As Integer
Get
Return _number
End Get
Set(ByVal Value As Integer)
_number = Value
End Set
End Property
Public Property Message() As String
Get
Return _message
End Get
Set(ByVal Value As String)
_message = Value
End Set
End Property
End Class
Public Class TestClass
Private _custom As New CustomClass
Public Property Custom() As CustomClass
Get
Return _custom
End Get
Set(ByVal Value As CustomClass)
_custom = Value
End Set
End Property
End Class
}}
#code(csharp){{
public class CustomClass
{
private int _number = 0;
private string _message = "hello";
public int Number
{
get {return _number;}
set {_number = value;}
}
public string Message
{
get {return _message;}
set {_message = value;}
}
}
public class TestClass
{
private CustomClass _custom = new CustomClass();
public CustomClass Custom
{
get {return _custom;}
set {_custom = value;}
}
}
}}
このような自作のクラス型のプロパティでもPropertyGridコン...
#code(vbnet){{
<TypeConverter(GetType(ExpandableObjectConverter))> _
Public Class CustomClass
'(省略)
End Class
}}
#code(csharp){{
[TypeConverter(typeof(ExpandableObjectConverter))]
public class CustomClass
{
//(省略)
}
}}
これで"CustomClass"型のプロパティが展開できるようになりま...
次のような型コンバータクラスを使用することにより、CustomC...
#code(vbnet){{
Public Class CustomClassConverter
Inherits ExpandableObjectConverter
'コンバータがオブジェクトを指定した型に変換できるか
'(変換できる時はTrueを返す)
'ここでは、CustomClass型のオブジェクトには変換可能と...
Public Overloads Overrides Function CanConvertTo( _
ByVal context As ITypeDescriptorContext, _
ByVal destinationType As Type) As Boolean
If destinationType Is GetType(CustomClass) Then
Return True
End If
Return MyBase.CanConvertTo(context, destinationTy...
End Function
'指定した値オブジェクトを、指定した型に変換する
'CustomClass型のオブジェクトをString型に変換する方法...
Public Overloads Overrides Function ConvertTo( _
ByVal context As ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo...
ByVal value As Object, _
ByVal destinationType As Type) As Object
If destinationType Is GetType(String) And TypeOf ...
Dim cc As CustomClass = CType(value, CustomCl...
Return cc.Number.ToString() + "," + cc.Message
End If
Return MyBase.ConvertTo(context, culture, value, ...
End Function
'コンバータが特定の型のオブジェクトをコンバータの型に...
'(変換できる時はTrueを返す)
'ここでは、String型のオブジェクトなら変換可能とする
Public Overloads Overrides Function CanConvertFrom( _
ByVal context As ITypeDescriptorContext, _
ByVal sourceType As Type) As Boolean
If sourceType Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function
'指定した値をコンバータの型に変換する
'String型のオブジェクトをCustomClass型に変換する方法...
Public Overloads Overrides Function ConvertFrom( _
ByVal context As ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo...
ByVal value As Object) As Object
If TypeOf value Is String Then
Dim ss As String() = value.ToString().Split(N...
Dim cc As New CustomClass
cc.Number = Integer.Parse(ss(0))
cc.Message = ss(1)
Return cc
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
End Class
}}
#code(csharp){{
public class CustomClassConverter : ExpandableObjectConve...
{
//コンバータがオブジェクトを指定した型に変換できるか
//(変換できる時はTrueを返す)
//ここでは、CustomClass型のオブジェクトには変換可能と...
public override bool CanConvertTo(
ITypeDescriptorContext context, Type destinationT...
{
if (destinationType == typeof(CustomClass))
return true;
return base.CanConvertTo(context, destinationType);
}
//指定した値オブジェクトを、指定した型に変換する
//CustomClass型のオブジェクトをString型に変換する方法...
public override object ConvertTo(ITypeDescriptorConte...
CultureInfo culture, object value, Type destinati...
{
if (destinationType == typeof(string) &&
value is CustomClass)
{
CustomClass cc = (CustomClass) value;
return cc.Number.ToString() + "," + cc.Message;
}
return base.ConvertTo(context, culture, value, de...
}
//コンバータが特定の型のオブジェクトをコンバータの型...
//(変換できる時はTrueを返す)
//ここでは、String型のオブジェクトなら変換可能とする
public override bool CanConvertFrom(
ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom (context, sourceType);
}
//指定した値をコンバータの型に変換する
//String型のオブジェクトをCustomClass型に変換する方法...
public override object ConvertFrom(ITypeDescriptorCon...
CultureInfo culture, object value)
{
if (value is string)
{
string[] ss = value.ToString().Split(new char...
CustomClass cc = new CustomClass();
cc.Number = int.Parse(ss[0]);
cc.Message = ss[1];
return cc;
}
return base.ConvertFrom(context, culture, value);
}
}
}}
使い方は前と同じです。
#code(vbnet){{
<TypeConverter(GetType(CustomClassConverter))> _
Public Class CustomClass
'(省略)
End Class
}}
#code(csharp){{
[TypeConverter(typeof(CustomClassConverter))]
public class CustomClass
{
//(省略)
}
}}
***もっと勉強したい方は... [#f77594a6]
MSDNにある「.NET Framework の PropertyGrid コントロールの...
参考:
-[[.NET Framework の PropertyGrid コントロールの高度な活...
-[[Visual Studio .NET プロパティ ブラウザによるコンポーネ...
**コメント [#f494856a]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]],2004-05-04 (火) 06:00:00,DOB...
終了行:
#title(.NETプログラミング研究 第32号)
#navi(.NETプログラミング研究)
#contents
*.NETプログラミング研究 第32号 [#we2fc89d]
**.NET Tips [#n3b218dd]
**PropertyGridコントロールの使い方 [#z10dcb4d]
#column(注意){{
この記事の最新版は「[[PropertyGridコントロールの使い方>ht...
}}
***「PropertyGridコントロール」とは? [#m4f5002a]
.NET Frameworkには、PropertyGridコントロールというコンポ...
PropertyGridコントロールは、ただオブジェクトを指定するだ...
補足:PropertyGridコントロールの使い方に関しては、MSDNに...
-[[.NET Framework の PropertyGrid コントロールの高度な活...
-[[Visual Studio .NET プロパティ ブラウザによるコンポーネ...
***とりあえず使ってみよう [#oe4c6c66]
PropertyGridクラスは、System.Windows.Forms名前空間、Syste...
「Windowsフォーム」タブにPropertyGridコントロールを追加す...
それでは早速PropertyGridコントロールを使ってみましょう。...
まずはPropertyGridコントロールのすばらしさを実感していた...
#code(vbnet){{
PropertyGrid1.SelectedObject = Me
}}
#code(csharp){{
PropertyGrid1.SelectedObject = this;
}}
ビルドし実行すると、PropertyGridコントロールにForm1のプロ...
PropertyGrid.SelectedObjectプロパティを設定しただけで、こ...
***PropertyGridコントロールの基本的なプロパティ [#d3f885a7]
PropertyGridコントロールで使用される基本的なプロパティ、...
PropertyGridコントロールのツールバーを表示しないようにす...
プロパティの並べ方は、PropertySortプロパティで指定します...
選択されている項目は、SelectedGridItemプロパティにより取...
***自作したクラスに使ってみよう [#r24d76c4]
次に、クラスを自作し、そのプロパティをPropertyGridコント...
まずは、次のようなクラスを作成します。
#code(vbnet){{
Public Class TestClass
Public Enum TestEnum
One
Two
Tree
End Enum
Private _integerValue As Integer = 0
Private _stringValue As String = "こんにちは"
Private _booleanValue As Boolean = False
Private _enumValue As TestEnum = TestEnum.One
Private _colorValue As System.Drawing.Color = _
System.Drawing.Color.Red
Public Property IntegerValue() As Integer
Get
Return _integerValue
End Get
Set(ByVal Value As Integer)
_integerValue = Value
End Set
End Property
Public Property StringValue() As String
Get
Return _stringValue
End Get
Set(ByVal Value As String)
_stringValue = Value
End Set
End Property
Public Property BooleanValue() As Boolean
Get
Return _booleanValue
End Get
Set(ByVal Value As Boolean)
_booleanValue = Value
End Set
End Property
Public Property EnumValue() As TestEnum
Get
Return _enumValue
End Get
Set(ByVal Value As TestEnum)
_enumValue = Value
End Set
End Property
Public Property ColorValue() As System.Drawing.Color
Get
Return _colorValue
End Get
Set(ByVal Value As System.Drawing.Color)
_colorValue = Value
End Set
End Property
End Class
}}
#code(csharp){{
public class TestClass
{
public enum TestEnum
{
One,
Two,
Tree
}
private int _integerValue = 0;
private string _stringValue = "こんにちは";
private bool _booleanValue = false;
private TestEnum _enumValue = TestEnum.One;
private System.Drawing.Color _colorValue =
System.Drawing.Color.Red;
public int IntegerValue
{
get {return _integerValue;}
set {_integerValue = value;}
}
public string StringValue
{
get {return _stringValue;}
set {_stringValue = value;}
}
public bool BooleanValue
{
get {return _booleanValue;}
set {_booleanValue = value;}
}
public TestEnum EnumValue
{
get {return _enumValue;}
set {_enumValue = value;}
}
public System.Drawing.Color ColorValue
{
get {return _colorValue;}
set {_colorValue = value;}
}
}
}}
このTestClassオブジェクトを次のようにしてPropertyGrid.Sel...
#code(vbnet){{
Dim cls As New TestClass
PropertyGrid1.SelectedObject = cls
}}
#code(csharp){{
TestClass cls = new TestClass();
PropertyGrid1.SelectedObject = cls;
}}
何もしなくてもPropertyGridコントロールでは、プロパティの...
それ以外では、Color型のプロパティではリストにより色の選択...
***プロパティの表示方法を変更する [#p77a5e06]
このようにPropertyGridコントロールは実に簡単に使えますの...
ここから以下は、特にプロパティの表示方法に関するテクニッ...
using System.ComponentModel;
VB.NETでは
Imports System.ComponentModel
が宣言されているものとします。
***プロパティのデフォルト値を指定する [#v5f2741c]
VS.NETの場合、プロパティがデフォルト値(規定値)でないと...
次の例では、IntegerValueプロパティのデフォルト値を0にして...
#code(vbnet){{
<DefaultValue(0)> _
Public Property IntegerValue() As Integer
Get
Return _integerValue
End Get
Set(ByVal Value As Integer)
_integerValue = Value
End Set
End Property
}}
#code(csharp){{
[DefaultValue(0)]
public int IntegerValue
{
get {return _integerValue;}
set {_integerValue = value;}
}
}}
ところで、Color型、Font型、Size型などのデフォルト値は上記...
-[[PropertyDescriptor.ShouldSerializeValue メソッド>http:...
次の例では、ColorValueプロパティのデフォルト値をColor.Red...
#code(vbnet){{
Private _colorValue As System.Drawing.Color = _
System.Drawing.Color.Red
Public Property ColorValue() As System.Drawing.Color
Get
Return _colorValue
End Get
Set(ByVal Value As System.Drawing.Color)
_colorValue = Value
End Set
End Property
Private Function ShouldSerializeColorValue() As Boolean
Return Not ColorValue.Equals(System.Drawing.Color.Red)
End Function
}}
#code(csharp){{
private System.Drawing.Color _colorValue =
System.Drawing.Color.Red;
public System.Drawing.Color ColorValue
{
get {return _colorValue;}
set {_colorValue = value;}
}
private bool ShouldSerializeColorValue()
{
return ColorValue != System.Drawing.Color.Red;
}
}}
***クラスのデフォルトプロパティを指定する [#x2b4780d]
PropertyGridコントロールで一番初めに選択されるプロパティ...
次の例では、"TestClass"クラスのデフォルトプロパティを"Str...
#code(vbnet){{
<DefaultProperty("StringValue")> _
Public Class TestClass
'(省略)
End Class
}}
#code(csharp){{
[DefaultProperty("StringValue")]
public class TestClass
{
//(省略)
}
}}
***プロパティの説明を表示する [#ld1f6ff8]
PropertyGridコントロールの説明ペインに、選択されているプ...
次の例では、StringValueプロパティの説明を設定しています。
#code(vbnet){{
<Description("ここにStringValueの説明を書きます。")> _
Public Property StringValue() As String
Get
Return _stringValue
End Get
Set(ByVal Value As String)
_stringValue = Value
End Set
End Property
}}
#code(csharp){{
[Description("ここにStringValueの説明を書きます。")]
public string StringValue
{
get {return _stringValue;}
set {_stringValue = value;}
}
}}
***プロパティの項目を指定する [#h0d542cd]
PropertyGridコントロールではプロパティを項目(カテゴリ)別...
次の例では、ColorValueプロパティの項目を"表示"にしていま...
#code(vbnet){{
<Category("表示")> _
Public Property ColorValue() As System.Drawing.Color
Get
Return _colorValue
End Get
Set(ByVal Value As System.Drawing.Color)
_colorValue = Value
End Set
End Property
}}
#code(csharp){{
[Category("表示")]
public System.Drawing.Color ColorValue
{
get {return _colorValue;}
set {_colorValue = value;}
}
}}
***プロパティを表示しない [#cc46e292]
PropertyGridコントロールに表示したくないプロパティには、F...
次の例では、BooleanValueプロパティをPropertyGridコントロ...
#code(vbnet){{
<Browsable(False)> _
Public Property BooleanValue() As Boolean
Get
Return _booleanValue
End Get
Set(ByVal Value As Boolean)
_booleanValue = Value
End Set
End Property
}}
#code(csharp){{
[Browsable(false)]
public bool BooleanValue
{
get {return _booleanValue;}
set {_booleanValue = value;}
}
}}
***プロパティの値が編集できないようにする [#a57e5c9d]
プロパティの値をユーザーが編集できないようにするには、Tru...
次の例では、IntegerValueプロパティをPropertyGridコントロ...
#code(vbnet){{
<ReadOnlyAttribute(True)> _
Public Property IntegerValue() As Integer
Get
Return _integerValue
End Get
Set(ByVal Value As Integer)
_integerValue = Value
End Set
End Property
}}
#code(csharp){{
[ReadOnly(true)]
public int IntegerValue
{
get {return _integerValue;}
set {_integerValue = value;}
}
}}
***「ファイルを開く」ダイアログを表示してファイルを選択で...
右側にボタンを表示し、このボタンをクリックすることにより...
次の例では、StringValueプロパティにボタンを表示し、「ファ...
#code(vbnet){{
<Editor(GetType(System.Windows.Forms.Design.FileNameEdito...
GetType(System.Drawing.Design.UITypeEditor))> _
Public Property StringValue() As String
Get
Return _stringValue
End Get
Set(ByVal Value As String)
_stringValue = Value
End Set
End Property
}}
#code(csharp){{
[Editor(typeof(System.Windows.Forms.Design.FileNameEditor),
typeof(System.Drawing.Design.UITypeEditor))]
public string StringValue
{
get {return _stringValue;}
set {_stringValue = value;}
}
}}
***プロパティを展開できるようにする [#i7883981]
例えば次のようなSize型のプロパティは、PropertyGridコント...
#code(vbnet){{
Public Class TestClass
Private _size As New Size(10, 10)
Public Property Size() As Size
Get
Return _size
End Get
Set(ByVal Value As Size)
_size = value
End Set
End Property
End Class
}}
#code(csharp){{
public class TestClass
{
private Size _size = new Size(10, 10);
public Size Size
{
get {return _size;}
set {_size = value;}
}
}
}}
しかし次のような自作のクラスの場合は、展開ができず、値を...
#code(vbnet){{
Public Class CustomClass
Private _number As Integer = 0
Private _message As String = "hello"
Public Property Number() As Integer
Get
Return _number
End Get
Set(ByVal Value As Integer)
_number = Value
End Set
End Property
Public Property Message() As String
Get
Return _message
End Get
Set(ByVal Value As String)
_message = Value
End Set
End Property
End Class
Public Class TestClass
Private _custom As New CustomClass
Public Property Custom() As CustomClass
Get
Return _custom
End Get
Set(ByVal Value As CustomClass)
_custom = Value
End Set
End Property
End Class
}}
#code(csharp){{
public class CustomClass
{
private int _number = 0;
private string _message = "hello";
public int Number
{
get {return _number;}
set {_number = value;}
}
public string Message
{
get {return _message;}
set {_message = value;}
}
}
public class TestClass
{
private CustomClass _custom = new CustomClass();
public CustomClass Custom
{
get {return _custom;}
set {_custom = value;}
}
}
}}
このような自作のクラス型のプロパティでもPropertyGridコン...
#code(vbnet){{
<TypeConverter(GetType(ExpandableObjectConverter))> _
Public Class CustomClass
'(省略)
End Class
}}
#code(csharp){{
[TypeConverter(typeof(ExpandableObjectConverter))]
public class CustomClass
{
//(省略)
}
}}
これで"CustomClass"型のプロパティが展開できるようになりま...
次のような型コンバータクラスを使用することにより、CustomC...
#code(vbnet){{
Public Class CustomClassConverter
Inherits ExpandableObjectConverter
'コンバータがオブジェクトを指定した型に変換できるか
'(変換できる時はTrueを返す)
'ここでは、CustomClass型のオブジェクトには変換可能と...
Public Overloads Overrides Function CanConvertTo( _
ByVal context As ITypeDescriptorContext, _
ByVal destinationType As Type) As Boolean
If destinationType Is GetType(CustomClass) Then
Return True
End If
Return MyBase.CanConvertTo(context, destinationTy...
End Function
'指定した値オブジェクトを、指定した型に変換する
'CustomClass型のオブジェクトをString型に変換する方法...
Public Overloads Overrides Function ConvertTo( _
ByVal context As ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo...
ByVal value As Object, _
ByVal destinationType As Type) As Object
If destinationType Is GetType(String) And TypeOf ...
Dim cc As CustomClass = CType(value, CustomCl...
Return cc.Number.ToString() + "," + cc.Message
End If
Return MyBase.ConvertTo(context, culture, value, ...
End Function
'コンバータが特定の型のオブジェクトをコンバータの型に...
'(変換できる時はTrueを返す)
'ここでは、String型のオブジェクトなら変換可能とする
Public Overloads Overrides Function CanConvertFrom( _
ByVal context As ITypeDescriptorContext, _
ByVal sourceType As Type) As Boolean
If sourceType Is GetType(String) Then
Return True
End If
Return MyBase.CanConvertFrom(context, sourceType)
End Function
'指定した値をコンバータの型に変換する
'String型のオブジェクトをCustomClass型に変換する方法...
Public Overloads Overrides Function ConvertFrom( _
ByVal context As ITypeDescriptorContext, _
ByVal culture As System.Globalization.CultureInfo...
ByVal value As Object) As Object
If TypeOf value Is String Then
Dim ss As String() = value.ToString().Split(N...
Dim cc As New CustomClass
cc.Number = Integer.Parse(ss(0))
cc.Message = ss(1)
Return cc
End If
Return MyBase.ConvertFrom(context, culture, value)
End Function
End Class
}}
#code(csharp){{
public class CustomClassConverter : ExpandableObjectConve...
{
//コンバータがオブジェクトを指定した型に変換できるか
//(変換できる時はTrueを返す)
//ここでは、CustomClass型のオブジェクトには変換可能と...
public override bool CanConvertTo(
ITypeDescriptorContext context, Type destinationT...
{
if (destinationType == typeof(CustomClass))
return true;
return base.CanConvertTo(context, destinationType);
}
//指定した値オブジェクトを、指定した型に変換する
//CustomClass型のオブジェクトをString型に変換する方法...
public override object ConvertTo(ITypeDescriptorConte...
CultureInfo culture, object value, Type destinati...
{
if (destinationType == typeof(string) &&
value is CustomClass)
{
CustomClass cc = (CustomClass) value;
return cc.Number.ToString() + "," + cc.Message;
}
return base.ConvertTo(context, culture, value, de...
}
//コンバータが特定の型のオブジェクトをコンバータの型...
//(変換できる時はTrueを返す)
//ここでは、String型のオブジェクトなら変換可能とする
public override bool CanConvertFrom(
ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom (context, sourceType);
}
//指定した値をコンバータの型に変換する
//String型のオブジェクトをCustomClass型に変換する方法...
public override object ConvertFrom(ITypeDescriptorCon...
CultureInfo culture, object value)
{
if (value is string)
{
string[] ss = value.ToString().Split(new char...
CustomClass cc = new CustomClass();
cc.Number = int.Parse(ss[0]);
cc.Message = ss[1];
return cc;
}
return base.ConvertFrom(context, culture, value);
}
}
}}
使い方は前と同じです。
#code(vbnet){{
<TypeConverter(GetType(CustomClassConverter))> _
Public Class CustomClass
'(省略)
End Class
}}
#code(csharp){{
[TypeConverter(typeof(CustomClassConverter))]
public class CustomClass
{
//(省略)
}
}}
***もっと勉強したい方は... [#f77594a6]
MSDNにある「.NET Framework の PropertyGrid コントロールの...
参考:
-[[.NET Framework の PropertyGrid コントロールの高度な活...
-[[Visual Studio .NET プロパティ ブラウザによるコンポーネ...
**コメント [#f494856a]
#comment
//これより下は編集しないでください
#pageinfo([[:Category/.NET]],2004-05-04 (火) 06:00:00,DOB...
ページ名:
▲
▼
[
トップ
] [
新規
|
子ページ作成
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]