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: - 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;
-
-
}
-
-
}
6 25741
Use sender arguments from event.
This code make 5 PictureBox and (only) change color if mouse on the object. -
using System;
-
using System.Collections.Generic;
-
using System.ComponentModel;
-
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()
-
{
-
InitializeComponent();
-
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.EventHandler(this.pictureBox_MouseLeave);
-
pictureBox[i].MouseHover += new System.EventHandler(this.pictureBox_MouseHover);
-
this.Controls.Add(pictureBox[i]);
-
}
-
}
-
-
private void pictureBox_MouseHover(object sender, EventArgs e)
-
{
-
PictureBox pb;
-
pb = (PictureBox)sender;
-
pb.BackColor = Color.BlueViolet;
-
}
-
-
private void pictureBox_MouseLeave(object sender, EventArgs e)
-
{
-
PictureBox pb;
-
pb = (PictureBox)sender;
-
pb.BackColor = Color.Aqua;
-
}
-
}
-
}
-
Correction: that program make 3 Picture Box,
to make 5, just change 3 -> 5
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.
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
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?
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");
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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?
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |