Пример #1
0
        private void AddComponent(BaseComponent component, Point location)
        {
            try
            {
                //Sets the component as a specific component, depending on the ComponentType of component.
                switch (component.ComponentType)
                {
                case ComponentType.Resistor:
                    component = new Resistor(component.Direction, component.Resistance, component.Voltage,
                                             component.Current);
                    break;

                case ComponentType.Voltmeter:
                    component = new Voltmeter(component.Direction, component.Resistance, component.Voltage,
                                              component.Current);
                    break;

                case ComponentType.Ammeter:
                    component = new Ammeter(component.Direction, component.Resistance, component.Voltage,
                                            component.Current);
                    break;

                case ComponentType.Cell:
                    component = new Cell(component.Direction, component.Resistance, component.Voltage,
                                         component.Current);
                    break;

                case ComponentType.WireStraight:
                    component = new Wire(component.Direction, component.Resistance, component.Voltage,
                                         component.Current);
                    break;

                case ComponentType.WireTwoAlternate:
                    component = new WireTwoWayAlt(component.Direction, component.Resistance, component.Voltage,
                                                  component.Current);
                    break;

                case ComponentType.WireThreeWay:
                    component = new WireThreeWay(component.Direction, component.Resistance, component.Voltage,
                                                 component.Current);
                    break;

                case ComponentType.WireFourWay:
                    component = new WireFourWay(component.Direction, component.Resistance, component.Voltage,
                                                component.Current);
                    break;

                default:
                    throw new ArgumentNullException();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            component.Location = location;

            var pictureBox = new PictureBox
            {
                Location  = location,
                BackColor = Color.Transparent,
                SizeMode  = PictureBoxSizeMode.AutoSize,
                Tag       = component.ID
            };


            //Events.
            pictureBox.MouseMove  += p_MouseMove;
            pictureBox.MouseClick += P_MouseClick;
            pictureBox.MouseWheel += P_MouseWheel;
            pictureBox.MouseHover += P_MouseHover;
            pictureBox.Image       = component.CurrentImage;


            Components.Add(component);
            //Adds the component to the main panel
            panelCircuitBoard.Controls.Add(pictureBox);
        }
Пример #2
0
        private void AddComponent(ComponentType componentType, int posX, int posY,
                                  ComponentDirection direction = ComponentDirection.Right, double resistance = 0, double voltage = 0,
                                  double current = 0)
        {
            //Ensures that the PictureBox that is to be created will be on a square.
            posX = RoundDownToNearest(posX, 50);
            posY = RoundDownToNearest(posY, 50);

            BaseComponent component;

            try
            {
                //Sets the component as a specific component, depending on the ComponentType the method receives.
                switch (componentType)
                {
                case ComponentType.Resistor:
                    component = new Resistor(direction, resistance, current, voltage);
                    break;

                case ComponentType.Voltmeter:
                    component = new Voltmeter(direction, resistance, current, voltage);
                    break;

                case ComponentType.Ammeter:
                    component = new Ammeter(direction, resistance, current, voltage);
                    break;

                case ComponentType.Cell:
                    component = new Cell(direction, resistance, current, voltage);
                    break;

                case ComponentType.WireStraight:
                    component = new Wire(direction, resistance, current, voltage);
                    break;

                case ComponentType.WireTwoAlternate:
                    component = new WireTwoWayAlt(direction, resistance, current, voltage);
                    break;

                case ComponentType.WireThreeWay:
                    component = new WireThreeWay(direction, resistance, current, voltage);
                    break;

                case ComponentType.WireFourWay:
                    component = new WireFourWay(direction, resistance, current, voltage);
                    break;

                default:
                    throw new ArgumentNullException();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            //Sets the location of component.
            component.Location = Location;

            var pictureBox = new PictureBox
            {
                Tag = component.ID,
                //Sets the location of the PictureBox.
                Location  = new Point(posX, posY),
                BackColor = Color.Transparent,
                SizeMode  = PictureBoxSizeMode.AutoSize
            };

            //Events.
            pictureBox.MouseMove  += p_MouseMove;
            pictureBox.MouseClick += P_MouseClick;
            pictureBox.MouseWheel += P_MouseWheel;
            pictureBox.MouseHover += P_MouseHover;
            pictureBox.Image       = component.CurrentImage;

            Components.Add(component);
            //Adds the component to the main panel.
            panelCircuitBoard.Controls.Add(pictureBox);
        }