fallwinters 2016-08-02 08:53 采纳率: 50%
浏览 968
已采纳

【C#菜鸟】打字游戏,为什么StatusScrip没有更新数据?

图片说明

图片说明

问题如下:
为什么StatusScrip控件没有更新数据?

源代码如下:

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 打字游戏120160802
{
public partial class Form1 : Form
{

Random random = new Random();
Status status = new Status();

    public Form1()
    {


        InitializeComponent();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        listBox1.Items.Add((Keys)random.Next(48, 57));

        if (listBox1.Items.Count > 10)
        {
            listBox1.Items.Clear();
            listBox1.Items.Add("Game Over");
            timer1.Stop();
        }


    }

    private void Form1_Load(object sender, EventArgs e)
    {


    }

    private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
        if (listBox1.Items.Contains(e.KeyCode))
        {
            listBox1.Items.Remove(e.KeyCode);
            listBox1.Refresh();

            if (timer1.Interval > 400)
                timer1.Interval -= 1;
            if (timer1.Interval > 250)
                timer1.Interval -= 1;
            if (timer1.Interval > 100)
                timer1.Interval -= 1;

            difficultyProgressBar.Value = 800 - timer1.Interval;

            status.Update(true);

        }
        else
        {
            status.Update(false);
        }
        correctLabel.Text = "Correct: " + status.Correct;
        missedLabel.Text = "Missed: " + status.Missed;
        totalLabel.Text = "Total: " + status.Total;
        accuracyLabel.Text = "Accuracy: " + status.Accuracy + "%";
    }

    private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {

    }
}

}

类——Status.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 打字游戏120160802
{
class Status
{
public int Total = 0;
public int Missed = 0;
public int Correct = 0;
public int Accuracy = 0;

    public void Update(bool correctKey)
    {
        Total++;

        if (!correctKey)
        {
            Missed++;
        }
        else
        {
            Correct++;
        }

        Accuracy = Correct / (Missed + Correct) * 100;

    }
}

}

谢谢!

  • 写回答

1条回答 默认 最新

  • fallwinters 2016-08-02 09:50
    关注

    因为没有把窗体的Keypreview属性改成true,因此导致没有更新,代码是没有问题的,终于找到问题所在了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置