C#窗体keydown(更改变量)

时间:2019-01-24 19:23:19

标签: c# forms keydown

我有问题。我想在按键盘上的箭头键时更改此变量。

int rev = 0;

所以我想到了这个

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;
        }
    }

我要使用此变量进行的操作是更改标签中的文本。

label4.Text = rev.ToString();

代码中没有错误,只是不起作用。我在哪里放置标签代码或更改变量时是否做错了问题?

完整代码

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 个答案:

答案 0 :(得分:0)

.ToString()方法将仅返回一个值。这意味着当您调用它时,它会吐出一个字符串,并且该字符串现在已设置为标签的文本字段。如果要更改rev,则标签将不会更改,因为其文本将设置为之前的字符串。

答案 1 :(得分:0)

现在,我发现将标签放在哪里很重要。所以我做了这个

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

我把这个正确的地方放在rev变量更改的地方,所以现在看起来像这样

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;
        }
    }

它有效。该值正在发生应有的变化。