CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2011
    Posts
    5

    2D PictureBox Array

    Hi,

    Im working on an app that displays pictureboxes dynamically through a 2d array. I have it working but I am trying to find the value of a selected picturebox using a click event by displaying it through a messagebox. I cannot figure out how to do this. Please help if you can.

    My code below:

    public partial class Form1 : Form
    {
    private PictureBox[,] pictureBox;

    private void displayImageBoxArray()
    {
    pictureBox = new PictureBox[3, 3];
    for (int i = 0; i < 3; i++)
    {
    for (int j = 0; j < 3; j++)
    {
    //string test = pictureBox[i.ToString(), j.ToString()];
    pictureBox[i, j] = new PictureBox();
    pictureBox[i, j].Left = i * 50;
    pictureBox[i, j].Top = j * 50;
    pictureBox[i, j].Width = 40;
    pictureBox[i, j].Height = 40;
    pictureBox[i, j].BackColor = Color.Black;
    pictureBox[i, j].Click += new EventHandler(ctrlClick);
    this.Controls.Add(pictureBox[i, j]);
    //MessageBox.Show(pictureBox[i, j.ToString());
    }
    }
    }

    private void ctrlClick(System.Object sender, EventArgs e)
    {
    Control ctrl = (Control)sender;
    MessageBox.Show("The index of pictureBox???");
    }

    public Form1()
    {
    InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
    displayImageBoxArray();
    }
    }

  2. #2
    Join Date
    Jan 2011
    Posts
    5

    Re: 2D PictureBox Array

    I got it working another way. I inserted

    pictureBox[i, j].Name = i.ToString() + j.ToString();

    then displayed it

    MessageBox.Show(pictureBox[i, j].Name);

    Is there another way of finding out the index value?

  3. #3
    Join Date
    Jan 2011
    Posts
    5

    Re: 2D PictureBox Array

    Another quick question....
    How can I get the i and j values and pass them to my ctrlClick method?

  4. #4
    Join Date
    Jan 2011
    Posts
    5

    Re: 2D PictureBox Array

    Got it.
    MessageBox.Show(ctrl.Name);

    thanks

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured