- PR -

コレクションエディタについて

1
投稿者投稿内容
まろ茶
会議室デビュー日: 2004/08/20
投稿数: 2
投稿日時: 2004-08-20 13:52
ユーザーコントロール作成時に、新規追加プロパティにて
コレクションエディタを表示させることは出来たのですが、
エディタの追加ボタンを押してメンバを登録する方法が
わかりません。

ToolBarコントロールのButtonsプロパティの様なことを
したいのですが・・・。
どなたか解かる方、サンプルの在りかだけでもいいので
よろしくお願いします。
えムナウ
大ベテラン
会議室デビュー日: 2004/06/10
投稿数: 187
お住まい・勤務地: 東京
投稿日時: 2004-08-20 23:27
ちょっと長いですが、こんなのはどうでしょうか?

コード:
<定義>
private ScheduleCollection		_ScheduleArray ;

<初期化>
_ScheduleArray = new ScheduleCollection() ;

<公開プロパティ>
[Description("スケジュールコレクションです。"),Category("設定")]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
[Browsable(true), Bindable(true)]
public ScheduleCollection ScheduleArray
{
	get 
	{
		return this._ScheduleArray ;
	}
	set
	{
		this._ScheduleArray = value ;
	}
}

<スケジュールクラス>
[Serializable()]
[TypeConverter(typeof(ScheduleConverter))]
public class Schedule 
{
	private DateTime	_start ;
	private TimeSpan	_span ;
	private String		_message ;

	public Schedule()
	{
		_start = DateTime.MinValue ;
		_span = new TimeSpan(0) ;
		_message = "" ;
	}

	public Schedule(DateTime start, TimeSpan span, String message)
	{
		_start = start ;
		_span = span ;
		_message = message ;
	}

	[Description("スケジュール開始日時です。"),Category("設定")]
	public DateTime StartTime 
	{
		get 
		{
			return this._start ;
		}
		set
		{
			this._start = value ;
		}
	}

	[Description("スケジュール期間です。"),Category("設定")]
	public TimeSpan TimeSpan 
	{
		get 
		{
			return this._span ;
		}
		set
		{
			this._span = value ;
		}
	}

	[Description("スケジュール内容です。"),Category("設定")]
	public String Message
	{
		get 
		{
			return this._message ;
		}
		set
		{
			this._message = value ;
		}
	}
}

<スケジュールクラスタイプコンバーター>
internal class ScheduleConverter : ExpandableObjectConverter 
{
	public override PropertyDescriptorCollection 
		GetProperties(ITypeDescriptorContext context, 
		object value, 
		Attribute[] filter)
	{
		return TypeDescriptor.GetProperties(value, filter);
	}

	public override bool GetPropertiesSupported(
		ITypeDescriptorContext context) 
	{
		return true;
	}

	public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) 
	{
		if (sourceType == typeof(string)) 
			return true;
		return base.CanConvertFrom(context, sourceType);
	}

	public override object ConvertFrom( ITypeDescriptorContext context, 
		System.Globalization.CultureInfo info, object value) 
	{
		if (value is string) 
		{
			try 
			{
				string[] v = ((string)value).Split(new char[] {','});
				return new Schedule( DateTime.Parse(v[0]), TimeSpan.Parse(v[1]), v[2] ) ;
			}
			catch
			{}
			throw new ArgumentException("Can not convert '" + (string)value + "' to type Schedule") ;
		}
		return base.ConvertFrom(context, info, value);
	}

	public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType) 
	{
		if (destinationType == typeof(InstanceDescriptor)) 
			return true;
		return base.CanConvertTo(context, destinationType);
	}

	public override object ConvertTo(ITypeDescriptorContext context, 
		System.Globalization.CultureInfo culture, object value, Type destType) 
	{
		if (destType == typeof(string) && value is Schedule) 
		{
			Schedule p = (Schedule)value;
			return p.StartTime.ToString("yyyy/MM/dd HH:mm:ss") + "," +
				p.TimeSpan.ToString() + "," + p.Message ;
		}
		if (destType == typeof(InstanceDescriptor) && value is Schedule) 
		{
			Schedule sc = (Schedule)value;
			System.Reflection.ConstructorInfo ctor = typeof(Schedule).GetConstructor(new Type[] {typeof(DateTime), typeof(TimeSpan), typeof(String)});
			if (ctor != null) 
			{
				return new InstanceDescriptor(ctor, new object[] {sc.StartTime,sc.TimeSpan,sc.Message});
			}
		}
		return base.ConvertTo(context, culture, value, destType);
	}   
}

