简体   繁体   中英

C# textbox1_keydown event with “static” variable

Hi I just wrote a method for keydown event of a textbox. The problem here is: I need a variable to be "static", that is, the change of the variable could be reserved for the next run of the method. I tried to use static but it seems only static method could allow such declaration.

Can I ask what can I do to solve the problem? Thank you very much!

private  void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {

        if (e.KeyCode != Keys.Enter) return;

        if (comboBox1.SelectedIndex == 0)
        {

            if (textBox1.Text == "00000000")
            {

                typeselected = true;
                type = 0;

            }
            else if (textBox1.Text == "00000001")
            {
                typeselected = true;
                type = 1;

            }
            else if (textBox1.Text == "00000002")
            {
                typeselected = true;
                type = 2;
            }
            else if (textBox1.Text == "00000003")
            {
                typeselected = true;
                type = 3;

            }
            else if (textBox1.Text == "00000004")
            {
                typeselected = true;
                type = 4;

            }
            else if (textBox1.Text == "00000005")
            {
                typeselected = true;
                type = 5;

            }
            else if (textBox1.Text == "00000006")
            {
                typeselected = true;
                type = 6;

            }


            else if (typeselected) { typeselected = excel0(textBox1.Text, type); }
        }

You could make use of an instance variable

An instance variable of a class comes into existence when a new instance of that class is created, and ceases to exist when there are no references to that instance and the instance's destructor (if any) has executed.

Something like

private int tada = 0;
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    tada++;
}

使您的静态变量成为类实例变量,而不是方法变量。

Instance methods (without the keyword static) can reference static class variables:

class X
{
    static int A;

    public void SetA(int newA)
    {
        A = newA;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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