473,395 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:


Expand|Select|Wrap|Line Numbers
  1. public void createBoardArray(int numOfPieces)
  2.         {
  3.             Point gamePieceStart = new Point(3, 5);
  4.             Point loadingLoc = new Point(120, 140);
  5.             PictureBox loadingPic = new PictureBox();
  6.  
  7.             gameBoard = new PictureBox[numOfPieces, numOfPieces];
  8.  
  9.  
  10.             //initializing array of images
  11.             for(int g = 0; g < numOfPieces; ++g)
  12.             {
  13.  
  14.                 for(int h = 0; h < numOfPieces; ++h)
  15.                 {
  16.                     gameBoard[g,h] = new PictureBox();
  17.  
  18.                     gameBoard[g, h].BackColor = System.Drawing.Color.Transparent;
  19.                     gameBoard[g, h].InitialImage = null;
  20.                     gameBoard[g, h].ErrorImage = null;
  21.                     gameBoard[g, h].Width = 28;
  22.                     gameBoard[g, h].Height = 26;
  23.  
  24.                     //adding click event
  25.                     gameBoard[g, h].Click += new EventHandler(picBox_OnClick);
  26.  
  27.                     gameBoard[g, h].Location = gamePieceStart;
  28.  
  29.                    //test image assignment
  30.                    // gameBoard[g, h].ImageLocation = Path.Combine(Environment.CurrentDirectory, @"bin\blackPiece3.gif");
  31.  
  32.                     gameBoard[g, h].Enabled = true;
  33.                     gameBoard[g, h].Visible = false;
  34.  
  35.                     gamePanel.Controls.Add(gameBoard[g, h]);
  36.  
  37.                     gamePieceStart.Y += 31;
  38.                 }
  39.  
  40.                 gamePieceStart.X += 31;
  41.                 gamePieceStart.Y = 5;
  42.  
  43.             }
  44.  
  45.         }
May 19 '07 #1
6 25741
shidec
26
Use sender arguments from event.
This code make 5 PictureBox and (only) change color if mouse on the object.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8.  
  9. namespace Puzzle
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.  
  14.         private PictureBox[] pictureBox;
  15.         public Form1()
  16.         {
  17.             InitializeComponent();
  18.             pictureBox = new PictureBox[3];
  19.             for(int i=0;i<3;i++)
  20.             {
  21.                 pictureBox[i]= new PictureBox();
  22.                 pictureBox[i].Left = i * 50;
  23.                 pictureBox[i].Top = 10;
  24.                 pictureBox[i].Width = 40;
  25.                 pictureBox[i].Height = 40;
  26.                 pictureBox[i].BackColor = Color.Aqua;
  27.                 pictureBox[i].MouseLeave += new System.EventHandler(this.pictureBox_MouseLeave);
  28.                 pictureBox[i].MouseHover += new System.EventHandler(this.pictureBox_MouseHover);
  29.                 this.Controls.Add(pictureBox[i]);
  30.             }
  31.         }
  32.  
  33.         private void pictureBox_MouseHover(object sender, EventArgs e)
  34.         {
  35.             PictureBox pb;
  36.             pb = (PictureBox)sender;
  37.             pb.BackColor = Color.BlueViolet;
  38.         }
  39.  
  40.         private void pictureBox_MouseLeave(object sender, EventArgs e)
  41.         {
  42.             PictureBox pb;
  43.             pb = (PictureBox)sender;
  44.             pb.BackColor = Color.Aqua;
  45.         }
  46.     }
  47. }
  48.  
May 20 '07 #2
shidec
26
Correction: that program make 3 Picture Box,
to make 5, just change 3 -> 5
May 20 '07 #3
vanc
211 Expert 100+
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.
May 20 '07 #4
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
Jun 7 '07 #5
mod4c
1
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?
Feb 5 '09 #6
tlhintoq
3,525 Expert 2GB
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(Environment.CurrentDirectory, @"bin\blackPiece3.gif");
Feb 5 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: SamSpade | last post by:
I have a usercontrol that contains a picturebox. The user can obtain (creategraphics) a picturebox graphics object and draw on the picturebox. She could do gr.GraphincInit.Millimeter and then draw...
5
by: Christopher Kurtis Koeber | last post by:
Dear All, This may sound like an elementary question but how do you implement scrollbars for the Picturebox control. Do I have to create my own code to do this or is there some property that I can...
2
by: Just Me | last post by:
Using a graphics object to draw a string in a picturebox. Been looking for a way to set a tab in the picturebox or graphic object and can not find out how to do that. Is it possible? ...
3
by: EnglishMan69 | last post by:
Hello All, I am using VB2005 Beta 2 in VS 2005 and am running into a small problem. I need to be able to add a picture box to the main form from within a thread. The program goes to a web...
7
by: kebalex | last post by:
Hi, I have an app (written in .NET 2.0) which updates a picturebox according to some user input (a slider control). when the user makes a change i loop through all of the pixels, do a...
5
by: toby | last post by:
Hi there, Is it possible to create an array of picturebox controls during run-time. I wish to create a new image/picturebox everytime a user clicks the button on a form, and they need to be...
4
by: Jerry West | last post by:
I have a routine that updates a PictureBox image every x seconds. I do this by first loading an array with the path to all of the images. I then generate a random number to use as the index of the...
4
by: munibe | last post by:
Hi, i have a problem about picturebox control. if you may help me, i will be so happy. i have a picturebox named pic_map, and i added a button named customer_button, my wish is to add a new small...
4
by: Jim McGivney | last post by:
In C# on Form1 I genetate an array of PictureBoxes and populate each with an image as seen in the code below. Later on I want to access a specific PictureBox to change its image, but I keep...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.