どっとねっとふぁん

C# Tips And Samples
in 検索

20.Tagプロパティを利用する

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

    20.Tagプロパティを利用する

    Windowsアプリケーションに追加できるコントロールはTagプロパティを持っています。
    このTagプロパティにはどんなオブジェクトでも格納できます。
    例えば、ラベルコントロールとボタンコントロールを関連づけて扱いたい、といった場合にTagプロパティを利用すると便利です。
    -------------------------------------------------------
    using System;
    using System.Windows.Forms;
    
    class WinSample : Form
    {
        public static void Main()
        {
            Application.Run(new WinSample());
        }
    
        public WinSample()
        {
            for(int i = 0; i <= 2; i++)
            {
                Label mylabel = new Label();
                mylabel.Location = new System.Drawing.Point(80, 100 + i * 50);
                mylabel.Text = "ラベル" + i.ToString();
                mylabel.Size = new System.Drawing.Size(50, 25);
                this.Controls.Add(mylabel);
    
                Button mybutton = new Button();
                mybutton.Location = new System.Drawing.Point(150, 100 + i * 50);
                mybutton.Text = "ボタン" + i.ToString();
                // Tagプロパティに関連するオブジェクトを格納する
                mybutton.Tag = mylabel;
                mybutton.Click += new EventHandler(this.mb_Click);
                this.Controls.Add(mybutton);
            }
        }
    
        private void mb_Click(object sender, EventArgs e)
        {
            MessageBox.Show(((Label)((Button)sender).Tag).Text + "が選ばれました");
        }
    }
    
    -------------------------------------------------------


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