何となく Blog by Jitta
Microsoft .NET 考

目次

Blog 利用状況
  • 投稿数 - 761
  • 記事 - 18
  • コメント - 35955
  • トラックバック - 222
ニュース
  • IE7以前では、表示がおかしい。div の解釈に問題があるようだ。
    IE8の場合は、「互換」表示を OFF にしてください。
  • 検索エンジンで来られた方へ:
    お望みの情報は見つかりましたか? よろしければ、コメント欄にどのような情報を探していたのか、ご記入ください。
It's ME!
  • はなおか じった
  • 世界遺産の近くに住んでます。
  • Microsoft MVP for Visual Developer ASP/ASP.NET 10, 2004 - 9, 2011
広告

記事カテゴリ

書庫

日記カテゴリ

ギャラリ

その他

わんくま同盟

同郷

 

とりあえず、ネタとして。

細かいところは見ていない。

コントロール本体
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace WankumaControls
{
    public delegate void ItemChangedNotifyDelegate(object sender, ItemChangedNotifyEventArgs e);
    /// <summary>
    /// UserControl1 の概要の説明です。
    /// </summary>
    public class LinkedDropDownList : System.Windows.Forms.ComboBox
    {
        /// <summary>
        /// 必要なデザイナ変数です。
        /// </summary>
        private System.ComponentModel.Container components = null;

        public LinkedDropDownList()
        {
            // この呼び出しは、Windows.Forms フォーム デザイナで必要です。
            InitializeComponent();

            // InitComponent 呼び出しの後に初期化処理を追加してください。
            base.DropDownStyle = ComboBoxStyle.DropDownList;
            this.affectedControl = null;
        }

        public new ComboBoxStyle DropDownStyle {
            get {
                return ComboBoxStyle.DropDownList;
            }
        }

        private LinkedDropDownList affectedControl;
        /// <summary>
        /// 変更の影響を受けるコントロールを取得、または設定します。
        /// </summary>
        [System.ComponentModel.BrowsableAttribute(true)]
        public LinkedDropDownList AffectedControl {
            get {
                return this.affectedControl;
            }
            set {
                this.affectedControl = value;
            }
        }

        [System.ComponentModel.Browsable(true)]
        public event ItemChangedNotifyDelegate ItemChangedNotify;
        protected virtual void OnItemChangedNotify(ItemChangedNotifyEventArgs e) {
            if (this.ItemChangedNotify != null) {
                this.ItemChangedNotify(this, e);
            }
        }
        protected override void OnSelectedIndexChanged(EventArgs e) {
            base.OnSelectedIndexChanged (e);
            if (this.affectedControl != null) {
                this.affectedControl.OnItemChangedNotify(new ItemChangedNotifyEventArgs(this.SelectedItem));
            }
        }

        /// <summary>
        /// 使用されているリソースに後処理を実行します。
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if( components != null )
                    components.Dispose();
            }
            base.Dispose( disposing );
        }

        #region コンポーネント デザイナで生成されたコード
        /// <summary>
        /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
        /// コード]エディタで変更しないでください。
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }
        #endregion

    }
}
イベント引数
using System;

namespace WankumaControls
{
    /// <summary>
    /// ParentChangedEventArgs の概要の説明です。
    /// </summary>
    public class ItemChangedNotifyEventArgs : EventArgs
    {
        public ItemChangedNotifyEventArgs(object item) : base() {
            this.selectedItem = item;
        }

        private object selectedItem;
        public object SelectedItem {
            get {
                return selectedItem;
            }
        }
    }
}
使用例
        public Form1() {
            //
            // Windows フォーム デザイナ サポートに必要です。
            //
            InitializeComponent();

            //
            // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
            //
            source = new DataTable();
            source.Columns.Add("parent", typeof(int));
            source.Columns.Add("code", typeof(int));
            source.Columns.Add("name", typeof(string));
            source.Rows.Add(new object[] {0, 1, "北海道"});
            source.Rows.Add(new object[] {0, 2, "青森県"});
            source.Rows.Add(new object[] {1, 1, "小樽市"});
            source.Rows.Add(new object[] {1, 2, "札幌市"});
            source.Rows.Add(new object[] {2, 1, "青森県"});
            source.Rows.Add(new object[] {2, 2, "十和田市"});
            source.AcceptChanges();
            DataView view = source.DefaultView;
            view.RowFilter = "[parent]=0";

            this.linkedDropDownList1.DataSource = view;
            this.linkedDropDownList1.ValueMember = "code";
            this.linkedDropDownList1.DisplayMember = "name";
        }

        #region Windows フォーム デザイナで生成されたコード
        /// <summary>
        /// デザイナ サポートに必要なメソッドです。このメソッドの内容を
        /// コード エディタで変更しないでください。
        /// </summary>
        private void InitializeComponent() {
            途中省略
            //
            // linkedDropDownList1
            //
            this.linkedDropDownList1.AffectedControl = this.linkedDropDownList2;
            this.linkedDropDownList1.Location = new System.Drawing.Point(32, 8);
            this.linkedDropDownList1.Name = "linkedDropDownList1";
            this.linkedDropDownList1.Size = new System.Drawing.Size(256, 20);
            this.linkedDropDownList1.TabIndex = 5;
            //
            // linkedDropDownList2
            //
            this.linkedDropDownList2.AffectedControl = null;
            this.linkedDropDownList2.Location = new System.Drawing.Point(48, 32);
            this.linkedDropDownList2.Name = "linkedDropDownList2";
            this.linkedDropDownList2.Size = new System.Drawing.Size(240, 20);
            this.linkedDropDownList2.TabIndex = 6;
            this.linkedDropDownList2.ItemChangedNotify += new WankumaControls.ItemChangedNotifyDelegate(this.linkedDropDownList2_ItemChangedNotify);
            途中省略
        }

        private void linkedDropDownList2_ItemChangedNotify(object sender, WankumaControls.ItemChangedNotifyEventArgs e) {
            DataView view = new DataView(source);
            DataRowView row = e.SelectedItem as DataRowView;
            if (row != null) {
                int proof = int.Parse(row["code"].ToString());
                view.RowFilter = string.Format("[parent]={0}", proof);
                this.linkedDropDownList2.DataSource = view;
                this.linkedDropDownList2.ValueMember = "code";
                this.linkedDropDownList2.DisplayMember = "name";
            }
        }

投稿日時 : 2007年6月13日 14:44
コメント
  • # re: LinkedDropDownList
    Jitta
    Posted @ 2007/06/13 14:45
    IE6きらい(--;
  • # re: 部品化する場合としない場合
    R.Tanaka.Ichiro's Blog
    Posted @ 2007/06/13 21:03
    re: 部品化する場合としない場合
タイトル
名前
Url
コメント