どっとねっとふぁん

C# Tips And Samples
in 検索

07.テキストボックスを追加する

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

    07.テキストボックスを追加する

    テキストボックスの内容をメッセージウィンドウに表示するサンプルです。
    -------------------------------------------------------
    using System;
    using System.Windows.Forms;
    
    class WinSample : Form
    {
        private Button mybutton;
        private TextBox mytextbox;
    
        public static void Main()
        {
            Application.Run(new WinSample());
        }
    
        public WinSample()
        {
            this.mybutton = new Button();
            this.mytextbox = new TextBox();
    
            this.SuspendLayout();
    
            this.Text = "Hello World!";
            // Enterキーにボタンを関連づける
            this.AcceptButton = this.mybutton;
    
            this.mybutton.Location = new System.Drawing.Point(200, 200);
            this.mybutton.Text = "入力確認";
            this.mybutton.Click += new EventHandler(this.mb_Click);
    
            // テキストボックスの表示位置設定
            this.mytextbox.Location = new System.Drawing.Point(100, 50);
    
            // テキストボックスとボタンをフォームのコントロールに追加
            this.Controls.AddRange(new Control[] { this.mytextbox, this.mybutton});
    
            this.ResumeLayout(false);
        }
    
        private void mb_Click(object sender, EventArgs e)
        {
            // テキストボックスの内容をメッセージボックスに表示
            MessageBox.Show(this.mytextbox.Text);
        }
    }
    
    -------------------------------------------------------
    テキストボックスに入力した後でEnterキーを押すと、入力確認ボタンを押したときと同じように動作しますが、これはAcceptButtonプロパティにボタンを設定しているためです。
    また、複数のコントロールを追加するのにAddRangeメソッドを利用しています。


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