示例#1
0
        private void loadGameButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog
            {
                AddExtension    = true,
                DefaultExt      = "mon",
                CheckPathExists = true,
                //sfd.FileName = "game.mon";
                Filter           = "Monopoly saved games (*.mon)|*.mon",
                InitialDirectory = AppDomain.CurrentDomain.BaseDirectory,
                Title            = "Choose a game to load",
                ValidateNames    = true
            };

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                if (File.Exists(ofd.FileName))
                {
                    Game g;
                    try
                    {
                        Stream          openFileStream = File.OpenRead(ofd.FileName);
                        BinaryFormatter deserializer   = new BinaryFormatter();
                        g = (Game)deserializer.Deserialize(openFileStream);
                        openFileStream.Close();

                        Hide();
                        using (Monopoly gw = new Monopoly(g.GetPlayers()))
                        {
                            gw.Show();
                            g.SetWindow(gw);
                            g.UpdateWindow();
                            gw.Hide();
                            if (gw.ShowDialog() == DialogResult.OK)
                            {
                                Show();
                            }
                        };
                    }
                    catch (IOException ex)
                    {
                        Console.WriteLine(ex.StackTrace);
                        MessageBox.Show("Failed to load game.");
                    }
                }
            }
        }