-2

I have a problem. I want to change this variable when I press an arrow key on a keyboard.

int rev = 0;

So I came up with this

public void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        switch (e.KeyValue)
        {
            case 37: rev -= 100; break;
            case 38: rev += 20; break;
            case 39: rev += 100; break;
            case 40: rev -= 20; break;
        }
    }

And what I am trying to do with this variable is change the text in the label.

label4.Text = rev.ToString();

There are no errors in the code, it just doesn't work. Does it matter where do I put the label code or did I do something wrong when changing the variable?

Full code

public partial class Form1 : Form
{
    int rev = 0;
    int gear = 1;
    int speed = 0;
    int key = 0;
    public Form1()
    {
        InitializeComponent();


        int[] g1 = new int[] { 1, 1 };
        int[] g2 = new int[] { 1, 2 };
        int[] g3 = new int[] { 1, 3 };
        int[] g4 = new int[] { 2, 3 };
        int[] g5 = new int[] { 2, 4 };
        int[] g6 = new int[] { 2, 5 };
        int[] g7 = new int[] { 2, 6 };
        int[] g8 = new int[] { 3, 6 };
        int[] g9 = new int[] { 3, 7 };
        int[] g10 = new int[] { 3, 8 };

        double r1 = Math.Round((2 * Math.PI * 5.5) / (2 * Math.PI * 6), 3);
        double r2 = Math.Round((2 * Math.PI * 5.5) / (2 * Math.PI * 5.5), 3);
        double r3 = Math.Round((2 * Math.PI * 5.5) / (2 * Math.PI * 5), 3);
        double r4 = Math.Round((2 * Math.PI * 8.5) / (2 * Math.PI * 5), 3);
        double r5 = Math.Round((2 * Math.PI * 8.5) / (2 * Math.PI * 4.5), 3);
        double r6 = Math.Round((2 * Math.PI * 8.5) / (2 * Math.PI * 4), 3);
        double r7 = Math.Round((2 * Math.PI * 8.5) / (2 * Math.PI * 3.5), 3);
        double r8 = Math.Round((2 * Math.PI * 10.5) / (2 * Math.PI * 3.5), 3);
        double r9 = Math.Round((2 * Math.PI * 10.5) / (2 * Math.PI * 3), 3);
        double r10 = Math.Round((2 * Math.PI * 10.5) / (2 * Math.PI * 2.5), 3);


        label4.Text = rev.ToString();

        switch (gear)
        {
            case 1: label5.Text = g1[0].ToString(); label6.Text = g1[1].ToString(); speed = (int)Math.Round(rev * r1) * 35 * 60 / 100000; break;
            case 2: label5.Text = g2[0].ToString(); label6.Text = g2[1].ToString(); speed = (int)Math.Round(rev * r2) * 35 * 60 / 100000; break;
            case 3: label5.Text = g3[0].ToString(); label6.Text = g3[1].ToString(); speed = (int)Math.Round(rev * r3) * 35 * 60 / 100000; break;
            case 4: label5.Text = g4[0].ToString(); label6.Text = g4[1].ToString(); speed = (int)Math.Round(rev * r4) * 35 * 60 / 100000; break;
            case 5: label5.Text = g5[0].ToString(); label6.Text = g5[1].ToString(); speed = (int)Math.Round(rev * r5) * 35 * 60 / 100000; break;
            case 6: label5.Text = g6[0].ToString(); label6.Text = g6[1].ToString(); speed = (int)Math.Round(rev * r6) * 35 * 60 / 100000; break;
            case 7: label5.Text = g7[0].ToString(); label6.Text = g7[1].ToString(); speed = (int)Math.Round(rev * r7) * 35 * 60 / 100000; break;
            case 8: label5.Text = g8[0].ToString(); label6.Text = g8[1].ToString(); speed = (int)Math.Round(rev * r8) * 35 * 60 / 100000; break;
            case 9: label5.Text = g9[0].ToString(); label6.Text = g9[1].ToString(); speed = (int)Math.Round(rev * r9) * 35 * 60 / 100000; break;
            case 10: label5.Text = g10[0].ToString(); label6.Text = g10[1].ToString(); speed = (int)Math.Round(rev * r10) * 35 * 60 / 100000; break;
        }


    }



    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void label3_Click(object sender, EventArgs e)
    {

    }



    private void label4_Click(object sender, EventArgs e)
    {

    }

    private void timer1_Tick(object sender, EventArgs e)
    {

    }
    public void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        switch (e.KeyValue)
        {
            case 37: rev -= 100; break;
            case 38: rev += 20; break;
            case 39: rev += 100; break;
            case 40: rev -= 20; break;
        }
    }



}
  • 2
    First, KeyPreview needs to be true for the form to see keystrokes. Then the keydown event will just change the `rev` variable not the label text. Those are set once when the form is created because that is where the code is – Ňɏssa Pøngjǣrdenlarp Jan 24 '19 at 19:26

2 Answers2

0

The .ToString() method will only return a value. This means that when you call it, it spits out a string, and that string is now set to the label's text field. If rev were to change, the label would not as its text is set to the string from earlier.

RetAFVLib
  • 176
  • 1
  • 2
  • 13
  • Thank you for your help, but I need to have .ToString() method cause otherwise I get an error. The problem was in the incorrect placement of the label changing code. – David Šeruga Jan 24 '19 at 19:44
0

Now I figured out that where you put your label does matter. So I made this

public void napis()
    {
        label4.Text = rev.ToString();
    }

And I put this right where the rev variable changes so it now looks like this

public void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        switch (e.KeyValue)
        {
            case 37: rev -= 100; napis(); break;
            case 38: rev += 20; napis(); break;
            case 39: rev += 100; napis(); break;
            case 40: rev -= 20; napis(); break;
        }
    }

And it works. The value is changing like it should.