我正在尝试声明 int
private int i = 15 - textBox1.Text.Length;
作为此代码的全局整数
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
i = 15 - textBox1.Text.Length;
timer1.Enabled = true;
timer1.Start();
}
else
{
timer1.Enabled = false;
}
}
private int i = 15 - textBox1.Text.Length; //this wont work but i need it to
private int y = 15 - textBox1.Text.Length; //this wont work either but i also need it to
private void timer1_Tick(object sender, EventArgs e)
{
if (i <= 11)
{
i++;
string ping = new string(' ', i) + textBox1.Text;
label1.Text = ping;
if (i == 10)
{
y = 11;
}
}
else if (y > 0)
{
y--;
string pong = new string(' ', y) + textBox1.Text;
label1.Text = pong;
if (y == 0)
{
i = 0;
}
}
}
但是我得到了错误
字段初始值设定项不能引用非静态字段、方法或属性“text_test.Form1.textBox1”
帮助?
最佳答案
您正在尝试设置变量 i
的 initial 值(顺便说一下,这是一个错误的变量名——变量名应该是描述性的!) .
您需要在工作流程的某个时刻设置 i
的值——在没有任何上下文的情况下,我猜可能是在 OnTextChange
事件或其他事件中类似。
关于c# - 在 WinForm 中声明一个全局整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9287805/