Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So int count = (int)button.tag is saying button does not exist because button is being set to a variable outside of its scope. I have tried numerous things to fix this because I can not place it inside the button_click since that would mess up this count.
Any ideas what to do?
C#
public MapForm(String rows, String cols)
{
    InitializeComponent();
     _col = int.Parse(cols);
    _row = int.Parse(rows);
    Begin();
}

public void Begin()
{
    int width = groupBox1.Width;
    int height = groupBox1.Height;
    int bW = width / _col;
    int bH = height / _row;

    Button[,] buttonArray = new Button[_row, _col];

    for (int i = 0; i < _row; i++)
    {
        for (int j = 0; j < _col; j++)
        {
            Button button = new Button();
            buttonArray[i, j] = button;
            button.Width = bW;
            button.Height = bH;
            button.Left = j * bW;
            button.Top = i * bH;
            button.Tag = 0;

            button.Click += new EventHandler(button_Click);

            groupBox1.Controls.Add(button);
        }
     }
}

int count = (int)button.Tag;

void button_Click(object sender, EventArgs e)
{
    Button button = (Button)sender;

    if (count < 3)
    {
        count++;

        if (count == 1)
        {
            button.Text = "a";
        }

        if (count == 2)
        {
            button.Text = "b";
        }
        if (count == 3)
        {
            button.Text = "c";
        }
    }
}
Posted
Updated 22-Mar-14 10:45am
v2
Comments
Kornfeld Eliyahu Peter 22-Mar-14 16:54pm    
That part of 'int count' does not make any sense...
Can you explain why it's there?

Move your int count = (int)button.Tag;... line lower:
C#
void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
int count = (int)button.Tag;
if (count < 3)


[UPDATED]
C#
void button_Click(object sender, EventArgs e)
{
    Button button = (Button)sender;
    int count = (int)button.Tag;

    if (count < 3)
    {
        count++;

        if (count == 1)
        {
            button.Text = "a";
        }

        if (count == 2)
        {
            button.Text = "b";
        }
        if (count == 3)
        {
            button.Text = "c";
        }
        button.Tag = count; // You need to store value of COUNT to your Button's TAG property
    }
}


I hope it helps you.
 
Share this answer
 
v5
But if I do that the button text is always a
 
Share this answer
 
Comments
Marcin Kozub 22-Mar-14 19:08pm    
It's very simple. When you're creating your buttons dynamically, you set theirs Tag property to 0:
...
button.Top = i * bH;
button.Tag = 0;
...

Next in your ButtonClick event handler you've got code:
if (count < 3)
{
count++;
if (count == 1)
{
button.Text = "a";
}

Your first condition will be true (if count < 3) because count = button.Tag which is initialized with 0.
Next you're incrementing value of count by 1 (count++). So, your count value will be 1. That's why you are getting button.Text = "a"...

I think that you want to store back your count value to button's Tag property. I've updated my answer.
angelandbuffy9 22-Mar-14 19:28pm    
Thank you!! :)
Marcin Kozub 22-Mar-14 20:44pm    
If I helped you, it would be nice, if you accept my solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900