use a picture box on a panel - c#

I need some help. I'm working on a chess game. I created my chess board on a panel, with pictureBox. As the code below
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
PictureBox boardcase = new PictureBox();
boardcase.Size = new Size(100, 100);
//if (i == j) { boardcase.BackColor = Color.White; }
if ((i + j) % 2 == 0) { boardcase.BackColor = Color.DarkBlue; }
else { boardcase.BackColor = Color.Gray; }
boardcase.Location = new Point(i * (boardcase.Width + 5) + 45, j * (boardcase.Height + 5) + 15);
panel1.Controls.Add(boardcase);
}
}
Then, I would like to know how I can use individually each pictureBox that i created, outside this block.
thanks a lot

Haha, ok.I used #TaW 's solution in comment section. and this is it… I just created a PictureBox List as : List<PictureBox> plateau = new List<PictureBox>(); and assigned each boardcase to the list in my For Loop. plateau.Add(boardcase); Then I can call plateau[i] in my code. :-) Thanks guys.

Related

c# picturebox to control not working

I am following a tutorial on how to make a recursive maze in c#.
this is the tutorial: http://www.c-sharpcorner.com/uploadfile/4a950c/solving-mazes-using-recursion/
i was following these steps,but I can't get any further because when I run this code, the picture boxes won't show up as in the tutorial.
this is the part where they should be made:
private void createNewMaze()
{
mazeTiles = new PictureBox[XTILES, YTILES];
//Loop for getting all tiles
for (int i = 0; i < XTILES; i++)
{
for (int j = 0; j < YTILES; j++)
{
//initialize a new PictureBox array at cordinate XTILES, YTILES
mazeTiles[i, j] = new PictureBox();
//calculate size and location
int xPosition = (i * TILESIZE) ; //13 is padding from left
int yPosition = (j * TILESIZE) ; //45 is padding from top
mazeTiles[i, j].SetBounds(xPosition, yPosition, TILESIZE, TILESIZE);
//make top left and right bottom corner light blue. Used for start and finish
if ((i == 0 && j == 0) || (i == XTILES - 1 && j == YTILES - 1))
mazeTiles[i, j].BackColor = Color.LightBlue;
else
{
//make all other tiles white
mazeTiles[i, j].BackColor = Color.White;
//make it clickable
EventHandler clickEvent = new EventHandler(PictureBox_Click);
mazeTiles[i, j].Click += clickEvent; // += used in case other events are used
}
//Add to controls to form (display picture box)
this.Controls.Add(mazeTiles[i, j]);
}
}
}
this is what i get:
Maze
i can't find the solution, maybe someone can, thanks in advance.

Get properties from object in tag

For a School assignment we are making a Chess game in C#, in which we have to learn to work in a Object-Oriented way. The board is made out of a 2D picturebox array in a nested for loop
//Create the Board Pattern out of PictureBoxes
#region Checkerboard
PictureBox[,] Vak = new PictureBox[8, 8];
for (int i = 0; i < 8; i++)
{
for (int x = 0; x < 8; x++)
{
Vak[i, x] = new PictureBox();
Vak[i, x].Name = String.Format("{0},{1}", i, x);
Vak[i, x].Width = 50;
Vak[i, x].Height = 50;
Vak[i, x].Location = new Point(xpos, ypos);
Vak[i, x].SizeMode = PictureBoxSizeMode.Zoom;
Vak[i, x].Click += Chess_Click;
if ((i + x) % 2 == 0)
{
Vak[i, x].BackColor = ColorTranslator.FromHtml("#e5e5e5"); //white ColorTranslator.FromHtml("#e5e5e5");
}
else
{
Vak[i, x].BackColor = ColorTranslator.FromHtml("#545454"); //black ColorTranslator.FromHtml("#545454");
}
xpos += 50;
this.Controls.Add(Vak[i, x]);
}
xpos = 50;
ypos += 50;
this.Controls.Add(border);
}
#endregion
On other posts i have found that i can Refer to a class (via the picturebox) using the .Tag property like so:
Vak[i,x].Tag = new Tower();
However, i cannot figure out a way to call properties from the Tower class from withing the tag
Say the Tower class has a property "name", how would i go about calling that
string objectname = Vak[i,x].Tag.(name?)
Sorry If this is a stupid question, but i am very new to programming.
Thanks!
You have to cast it, since Tag is an Object type:
Tower tower = Vak[i,x].Tag as Tower;
if(tower!=null)
{
//do stuff
}
However, i would avoid storing data structures in Tag properties like this.

Problems with logic in c# script where images are loaded in a grid type look

i have an unknown amount of images that im adding to a Grid control with code and im kind of lost in the logic as images are inserted in wrong order. Have a look (Modulus like this because of testing):
grid.Height = this.Height;
grid.Width = this.Width;
grid.ShowGridLines = true;
for (int i = 0; i < 50; i++)
{
RowDefinition rowDef = new RowDefinition();
rowDef.Height = new GridLength(50);
grid.RowDefinitions.Add(rowDef);
ColumnDefinition colDef = new ColumnDefinition();
colDef.Width = new GridLength(50);
grid.ColumnDefinitions.Add(colDef);
}
int x = 1;
int y = 1;
for (int i = 0; i < 50; i++)
{
y++;
if (i % 10 == 0)
{
x++;
y = 1;
}
Image img = new Image() { Source = new BitmapImage(new Uri("Images/positive.png",UriKind.Relative)), Width = 50, Height = 50, Margin = new Thickness(2,2,2,2) };
grid.Children.Add(img);
Grid.SetRow(img, x);
Grid.SetColumn(img, y);
}
Result:
As you can see the images are starting on a new row every 10 image as the modulus says, but they do not start at the first row in the first coloumn.
What i want to achieve is this:
What am i doing wrong? Thanks!
Set intitial values as follows should solve your problem.
int x = -1;
int y = -1;
And inside if you should try
if (i % 10 == 0)
{
x++;
y = 0;
}
On your first iteration through the loop, i is 0.
(0 % 10 == 0) // true
So x is incremented by 1 immediately.
Set a breakpoint at the start of your for loop and follow the execution. In this case you'd have seen it immediately.

