ピクチャーボックスをクリックすると名前が出てきますのでやってみてください。
piturebox1,piturebox2,piturebox3,piturebox4,iturebox5,piturebox6 次の行に行ってピクチャーボックスをクリックすると
piturebox3,piturebox4,iturebox9,piturebox10,iturebox11,piturebox12
のようになってしまいます。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace map
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//ボタンコントロール配列のフィールドを作成
private PictureBox[] pictures = new PictureBox[0];
private void Form1_Load(object sender, EventArgs e)
{
create_picturebox();
}
private void Form_ResizeEnd(object sender, EventArgs e)
{
}
private void create_picturebox()
{
// フォームのクライアント領域のサイズを取得する
System.Drawing.Size tSize = this.ClientSize;
int Height = Size.Height / 255 + 1;
int Width = Size.Width / 255 + 1;
int Pic_count = 0;
// 取得したフォームのクライアント領域のサイズを表示する
//MessageBox.Show(tSize.ToString());
for (int n1 = 1; n1 <= Height; n1++)
{
for (int n2 = 1; n2 <= Width; n2++)
{
Pic_count++;
Array.Resize(ref this.pictures, Pic_count + 1);
this.pictures[Pic_count] = new System.Windows.Forms.PictureBox();
this.pictures[Pic_count].Location = new System.Drawing.Point((n2 - 1) * 255, (n1 - 1) * 255);
this.pictures[Pic_count].Name = "pictureBox" + Pic_count.ToString();
this.pictures[Pic_count].Size = new System.Drawing.Size(255, 255);
this.pictures[Pic_count].TabIndex = Pic_count;
this.pictures[Pic_count].TabStop = true;
this.pictures[Pic_count].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
//↓コレがないとだめ
this.pictures[Pic_count].Click += new System.EventHandler(this.pictureBox_Click);
this.Controls.Add(this.pictures[Pic_count]);
this.pictures[Pic_count].Show();
}
}
}
private void pictureBox_Click(object sender, EventArgs e)
{
//senderはクリックされたピクチャボックスのどれか
PictureBox pict = sender as PictureBox;
MessageBox.Show(pict.Name);
}
private void button1_Click(object sender, EventArgs e)
{
create_picturebox();
}
}
}