|
-
November 15th, 2009, 06:46 AM
#1
How to change button color using tag or name property
Hey,
I'm creating a little battleships game and it contains two 10*10 fields which itself contains 200 buttons. I'm creating them using the following code:
Code:
private void battleField(int x, int y, int size, string shipName)
{
int pointA = 0; // first coordinate
for (int i = 0; i < size * 10; i += size)
{
int pointB = 0; // second coordinate
for (int t = 0; t < size * 10; t += size)
{
b1 = new Button();
b1.Location = new Point(x + t, y + i);
b1.Size = new Size(size, size);
b1.Name = (shipName + " " + System.Convert.ToString(pointA)+ " " + System.Convert.ToString(pointB));
b1.MouseUp += new MouseEventHandler(MainForm_MouseUp);
b1.BackColor = customColor;
this.Controls.Add(b1);
pointB++;
}
pointA++;
}
}
Later on, when the game has begun, I would want to find a specific button, and change it's color. In web application it could be done using ID and findControl:
Code:
Button x = (Button)Panel1.FindControl("f" + Convert.ToString(i));
x.BackColor = Color.Black;
Unfortunately this approach doesn't work in windows forms applications and I haven't found a good way to do it. I tried to do something like this, but I still can't get the specified button to change it's color.
Code:
protected void changeColor(int x, int y, Color col)
{
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(Button))
{
if (c.Tag == (System.Convert.ToString(x) + " " + System.Convert.ToString(y)))
{
c.BackColor = col;
}
}
}
}
Any help would be appreciated.
-
November 15th, 2009, 12:21 PM
#2
Re: How to change button color using tag or name property
---EDIT----
You never assign anything to your .Tag property :P
I cant see why it wouldn't work except that in your web application you are looking for controls in panel1, whilst in your forms app you refer to this.Controls. Is "this" refering to the form or panel1 ?
I did a simple test
Code:
private void button1_Click(object sender, EventArgs e)
{
button1.BackColor = Color.Yellow;
}
That changes the color of the button without any problems. Can you place a breakpoint in your code where it is supposed to change the color and see that it is actually being executed?
Last edited by ixilom; November 15th, 2009 at 12:24 PM.
Reason: Noticed something
-
November 15th, 2009, 05:47 PM
#3
Re: How to change button color using tag or name property
Actually I assigned Tag properties for those buttons, but as it didn't work at the time I just removed it from my code.
I did a little bit of testing and I found out that I was missing just a single cast. I had to add "(string)" in front of "c.Tag" and everything started to move.
Code:
protected void changeColor(int x, int y, Color col)
{
foreach (Control c in this.Controls)
{
if (c.GetType() == typeof(Button))
{
if ((string)c.Tag == (System.Convert.ToString(x) + " " + System.Convert.ToString(y)))
{
c.BackColor = col;
}
}
}
}
Thanks for the help
-
November 15th, 2009, 10:24 PM
#4
Re: How to change button color using tag or name property
Have you investigated using a TableLayoutPanel to layout your buttons? You may find this easier in the long run (including finding them).
Rob
-
Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|