不存在问题 [英] Does not Exist Issue
本文介绍了不存在问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以 int count =(int)button.tag
表示按钮不存在,因为按钮被设置为其范围之外的变量。我已经尝试了很多东西来解决这个问题,因为我不能将它放在button_click中,因为这会弄乱这个数。
任何想法该做什么?
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?
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";
}
}
}
推荐答案
移动 int count =(int)button.Tag; ... line lower:
Move your int count = (int)button.Tag;... line lower:
void button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
int count = (int)button.Tag;
if (count < 3)
[更新]
[UPDATED]
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.
但是如果我这样做的话按钮文本始终是
But if I do that the button text is always a
这篇关于不存在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文