Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
(C#Windows窗体应用程序)如何重新启动应用程序_C#_Winforms_Restart - Fatal编程技术网

(C#Windows窗体应用程序)如何重新启动应用程序

(C#Windows窗体应用程序)如何重新启动应用程序,c#,winforms,restart,C#,Winforms,Restart,我刚刚完成了Head First C#的一个练习,在那里我制作了一个打字游戏。这本书留给读者去弄清楚如何做到这一点,这样玩家一旦输了就可以开始新的游戏。用户输掉游戏后,窗口显示消息“游戏结束”。我想有一个新的窗口弹出,并询问用户,如果他们想再次发挥一旦他们关闭了游戏在屏幕上。我希望有两个按钮;一个说“不”,一个说“是”。我一直坚持的是,如果用户决定要再次玩,我应该(或将)如何重新启动应用程序。我将在下面复制并粘贴我的代码: namespace _7HeadFirstProject { p

我刚刚完成了Head First C#的一个练习,在那里我制作了一个打字游戏。这本书留给读者去弄清楚如何做到这一点,这样玩家一旦输了就可以开始新的游戏。用户输掉游戏后,窗口显示消息“游戏结束”。我想有一个新的窗口弹出,并询问用户,如果他们想再次发挥一旦他们关闭了游戏在屏幕上。我希望有两个按钮;一个说“不”,一个说“是”。我一直坚持的是,如果用户决定要再次玩,我应该(或将)如何重新启动应用程序。我将在下面复制并粘贴我的代码:

namespace _7HeadFirstProject
{
    public partial class Form1 : Form
    { 
        Random random = new Random();
        Stats stats = new Stats();

        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            // Add a random key to the ListBox
            listBox1.Items.Add((Keys)random.Next(65, 90));
            if (listBox1.Items.Count > 7)
            {
                listBox1.Items.Clear();
                listBox1.Items.Add("Game Over");
                timer1.Stop();
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            // If the user pressed a key that's in the ListBox... 
            // ... remove it and then make the game a little faster
            if (listBox1.Items.Contains(e.KeyCode))
            {
                listBox1.Items.Remove(e.KeyCode);
                listBox1.Refresh();
                if (timer1.Interval > 400)
                    timer1.Interval -= 10;
                if (timer1.Interval > 250)
                    timer1.Interval -= 7;
                if (timer1.Interval > 100)
                    timer1.Interval -= 2;
                difficultyProgressBar.Value = 800 - timer1.Interval;

                // The user pressed a correct key, so update the Stats object...
                // ...by calling its Update() method with the argument true
                stats.Update(true);
            }
            else
            {
                // The user pressed an incorrect key, so update the Stats object...
                // ...by calling its Update() method with the argument false
                stats.Update(false);
            }

            // Update the labels on the StatusStrip
            correctLabel.Text = "Correct: " + stats.Correct;
            missedLabel.Text = "Missed: " + stats.Missed;
            totalLabel.Text = "Total: " + stats.Total;
            accuracyLabel.Text = "Accuracy: " + stats.Accuracy + "%";
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            MessageBox.Show("Would you like to play again?");
            if

        }
    }
}
不同类别:

namespace _7HeadFirstProject
{
    class Stats
    {
        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 = 100 * Correct / Total;
        }
    }
}
试试这个:

   if ((MessageBox.Show("Would you like to play again?", "Message", MessageBoxButtons.YesNo)) ==
                DialogResult.Yes)
  { 
     Application.Restart();
  }

你整个比赛都在进行中,所以别碰那种形式。向项目中添加另一个表单,然后将新表单设置为启动表单。通过打开
Program.cs
并修改此行,可以将其设置为启动窗体:

 // Instead of Form1 put the name of your new form
Application.Run(new Form1());
双击新表单并将此代码放入其中:

// Note: Your load method may have a different name.
private void Form2_Load(object sender, EventArgs e)
{
    this.StartNewGame();
}

private void GameForm_FormClosed(object sender, FormClosedEventArgs e)
{
    if (MessageBox.Show("Continue?", "Continue?", MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        this.StartNewGame();
    }
}

private void StartNewGame()
{
    // Your game form may have a different name so change this to that name
    var gameForm = new Form2();
    gameForm.FormClosed += GameForm_FormClosed;
    gameForm.Show();
}
每次用户按下对话框上的“是”按钮时,您都在创建一个全新的表单实例(游戏)。在此新表单中,您还可以拥有一个数组,该数组跟踪游戏总数以及每场游戏的分数,以便在用户选择编号时显示。您所需要的是如下内容:

var games = new List<Stats>();
// keep adding to it every time you call StartNewGame() method.
var games=新列表();
//每次调用startNewName()方法时,都要继续添加到它。

System.Windows.Forms.dlys中发生了类型为“System.ComponentModel.Win32Exception”的未处理异常。我在评论中明确了这一点。你需要把你的游戏表格的名字放在那里。我认为您的问题中游戏表单的名称是
Form1
,因此您需要更改为
var gameForm=new Form1()
。不要只是复制粘贴我的代码,你需要修改它一点,这就是为什么我在我的代码中留下注释。我知道我必须替换表单名称,我做到了。我不小心将新表单放在var gameForm=new NewFormName()中,而不是使用Form1。谢谢你的快速回复。关于如何使新表单仅在用户关闭主表单后弹出的任何见解?现在它会立即弹出。不要将代码放在主窗体的加载方法中。主窗体并不意味着要关闭,因为它是应用程序的主窗体。您可以在显示游戏时隐藏它,然后在游戏窗口关闭时,再次显示主窗体或不显示主窗体。但是不要关闭它。MessageBox运行得很好,但是,我创建了一个新的表单,它与MessageBox的用途相同,在游戏启动后会立即弹出。我不需要这个表单,但它与messagebox共享同一个类,我不知道如何在不破坏messagebox/新游戏功能的情况下摆脱它。