简体   繁体   English

在WinForm中声明一个全局整数

[英]Declare a global integer in WinForm

I am trying to declare the int 我正在尝试声明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' 字段初始化程序不能引用非静态字段,方法或属性'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!). 您正在尝试设置变量i初始值(顺便说一句,这是一个不好的变量名-变量名应具有描述性!)。

You'll need to set the value of i at some point in your workflow -- without any context, I'd guess probably in an OnTextChange event or something similar. 您需要在工作流中的某个时刻设置i的值-没有任何上下文,我想可能是在OnTextChange事件或类似事件中。

private int i;

然后在那之后(在构造函数中)

i = textBox1.Text.Length;

It is important to understand the order in which code runs. 了解代码运行的顺序很重要。 You put the field declaration in the middle of your code, perhaps hoping that it would get initialized after the checkBox1_CheckedChanged() method runs. 您可以将字段声明放在代码的中间,也许希望它运行checkBox1_CheckedChanged()方法之后得到初始化。 But no, fields are initialized before the constructor of the class runs. 但是不可以,在类的构造函数运行之前 ,必须先初始化字段。

That could never come to a good end. 那永远不会有一个好的结局。 The textBox1 object doesn't exist yet, it gets created by the constructor in the InitializeComponent() method. textBox1对象尚不存在,它由构造函数在InitializeComponent()方法中创建。 It will most certainly not have a Length yet, that won't happen until much later when the user types something. 这将肯定不会有长度的是,这不会发生,直到很久以后,当用户键入的东西。 The compiler error keeps you out of trouble. 编译器错误使您摆脱麻烦。

I have no idea what the code tries to do. 我不知道代码试图做什么。 But assuming the i variable needs to be initialized with something, you do so when the user types something in the text box. 但是,假设i变量需要使用某些内容进行初始化,那么当用户在文本框中键入某些内容时,您需要这样做。 Which makes the Text.Length property change. 这使Text.Length属性发生更改。 Add an event handler for the textbox' TextChanged event: 为文本框的TextChanged事件添加事件处理程序:

    private void textBox1_TextChanged(object sender, EventArgs e) {
        if (textBox1.Text.Length <= 15) {
            i = 15 - textBox1.Text.Length;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM