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 + "が選ばれました");
}
}
-------------------------------------------------------
あおい情報システム株式会社 小野修司(どっとねっとふぁん)