どっとねっとふぁん

C# Tips And Samples
in 検索

11.モードレスダイアログボックスを作成する

最新の投稿は、投稿日時: 06-24-2005, 11:20 午前 投稿者: ono です。スレッドには 0 件の返答があります。
投稿のソート: 前へ 次へ
  •  06-24-2005, 11:20 午前 83

    11.モードレスダイアログボックスを作成する

    モードレスなダイアログボックスを作成する場合、ベースとなるWindowをダイアログWindowのオーナーとするのがよいでしょう。
    この場合、ダイアログのほうは必ずベースとなるWindowより前面に表示されますが、ダイアログを表示したままでベースのWindowを操作することが可能です。

    以下のサンプルでは、ベースWindowとダイアログWindowの間でデータをやりとりできるようにしています。
    -------------------------------------------------------
    using System;
    using System.Windows.Forms;
    
    class WinSample : Form
    {
        private Button mybutton;
        private TextBox myTextBox;
    
        public string DispText
        {
            get { return this.myTextBox.Text; }
            set { this.myTextBox.Text = value; }
        }
    
        public static void Main()
        {
            Application.Run(new WinSample());
        }
    
        public WinSample()
        {
            this.Text = "オーナーウィンドウ";
    
            this.myTextBox = new TextBox();
            this.myTextBox.Location = new System.Drawing.Point(100, 100);
            this.Controls.Add(myTextBox);
    
            this.mybutton = new Button();
            this.mybutton.Location = new System.Drawing.Point(200, 200);
            this.mybutton.Text = "子ウィンドウ";
            this.mybutton.Click += new EventHandler(this.button_Click);
            this.Controls.Add(mybutton);
        }
    
        private void button_Click(object sender, EventArgs e)
        {
            // 子ウィンドウの生成
            WinSample2 ws2 = new WinSample2();
            // 子ウィンドウのオーナーを設定
            ws2.Owner = this;
            // 子ウィンドウにデータを設定
            ws2.DispText2 = myTextBox.Text;
            // 子ウィンドウの表示
            ws2.Show();
        }
    }
    
    class WinSample2 : Form
    {
        private Button mybutton2;
        private Button mybutton3;
        private TextBox myTextBox2;
    
        public string DispText2
        {
            set { this.myTextBox2.Text = value; }
        }
    
        public WinSample2()
        {
            this.Text = "子ウィンドウ";
    
            this.myTextBox2 = new TextBox();
            this.myTextBox2.Location = new System.Drawing.Point(100, 100);
            this.Controls.Add(myTextBox2);
    
            this.mybutton2 = new Button();
            this.mybutton2.Location = new System.Drawing.Point(200, 180);
            this.mybutton2.Text = "親から取得";
            this.mybutton2.Click += new EventHandler(this.button2_Click);
            this.Controls.Add(mybutton2);
    
            this.mybutton3 = new Button();
            this.mybutton3.Location = new System.Drawing.Point(200, 230);
            this.mybutton3.Text = "親へ設定";
            this.mybutton3.Click += new EventHandler(this.button3_Click);
            this.Controls.Add(mybutton3);
        }
    
        private void button2_Click(object sender, EventArgs e)
        {
            // 親ウィンドウからデータを取り出す
            this.myTextBox2.Text = ((WinSample)this.Owner).DispText;
        }
    
        private void button3_Click(object sender, EventArgs e)
        {
            // 親ウィンドウにデータを設定
            ((WinSample)this.Owner).DispText = this.myTextBox2.Text;
        }
    }
    
    -------------------------------------------------------

    クラス間のデータの受け渡しにはできるだけプロパティを利用したほうがよいでしょう。
    クラスの内部の実装が変わったときに、その変更がなかったかのように見せかけることができます。
    たとえば、上記のサンプルでWinSample側のTextBoxをLabelに変更したとしても、WinSample2側にまったく手を加えずに動作させることが可能です。
    WinSampleとWinSample2を別アセンブリとしてコンパイルしていた場合、WinSample2側は再コンパイルもせずに利用できることになります。


    あおい情報システム株式会社
     小野修司(どっとねっとふぁん)
RSS ニュースフィードで参照
SkinName:iroha_CS2
Powered by Community Server, by Telligent Systems