简体   繁体   中英

Variable wont update in winforms

void Neededtype_KeyDown(object sender, KeyEventArgs e)
{
    if (neededkey == e.KeyCode)
    {
        neededtype.ForeColor   = Color.FromName("Gray");
        mostanikari++;

        if (mostanikari == 2)
        {
            neededtype.Text = "work";
        }

        neededtype.SelectionStart = mostanikari;
        kovikari = mostaniszo[mostanikari].ToString();
        neededtype.SelectionLength = neededtype.Text.Length;
        neededtype.SelectionColor = Color.FromName("Blue");

        if (mostanikari == neededtype.Text.Length)
        {
            //random word gen here
        }
    }

    InitializeComponent();
}
  • neededkey : the key that needs to be pressed
  • mostanikari : is an int that keeps track of which letter we are on now
  • kovikari : is the string neededkey is calculated from
  • neededtype : is the richtextbox i am displaying the word in

This code over here is trying to add +1 to mostanikari and then move forward with the selection

You don't need to be calling InitializeComponent() when you press a key because this will reset the object to its default state. Which is why you don't see a change in the winform.

void Neededtype_KeyDown(object sender, KeyEventArgs e) 
{
    if (neededkey == e.KeyCode)
    {
        neededtype.ForeColor   = Color.FromName("Gray");
        mostanikari++;
        if (mostanikari == 2)
        {
            neededtype.Text = "work";
        }
        neededtype.SelectionStart = mostanikari;
        kovikari = mostaniszo[mostanikari].ToString();
        neededtype.SelectionLength = neededtype.Text.Length;
        neededtype.SelectionColor = Color.FromName("Blue");

        if (mostanikari == neededtype.Text.Length)
        {
            //random word gen here
        }
    }
}

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