<ScheduleCollectionクラス>
[Serializable()]
public class ScheduleCollection : CollectionBase  
{
	public Schedule this[ int index ]  
	{
		get  
		{
			return( (Schedule) List[index] );
		}
		set  
		{
			List[index] = value;
		}
	}

	public int Add( Schedule value )  
	{
		return( List.Add( value ) );
	}

	public int IndexOf( Schedule value )  
	{
		return( List.IndexOf( value ) );
	}

	public void Insert( int index, Schedule value )  
	{
		List.Insert( index, value );
	}

	public void Remove( Schedule value )  
	{
		List.Remove( value );
	}

	public bool Contains( Schedule value )  
	{
		// If value is not of type Schedule, this will return false.
		return( List.Contains( value ) );
	}

	protected override void OnInsert( int index, Object value )  
	{
		if ( value.GetType() != Type.GetType("WindowsControlLibrary1.Schedule") )
			throw new ArgumentException( "value must be of type Schedule. (" + value.GetType().ToString() + ")" , "value" );
	}

	protected override void OnRemove( int index, Object value )  
	{
		if ( value.GetType() != Type.GetType("WindowsControlLibrary1.Schedule") )
			throw new ArgumentException( "value must be of type Schedule. (" + value.GetType().ToString() + ")" , "value" );
	}

	protected override void OnSet( int index, Object oldValue, Object newValue )  
	{
		if ( newValue.GetType() != Type.GetType("WindowsControlLibrary1.Schedule") )
			throw new ArgumentException( "newValue must be of type Schedule. (" + newValue.GetType().ToString() + ")", "newValue" );
	}

	protected override void OnValidate( Object value )  
	{
		if ( value.GetType() != Type.GetType("WindowsControlLibrary1.Schedule") )
			throw new ArgumentException( "value must be of type Schedule. (" + value.GetType().ToString() + ")" );
	}
}


_________________
えムナウ Microsoft MVP for Visual Developer - C#,2005/01-2007/12
えムナウのプログラミングのページ Blog1 Blog2
えムナウ
大ベテラン
会議室デビュー日: 2004/06/10
投稿数: 187
お住まい・勤務地: 東京
投稿日時: 2004-08-21 00:49
こんな長いサンプルをあげるくらいだったらうちのサンプルに流しておけばよかった。
サンプルで出していたのを思い出しました。

スプレッドシート
サンプル7 列・行ごとにサイズを変更/一旦削除したブロック対応を復活
http://www.geocities.jp/mnow/cs_usercontrol12.html
の 「CellRange.cs スプレッドシートセル範囲クラス」を見たほうがよかったかもしれません。
使い方は SpreadControl.cs スプレッドシートコントロール本体 を検索しながら見てください。

コレクションエディタでスプレッドシートセル範囲を編集できます。

_________________
えムナウ Microsoft MVP for Visual Developer - C#,2005/01-2007/12
えムナウのプログラミングのページ Blog1 Blog2
まろ茶
会議室デビュー日: 2004/08/20
投稿数: 2
投稿日時: 2004-08-23 13:18
レスありがとうございました。
おかげさまで、光明を見出すことが出来そうです。

本当にありがとうございました!
1

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