Trying to create a grid of pictureboxes in an array for a board in c#

I'm trying to make a board for a Naughts and crosses (tic tac toe) game using pictureboxes in an array and what I've come up with is this. This starts when I press a button.
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
PictureBox[,] pb = new PictureBox[i, j];
pb[i, j].Location = new Point(i * 150 + 100, j * 150 + 100);
pb[i, j].Width = 150;
pb[i, j].Height = 150;
pb[i, j].Visible = true;
pb[i, j].BorderStyle = BorderStyle.FixedSingle;
pb[i, j].BringToFront();
this.Controls.Add(pb[i, j]);
}
}
this throws me a "System.IndexOutOfRangeException" on the line
pb[i, j].Location = new Point(i * 150 + 100, j * 150 + 100);
Whats wrong here?
You're not far off - you need to declare the array outside of the loop, and create a picture with each iteration - this works (tested):
PictureBox[,] pb = new PictureBox[3, 3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
pb[i,j] = new PictureBox();
pb[i, j].Location = new Point(i * 150 + 100, j * 150 + 100);
pb[i, j].Width = 150;
pb[i, j].Height = 150;
pb[i, j].Visible = true;
pb[i, j].BorderStyle = BorderStyle.FixedSingle;
pb[i, j].BringToFront();
this.Controls.Add(pb[i, j]);
}
}
(Note the logic in the loop was wrong too, it should be < 3 not <= 3 as you're starting at 0)
You have declared and instantiated your multidimennsional array within your for loops. Try the following:
PictureBox[,] pb = new PictureBox[3, 3];
for (int i = 0; i <= 3; i++)
{
for (int j = 0; j <= 3; j++)
{
pb[i, j] = new PictureBox();
pb[i, j].Location = new Point(i * 150 + 100, j * 150 + 100);
pb[i, j].Width = 150;
pb[i, j].Height = 150;
pb[i, j].Visible = true;
pb[i, j].BorderStyle = BorderStyle.FixedSingle;
pb[i, j].BringToFront();
this.Controls.Add(pb[i, j]);
}
}
Well i would say your approach of creating picture box array to create the Ticktack Toe game is wrong.Plus your code is inefficient
In your code you are repeatedly creating redundant arrays
PictureBox[,] pb = new PictureBox[i, j];
You are wasting Runtime memory here.
What i would recommend is creating a new Picturebox Class Inherited from the PictureBox class.
Divide this rectangular area into 3X3 Matrix(in terms of dimension)
Then you need to Capture the click event and get the point
private void pictureBox1_Click(object sender, EventArgs e)
{
MouseEventArgs eM = (MouseEventArgs)e;
//eM.X -X Coordinate eM.Y -Y Coordinate
}
Using this Coordinate identify the matrix location,where the user clicked.Then redraw the whole picture-box to reflect the input made by user.
This will separate the need for maintaining a picturebox array you can concentrate just to maintain a 3X3 Integer or Boolean array(0 or 1).When a win condition occurs you just need to draw a line across the matching array entries.
Use the Graphics.DrawLine method to do this

Dynamically programming a grid consisting of 64 buttons (8x8)

I'm trying to create a chess game purely for my learning C# and chess. Just to start off with, I would like to create an 8x8 grid of buttons through code rather than the designer. This would save me hard coding each button individually.
A button array would seem a good way to start but I have no idea how to implement this.
You can create a "square" class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
class Square:PictureBox
{
private bool color;
private char piece;
}
and define an array to make place for 8x8 squares.
public partial class Form1 : Form
{
Square[,] square = new Square[8, 8];
public Form1()
{
InitializeComponent();
int i, j;
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
this.square[i, j] = new Square();//Creating the chess object//
this.square[i, j].BackColor = System.Drawing.SystemColors.ActiveCaption;
this.square[i, j].BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.square[i, j].Location = new System.Drawing.Point(57 + i * 40, 109 + j * 40);
this.square[i, j].Name = "chessBox1";
this.square[i, j].Size = new System.Drawing.Size(40, 40);
this.square[i, j].TabIndex = 2;
this.square[i, j].TabStop = false;
this.Controls.Add(this.square[i, j]);
}
}
}
}
int ButtonWidth = 40;
int ButtonHeight = 40;
int Distance = 20;
int start_x = 10;
int start_y = 10;
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 8; y++)
{
Button tmpButton = new Button();
tmpButton.Top = start_x + (x * ButtonHeight + Distance);
tmpButton.Left = start_y + (y * ButtonWidth + Distance);
tmpButton.Width = ButtonWidth;
tmpButton.Height = ButtonHeight;
tmpButton.Text = "X: " + x.ToString() + " Y: " + y.ToString();
// Possible add Buttonclick event etc..
this.Controls.Add(tmpButton);
}
}
May be you ca use the code below to solve your problem. This code is of Windows Form application in C#. And for the control Button.
for (int i = 0; i< 8; i++)
{
for (int j = 0; j < 8; j++)
{
Button BtnNew = new Button;
BtnNew.Height = 80;
BtnNew.Width = 80;
BtnNew.Location = new Point(80*i, 80*j);
this.Controls.Add(BtnNew);
}
}

Categories