Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, December 8, 2010 9:50 PM
Hello,
This is for my homework. Actually it is to find out the places that the knight will play on a chess table. I did the work indeed.
But I want to add some visuality. I added 64 pictureboxes on the form in design time.
I want to display the knight image on the square (picturebox in my case) that is clicked.
If and only if I can get the name of the PictureBox that is clicked by mouse.
I can manipulate the click events of each picturebox, but is there a way to get the name of the control that is clicked?
Thanks,
Orkun
All replies (6)
Wednesday, December 8, 2010 10:31 PM ✅Answered | 1 vote
Use the same Click event handler for all your pictureboxes. The sender cast to a PictureBox gives you the PictureBox that was clicked. It would probably be easier to layout your pictureboxes programmatically rather than using the designer.
Thursday, December 9, 2010 8:34 AM ✅Answered | 1 vote
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 64; i++)
{
PictureBox newPB = new PictureBox();
newPB.Name = "PB" + i.ToString();
newPB.Image = Properties.Resources.YourPicture;
newPB.Location = new Point(100, i * 15); // Enter the right location for each picture
newPB.Click += new EventHandler(pb_Click);
this.Controls.Add(newPB);
}
}
void pb_Click(object sender, EventArgs e)
{
PictureBox clickedPictureBox = sender as PictureBox;
this.Text = clickedPictureBox.Name; // Just for demo. The name will appear on the forms' title bar
}
Noam B.
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
Thursday, December 9, 2010 3:20 PM ✅Answered | 1 vote
Try this:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
partial class Form1 : Form
{
private ChessBoard Board = new ChessBoard();
public Form1()
{
InitializeComponent();
this.Size = new Size(Board.Width + 8, Board.Height + 28);
Board.Parent = this;
}
public class ChessBoard : Panel
{
internal PB[,] PBs = new PB[8, 8];
public ChessBoard()
{
for (int Y = 0; Y <= 7; Y++)
{
for (int X = 0; X <= 7; X++)
{
PBs[X, Y] = new PB();
int L = X * PBs[X, Y].Width;
int T = Y * PBs[X, Y].Height;
PBs[X, Y].Location = new Point(L, T);
if ((Y * 7 + X) % 2 == 0)
{
PBs[X, Y].BackColor = Color.Red;
}
else
{
PBs[X, Y].BackColor = Color.Black;
}
PBs[X, Y].Name = ((char)(65 + X)).ToString() + (7 - Y + 1).ToString();
PBs[X, Y].Parent = this;
}
}
this.Size = new Size(PBs[7, 7].Right, PBs[7, 7].Bottom);
}
public class PB : PictureBox
{
internal bool DrawName;
public PB()
{
this.Size = new Size(50, 50);
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
this.DrawName = true;
this.Refresh();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if (this.DrawName)
{
Font F = this.Parent.Font;
int H = (this.Height - F.Height) / 2;
int W = (this.Width - F.Height) / 2;
pe.Graphics.DrawString(this.Name, F, Brushes.White, W, H);
}
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
this.DrawName = false;
this.Refresh();
}
}
}
}
}
Wednesday, December 8, 2010 9:57 PM | 1 vote
Hello antmen,
your method requires an event for each of your picture boxes, i would personally use a data grid view with image columns. That way when the user clicks an image you can get the exact cell that was clicked then continue along. For your method you could use the MouseClick event and grab the name there(again for every picture box).
Thursday, December 9, 2010 8:37 AM
Thank you very much for your answers. I will try your recommendations.
Friday, December 10, 2010 10:30 AM
Wow, that's impressive.
I should go through this code line by line since I do not understand most part of it.
But I think this is one of the most effective way of learning programming.
Thanks again,