Beispiel #1
0
        /// <summary>
        /// To load a grid from file
        /// </summary>
        /// <returns></returns>
        private void Load()
        {
            List <CrossroadProperties> newProperties = new List <CrossroadProperties>();

            bf = null;
            fs = null;
            int timer = 3;

            try
            {
                DialogResult dr = openGrid.ShowDialog();
                if (dr.ToString() == "OK")
                {
                    fs          = new FileStream(openGrid.FileName, FileMode.Open, FileAccess.Read);
                    bf          = new BinaryFormatter();
                    fs.Position = 0;

                    newProperties = ((List <CrossroadProperties>)(bf.Deserialize(fs)));
                }
                if (newProperties.Count > 0)
                {
                    grid1.Controls.Clear();
                    foreach (CrossroadProperties prop in newProperties)
                    {
                        if (prop.Type == 1)
                        {
                            Crossroad A = new CrossroadA();
                            A.AllowDrop       = true;
                            A.DragOver       += Crossroad_DragOver;
                            A.BackgroundImage = Properties.Resources.Crossroad1;

                            A.Width  = 200;
                            A.Height = 200;

                            A.BackgroundImageLayout = ImageLayout.Stretch;
                            A.MouseClick           += crossroadA1_MouseDown;
                            A.delete.Click         += (sender2, eventArgs2) =>
                            {
                                grid1.Controls.Remove(A);
                            };
                            A.Location = prop.Location;
                            A.PlaceTrafficLights(3000);

                            grid1.Controls.Add(A);
                        }
                        else
                        {
                            Crossroad B = new CrossroadB();
                            B.AllowDrop             = true;
                            B.DragOver             += Crossroad_DragOver;
                            B.BackgroundImage       = Properties.Resources.Crossroad2;
                            B.Location              = grid1.PointToClient(Cursor.Position);
                            B.Width                 = 200;
                            B.Height                = 200;
                            B.BackgroundImageLayout = ImageLayout.Stretch;
                            B.delete.Click         += (sender2, eventArgs2) =>
                            {
                                grid1.Controls.Remove(B);
                            };
                            B.Location = prop.Location;
                            B.PlaceTrafficLights(3000);
                            grid1.Controls.Add(B);
                        }
                        timer = prop.TrafficLightTimer;
                        numericUpDown1.Value = prop.Cars;
                        numericUpDown3.Value = prop.TrafficLightTimer;
                    }

                    foreach (Crossroad c in grid1.Controls.OfType <Crossroad>())
                    {
                        c.SetTimerInterval(timer);
                    }
                }
            }
            catch (SerializationException) { }
            finally
            {
                if (fs != null)
                {
                    fs.Close();
                }
            }
            saved = true;
        }
Beispiel #2
0
        private void pictureBox1_DragDrop(object sender, DragEventArgs e)
        {
            Crossroad c = (Crossroad)(e.Data.GetData(e.Data.GetFormats()[0]));

            if (c != null)
            {
                if (c.Name == "crossroadA1")
                {
                    Crossroad A = new CrossroadA();
                    A.AllowDrop       = true;
                    A.DragOver       += Crossroad_DragOver;
                    A.BackgroundImage = Properties.Resources.Crossroad1;

                    A.Width  = 200;
                    A.Height = 200;

                    A.BackgroundImageLayout = ImageLayout.Stretch;

                    A.MouseDown += Crossroad_MouseDown;

                    A.delete.Click += (sender2, eventArgs2) =>
                    {
                        grid1.Controls.Remove(A);
                    };

                    //Places the crossroad in the placeholder
                    foreach (var placeholder in grid1.Placeholders)
                    {
                        if (placeholder.Contains(grid1.PointToClient(Cursor.Position)))
                        {
                            A.Location = placeholder.Location;

                            A.PlaceTrafficLights(3000);
                        }
                    }

                    //adds the crossroad to the grid
                    grid1.Controls.Add(A);
                }
                else if (c.Name == "crossroadB1")
                {
                    Crossroad B = new CrossroadB();
                    B.AllowDrop             = true;
                    B.DragOver             += Crossroad_DragOver;
                    B.BackgroundImage       = Properties.Resources.Crossroad2;
                    B.Location              = grid1.PointToClient(Cursor.Position);
                    B.Width                 = 200;
                    B.Height                = 200;
                    B.BackgroundImageLayout = ImageLayout.Stretch;
                    B.MouseDown            += Crossroad_MouseDown;
                    B.delete.Click         += (sender2, eventArgs2) =>
                    {
                        grid1.Controls.Remove(B);
                    };

                    foreach (var placeholder in grid1.Placeholders)
                    {
                        if (placeholder.Contains(grid1.PointToClient(Cursor.Position)))
                        {
                            B.Location = placeholder.Location;
                            B.PlaceTrafficLights(3000);
                        }
                    }

                    grid1.Controls.Add(B);
                }
            }
        }