I am trying to declare the int
private int i = 15 - textBox1.Text.Length;
as a global integer for this code
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;
}
}
}
but i get the error
A field initializer cannot reference the non-static field, method, or property ‘text_test.Form1.textBox1’
help?
You’re trying to set the initial value of the variable
i(which is a bad variable name, by the way — variable names should be descriptive!).You’ll need to set the value of
iat some point in your workflow — without any context, I’d guess probably in anOnTextChangeevent or something similar.