C# pictureBox array questions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • robusto33
    New Member
    • May 2007
    • 1

    C# pictureBox array questions

    Hi everyone, I'm really new to C#.net development, especially for win32 applications. I'm basically making a board game and was wondering if anyone could help me out with this predicament:

    I have a dynamically created array based on the size of the Board (13x13 or 19x19). I can make the array fine and position the pictureBoxes over the background, but I want to be able to change the properties of all the pictureBoxes based on a Click event. Is there any way to define a universal click event for all the pictureboxes in the array that would only modify the clicked picturebox? I'm trying to make them visible and assigning an image path upon firing the click event.

    Please help, my brain is totally fried.


    Here's sample code:


    Code:
    public void createBoardArray(int numOfPieces)
            {
                Point gamePieceStart = new Point(3, 5);
                Point loadingLoc = new Point(120, 140);
                PictureBox loadingPic = new PictureBox();
                 
                gameBoard = new PictureBox[numOfPieces, numOfPieces];
    
    
                //initializing array of images
                for(int g = 0; g < numOfPieces; ++g)
                {
    
                    for(int h = 0; h < numOfPieces; ++h)
                    {
                        gameBoard[g,h] = new PictureBox();
                        
                        gameBoard[g, h].BackColor = System.Drawing.Color.Transparent;
                        gameBoard[g, h].InitialImage = null;
                        gameBoard[g, h].ErrorImage = null;
                        gameBoard[g, h].Width = 28;
                        gameBoard[g, h].Height = 26;
    
                        //adding click event
                        gameBoard[g, h].Click += new EventHandler(picBox_OnClick);
    
                        gameBoard[g, h].Location = gamePieceStart;
    
                       //test image assignment
                       // gameBoard[g, h].ImageLocation = Path.Combine(Environment.CurrentDirectory, @"bin\blackPiece3.gif");
    
                        gameBoard[g, h].Enabled = true;
                        gameBoard[g, h].Visible = false;
    
                        gamePanel.Controls.Add(gameBoard[g, h]);
    
                        gamePieceStart.Y += 31;
                    }
    
                    gamePieceStart.X += 31;
                    gamePieceStart.Y = 5;
                    
                }
       
            }
    Last edited by Frinavale; Feb 5 '09, 05:25 PM. Reason: Moved to C# Answers from .NET and changed italic font tags to code tags
  • shidec
    New Member
    • May 2007
    • 26

    #2
    Use sender arguments from event.
    This code make 5 PictureBox and (only) change color if mouse on the object.

    [CODE=vbnet]
    using System;
    using System.Collecti ons.Generic;
    using System.Componen tModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows. Forms;

    namespace Puzzle
    {
    public partial class Form1 : Form
    {

    private PictureBox[] pictureBox;
    public Form1()
    {
    InitializeCompo nent();
    pictureBox = new PictureBox[3];
    for(int i=0;i<3;i++)
    {
    pictureBox[i]= new PictureBox();
    pictureBox[i].Left = i * 50;
    pictureBox[i].Top = 10;
    pictureBox[i].Width = 40;
    pictureBox[i].Height = 40;
    pictureBox[i].BackColor = Color.Aqua;
    pictureBox[i].MouseLeave += new System.EventHan dler(this.pictu reBox_MouseLeav e);
    pictureBox[i].MouseHover += new System.EventHan dler(this.pictu reBox_MouseHove r);
    this.Controls.A dd(pictureBox[i]);
    }
    }

    private void pictureBox_Mous eHover(object sender, EventArgs e)
    {
    PictureBox pb;
    pb = (PictureBox)sen der;
    pb.BackColor = Color.BlueViole t;
    }

    private void pictureBox_Mous eLeave(object sender, EventArgs e)
    {
    PictureBox pb;
    pb = (PictureBox)sen der;
    pb.BackColor = Color.Aqua;
    }
    }
    }
    [/CODE]

    Comment

    • shidec
      New Member
      • May 2007
      • 26

      #3
      Correction: that program make 3 Picture Box,
      to make 5, just change 3 -> 5

      Comment

      • vanc
        Recognized Expert New Member
        • Mar 2007
        • 211

        #4
        I think you can create just one click event for all of those picture boxes, but you should check for the sender's name to clarify which picture box is the sender, and make your move.

        cheers.

        Comment

        • IceRaider
          New Member
          • Jun 2007
          • 1

          #5
          great code guys, it was just what i need. by the way if in each square i want to put the number of it? kinda like a label, how should i do it?


          thx

          Comment

          • mod4c
            New Member
            • Feb 2009
            • 1

            #6
            remove picturebox

            If I wanted to remove the last 2 picture boxes with a button click, should the visible tag work to not display them or does the drawn boxes stay unless I do a remove or something similar?

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Couple things

              A) I would probably make a custom picture box class that handles the click and other events. Put the burden on the individual pieces instead of the program trying to manage them all.
              B) referencing graphics in your \bin\ directory works while debugging but you don't have one of these directories when you build an installer and install the finished program on other PC. You probably want to take a look at how embedded resources work.
              Path.Combine(En vironment.Curre ntDirectory, @"bin\blackPiec e3.gif");

              Comment

              Working...