架空の映画のネタバレレビュー

C#でのコントロール配列について

ピクチャーボックスを配列にして動的生成しています
(日本語おかしい…?)

そこで質問です。
あるピクチャーボックスをクリックすると
クリックされたピクチャーボックスの値(?)とかを取得
(たとえば、Nameとか)

1つだけの場合なら、イベントのClickでできたんですが
配列にすると、どうすればいいのかわからなくなりました


わかりやすいサイトとかあったら教えてください!

A 回答 (1件)

 こんにちは。


 ピクチャボックスを作成する際にクリックイベントを登録した後、クリックイベントのsenderで判断すれば良いのでは。
 雑ではありますが、以下参考程度に。

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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
private PictureBox[] pictures = new PictureBox[0];

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
for (int n = 0; n < 16; ++n)
{
Array.Resize(ref this.pictures, n + 1);
this.pictures[n] = new System.Windows.Forms.PictureBox();
this.pictures[n].Location = new System.Drawing.Point((n % 4) * 32, (n / 4) * 32);
this.pictures[n].Name = "pictureBox" + n.ToString();
this.pictures[n].Size = new System.Drawing.Size(32, 32);
this.pictures[n].TabIndex = n;
this.pictures[n].TabStop = true;
this.pictures[n].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
//↓コレがないとだめ
this.pictures[n].Click += new System.EventHandler(this.pictureBox_Click);
this.Controls.Add(this.pictures[n]);
this.pictures[n].Show();
}
}

private void pictureBox_Click(object sender, EventArgs e)
{
//senderはクリックされたピクチャボックスのどれか
PictureBox pict = sender as PictureBox;
MessageBox.Show(pict.Name);
}
}
}
    • good
    • 0
この回答へのお礼

ありがとうございます!!
もう少し考えれば
自分がやりたい操作ができそうです!!

お礼日時:2010/05/06 15:46

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!