コード例 #1
0
        /// <summary>
        /// Button load game
        /// Voor elke line(1t/m16) set picturebox.ImageLocation
        /// Laaste vijf lines voor speler + score + beurt
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_LoadGame_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < PictureBoxes.Count(); i++)
            {
                PictureBoxes[i] = null;
            }
            Stream myStream = null;
            var    path     = Path.Combine(Directory.GetCurrentDirectory(), "../../Resources/savefiles/");

            OpenFileDialog openFile = new OpenFileDialog();         // Nieuwe fileDialog venster

            openFile.InitialDirectory = @"c:\\" + path;             // Pad naar savFiles
            openFile.Filter           = "Text files (*.txt)|*.txt"; // Filteren op .txt files
            openFile.FilterIndex      = 2;                          // Twee index filters
            openFile.RestoreDirectory = true;

            if (openFile.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    if ((myStream = openFile.OpenFile()) != null)
                    {
                        using (StreamReader reader = new StreamReader(myStream))
                        {
                            string   line;
                            string[] sline;
                            string[] pline;
                            line = reader.ReadLine();             // Lees de lines in bestand
                            int c = 0;
                            while (line != "asdfghjklqwertyuiop") // Voor elke Picturebox doe dan..
                            {
                                sline    = line.Split('|');
                                pline    = sline[1].Split(',');
                                pline[1] = pline[1].Replace("}", string.Empty);
                                PictureBoxes[c].ImageLocation = sline[0];   // Zet line naar picturebox.imageLocation
                                int x = Int32.Parse(pline[0].Split('=')[1]);
                                int y = Int32.Parse(pline[1].Split('=')[1]);
                                PictureBoxes[c].Tag       = Image.FromFile(sline[0]);
                                PictureBoxes[c].Image.Tag = sline[0];
                                PictureBoxes[c].Location  = new Point(x, y);
                                line = reader.ReadLine();   // Lees de lines in bestand
                                c++;
                                //showImages(); //<- is om te testen of loading picturebox images werkt
                            }
                        }
                    }
                    List <string> text = File.ReadLines(openFile.FileName).Reverse().Take(6).ToList();
                    naamP2     = text[3];
                    scoreP2    = Int32.Parse(text[2]);
                    scoreP1    = Int32.Parse(text[4]);
                    naamP1     = text[5];
                    MatrixSize = Int32.Parse(text[0]);
                }
                catch (Exception error)
                {
                    MessageBox.Show("Error: Could not read file from disk. Original error: " + error.Message);
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Als er een vrij slot is zoek dan nieuw image
        /// </summary>
        private PictureBox getFreeSlot()
        {
            int num;

            do
            {
                num = random.Next(0, PictureBoxes.Count());
            } while (PictureBoxes[num].Tag != null);
            return(PictureBoxes[num]);
        }