Get properties from object in tag - c#

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.

Related

use a picture box on a panel

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.

How to create a Grid system where each square has an x and y value using visual studios graphics?

My goal is to have a robot use this grid to create a map base off the information it collects from its surroundings. When the robot detects an object the square in front of it turns red. Currently I am stuck on how I can give each square an x and Y value for location purposes. Also when I scroll the screen the block sizes change, can someone provide help with that as well?
Rectangle rect = new Rectangle(700, 350, 50, 50);
g.DrawRectangle(myPen, rect); // Draws the Rectangle to the screen
e.Graphics.FillEllipse(myBrush, 700,350,50,50);`
for (int i = 0; i < 9900; i = i + 50)
{
rect = new Rectangle(0 + i, 0, 50, 50);
g.DrawRectangle(myPen, rect);
for (int j = 0; j < 9900; j = j + 50)
{
rect = new Rectangle(0 + i, 0 + j, 50, 50);
g.DrawRectangle(myPen, rect);
}
}
Here is a very quick example of how to do this using a 2d array. It was written in LINQPad, so it may look a little odd, but it should give you some leads. It allows you to store a map and look up values using x and y coordinates. You can use the CellInfo class to add any extra information about the cell that you need, beyond if it is blocking or not.
Ideally, you would want to wrap the entire array up in your own Map class, that abstracts away the details, and gives you a lot of helpful utility functions. For instance, if your map is extremely large, you may run out of memory. You could have the Map class only load smaller blocks of the map from files on disk as needed, or even make the map wrap around its self easily.
void Main()
{
var map = new CellInfo[10, 10];
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
map[x, y] = new CellInfo();
}
}
var rnd = new Random();
for (int i = 0; i < 20; i++)
{
map[rnd.Next(0, 10), rnd.Next(0, 10)].IsBlocked = true;
}
DrawMap(map).Dump();
}
public Bitmap DrawMap(CellInfo[,] map)
{
var img = new Bitmap(320, 320, PixelFormat.Format32bppArgb);
using (var g = Graphics.FromImage(img))
{
for (int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
var cell = map[x, y];
Brush brush = cell.IsBlocked ? Brushes.Red : Brushes.White;
g.FillRectangle(brush, x * 32, y * 32, 31, 31);
g.DrawRectangle(Pens.Black, x * 32, y * 32, 31, 31);
}
}
}
return img;
}
public class CellInfo
{
public bool IsBlocked { get; set; } = false;
}
It produces the following output (varies each time it is run):

c# adding New Button Depending on the # of items in mySQLWorkbench

This code adds new buttons depending on the # of items saved on my items table.
mySQL WorkBench
I dont know why it keeps on duplicating my buttons. I just want to keep the buttons arranged into 4 columns.
object[] itemDetail;
object[] itemLi = itemsWS.searchItem("", "drinks", "all");
int cleft = 0;
for (int i = 0; i < itemLi.Length; i++)
{
itemDetail = itemsWS.getItemInfo(itemLi[i].ToString());
for (int x = 35; x < 537; x++)
{
Button myButton = new Button();
myButton.Text = itemDetail[0].ToString();
myButton.Top = cleft * 80;
myButton.Left = 70;
myButton.Location = new Point(x, cleft);
myButton.Size = new Size(100, 60);
tabPage1.Controls.Add(myButton);
cleft = cleft + 15;
//cleft = cleft + 1;
x += 134;
}
}
try something like this:
int x = 35;
int cleft = 0;
foreach (var item in itemLi)
{
Button myButton = new Button();
myButton.Text = itemDetail[0].ToString();
myButton.Top = cleft * 80;
myButton.Left = 70;
myButton.Location = new Point(x, cleft);
myButton.Size = new Size(100, 60);
tabPage1.Controls.Add(myButton);
x += 134;
// Check if x is greater than form size,
// If so, resets x, and increments cleft
if (x >= 537)
{
x == 35;
cleft += 15
}
}
I'm not exactly sure what x and cleft are doing, but you get the idea (hopefully)
You can also put FlowLayoutPanel inside the Bread tabpage, then just set the Orientation property of the FlowLayoutPanel, then do Ben's suggestion without the Top, Left, Location, cleft, and x stuff so you won't need to set the location of the Buttons.

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);
}
}

how to position an object using variables

I'm trying to create 81 picture boxes and have them automatically positioned a certain distance apart from one another but they don't seem to be placing in any logical order. I have to initialize the X point to -1700 for them to even appear on the screen. The following code gets the first 15 where I want them but then they start stacking on top of one another instead of continuing the pattern. This is the result of about an hour of tinkering but initially the logic looked fine. I even had a message box that would display the current X,Y that was being set and it was correct it just would not place them at those coordinates.
int X = -1700;
int Y = 0;
for (int i = 0; i < 81; i++)
{
this.Controls.Add(championThumbNailsArray[i]);
championThumbNailsArray[i].Height = 80;
championThumbNailsArray[i].Width = 80;
championThumbNailsArray[i].Location = new Point(X, Y);
// MessageBox.Show(Convert.ToString(X) + "," + Convert.ToString(Y));
championThumbNailsArray[i].ImageLocation = akali.grabPicture();
//championThumbNailsArray[i].ImageLocation = championsArray[i].grabPicture();
if (X <= 425)
X = X + 85;
else
{
X = -1700;
Y = Y + 85;
}
}
Instead of manually placing elements use a FlowLayoutPanel. Add the controls to the panel and let it do the arrangement for you.
This code works as you are expecting
private void Form1_Load(object sender, EventArgs e)
{
int x = 0;
int y = 0;
for (int i = 0; i < 81; i++)
{
PictureBox p = new PictureBox();
p.BorderStyle = BorderStyle.Fixed3D;
p.Height = 80;
p.Width = 80;
p.Location = new Point(x, y);
x += 85;
if (x > 425)
{
x = 0;
y += 85;
}
this.Controls.Add(p);
}
}
But I would go with something like #Ed said, a FlowLayout control

Categories